-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_tile_composition.py
More file actions
273 lines (214 loc) · 11 KB
/
05_tile_composition.py
File metadata and controls
273 lines (214 loc) · 11 KB
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
#!/usr/bin/env python3
"""FLUX Tile Composition — Building with the Tile System.
Demonstrates the tile system:
- Load built-in tiles from the library
- Create a custom tile (audio_filter)
- Compose tiles: chain, parallel, nest
- Show the tile graph
- Search the registry
Run:
PYTHONPATH=src python3 examples/05_tile_composition.py
"""
from __future__ import annotations
from typing import Any
# ── ANSI helpers ──────────────────────────────────────────────────────────
CYAN = "\033[96m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
MAGENTA = "\033[95m"
BOLD = "\033[1m"
DIM = "\033[2m"
RESET = "\033[0m"
def header(text: str) -> None:
width = 64
print()
print(f"{BOLD}{MAGENTA}{'═' * width}{RESET}")
print(f"{BOLD}{MAGENTA} {text}{RESET}")
print(f"{BOLD}{MAGENTA}{'═' * width}{RESET}")
def info(text: str) -> None:
print(f" {GREEN}✓{RESET} {text}")
def detail(text: str) -> None:
print(f" {DIM}{text}{RESET}")
def section(text: str) -> None:
print()
print(f"{BOLD}{CYAN}── {text} {'─' * (56 - len(text))}{RESET}")
# ══════════════════════════════════════════════════════════════════════════
# Custom Tile Blueprint (FIR Emitter)
# ══════════════════════════════════════════════════════════════════════════
def _audio_filter_blueprint(
builder: Any,
inputs: dict[str, Any],
params: dict[str, Any],
) -> dict[str, Any]:
"""FIR blueprint for a custom audio_filter tile.
Applies a configurable low-pass filter: output = input * (1 - alpha) + prev * alpha
"""
from flux.fir.types import TypeContext, IntType
from flux.fir.values import Value
data = inputs.get("data")
if data is None:
return {}
alpha = params.get("alpha", 128) # fixed-point alpha
# Simulate: output = call("_audio_filter_fn", [data], i32)
result = builder.call("_audio_filter_fn", [data], IntType(32, True))
if result is not None:
return {"result": result}
return {}
# ══════════════════════════════════════════════════════════════════════════
# Main
# ══════════════════════════════════════════════════════════════════════════
if __name__ == "__main__":
print()
print(f"{BOLD}{YELLOW}{'╔' + '═' * 62 + '╗'}{RESET}")
print(f"{BOLD}{YELLOW}{'║'} FLUX Tile Composition — Building with Tiles {'║'}{RESET}")
print(f"{BOLD}{YELLOW}{'║'} Like a DJ layering samples from a vinyl library {'║'}{RESET}")
print(f"{BOLD}{YELLOW}{'╚' + '═' * 62 + '╝'}{RESET}")
from flux.tiles.registry import TileRegistry, default_registry
from flux.tiles.tile import Tile, TileType, CompositeTile, ParallelTile
from flux.tiles.library import ALL_BUILTIN_TILES
from flux.tiles.ports import TilePort, PortDirection
from flux.fir.types import TypeContext, IntType
_ctx = TypeContext()
_i32 = _ctx.get_int(32)
# ── Step 1: Explore the built-in tile library ───────────────────
section("Step 1: Built-In Tile Library")
info(f"Registry: {default_registry}")
info(f"Total tiles available: {default_registry.count}")
# Group by type
by_type: dict[str, list] = {}
for tile in default_registry.all_tiles:
t = tile.tile_type.value
by_type.setdefault(t, []).append(tile)
print()
for ttype, tiles in sorted(by_type.items()):
names = ", ".join(t.name for t in sorted(tiles, key=lambda x: x.name))
detail(f" {ttype:<10s} ({len(tiles):>2d}): {names}")
# ── Step 2: Search the registry ──────────────────────────────────
section("Step 2: Tile Search & Discovery")
queries = ["map", "memory", "loop", "tell", "print"]
for query in queries:
results = default_registry.search(query)
names = [t.name for t in results[:5]]
info(f"Search '{query}' → {len(results)} results: {', '.join(names)}")
# ── Step 3: Inspect a tile in detail ─────────────────────────────
section("Step 3: Tile Anatomy — Inspect 'map' Tile")
map_tile = default_registry.get("map")
if map_tile:
info(f"Name: {map_tile.name}")
info(f"Type: {map_tile.tile_type.value}")
info(f"Cost estimate: {map_tile.cost_estimate}")
info(f"Abstraction: {map_tile.abstraction_level} (1=low-level, 10=high)")
info(f"Language pref: {map_tile.language_preference}")
info(f"Has FIR blueprint: {map_tile.fir_blueprint is not None}")
info(f"Parameters: {map_tile.params}")
info(f"Tags: {', '.join(sorted(map_tile.tags))}")
detail(f"Inputs:")
for port in map_tile.inputs:
detail(f" {port.name} ({port.direction.value})")
detail(f"Outputs:")
for port in map_tile.outputs:
detail(f" {port.name} ({port.direction.value})")
# ── Step 4: Create a custom tile ────────────────────────────────
section("Step 4: Create Custom Tile — 'audio_filter'")
audio_filter = Tile(
name="audio_filter",
tile_type=TileType.COMPUTE,
inputs=[
TilePort("data", PortDirection.INPUT, _i32),
],
outputs=[
TilePort("result", PortDirection.OUTPUT, _i32),
],
params={"alpha": 128, "cutoff_hz": 8000},
fir_blueprint=_audio_filter_blueprint,
cost_estimate=2.5,
abstraction_level=5,
language_preference="fir",
tags={"audio", "dsp", "filter", "low-pass", "custom"},
)
info(f"Created tile: {audio_filter}")
info(f" Type: {audio_filter.tile_type.value}")
info(f" Cost: {audio_filter.cost_estimate}")
info(f" Tags: {', '.join(sorted(audio_filter.tags))}")
# Register it
default_registry.register(audio_filter)
info(f"Registered in default_registry (now {default_registry.count} tiles)")
# ── Step 5: Tile composition — Chaining ─────────────────────────
section("Step 5: Tile Composition — Chain")
map_t = default_registry.get("map")
filter_t = default_registry.get("audio_filter")
if map_t and filter_t:
# Chain: map → audio_filter
composed = map_t.compose(filter_t, mapping={"result": "data"})
info(f"Composed tile: {composed.name}")
info(f" Inputs: {[p.name for p in composed.inputs]}")
info(f" Outputs: {[p.name for p in composed.outputs]}")
info(f" Total cost: {composed.cost_estimate}")
detail(f" Data flows: map.result → audio_filter.data")
# ── Step 6: Tile composition — Parallel ─────────────────────────
section("Step 6: Tile Composition — Parallel")
loop_t = default_registry.get("loop")
if loop_t:
parallel = loop_t.parallel(4)
info(f"Parallel tile: {parallel.name}")
info(f" Replications: {parallel.count}")
info(f" Total cost: {parallel.cost_estimate} (was {loop_t.cost_estimate})")
detail(f" 4 parallel loop instances")
# ── Step 7: Three-way chain ─────────────────────────────────────
section("Step 7: Three-Way Chain — map → filter → reduce")
reduce_t = default_registry.get("reduce")
if map_t and filter_t and reduce_t:
chain = map_t.compose(filter_t, mapping={"result": "data"})
chain2 = chain.compose(reduce_t, mapping={"result": "data"})
info(f"Chain: {chain2.name}")
info(f" Inputs: {[p.name for p in chain2.inputs]}")
info(f" Outputs: {[p.name for p in chain2.outputs]}")
info(f" Total cost: {chain2.cost_estimate}")
detail(f" Data flow: map.result → filter.data → reduce.data")
# ── Step 8: Find alternatives ────────────────────────────────────
section("Step 8: Find Tile Alternatives")
if map_t:
alternatives = default_registry.find_alternatives(map_t)
if alternatives:
info(f"Alternatives to 'map' (same port signature):")
for alt in alternatives:
detail(f" {alt.name:<16s} cost={alt.cost_estimate:.1f} "
f"type={alt.tile_type.value}")
else:
info("No alternatives found for 'map' (unique port signature)")
# ── Step 9: Most/least expensive tiles ───────────────────────────
section("Step 9: Cost Analysis")
expensive = default_registry.most_expensive(5)
cheap = default_registry.least_expensive(5)
info("Most expensive tiles:")
for t in expensive:
detail(f" {t.name:<18s} cost={t.cost_estimate:.1f} "
f"type={t.tile_type.value:<10s} abstract={t.abstraction_level}")
info("Least expensive tiles:")
for t in cheap:
detail(f" {t.name:<18s} cost={t.cost_estimate:.1f} "
f"type={t.tile_type.value:<10s} abstract={t.abstraction_level}")
# ── Step 10: Tile graph visualization ────────────────────────────
section("Step 10: Tile Type Distribution")
type_counts = {}
for tile in default_registry.all_tiles:
tt = tile.tile_type.value
type_counts[tt] = type_counts.get(tt, 0) + 1
total = sum(type_counts.values())
max_count = max(type_counts.values()) if type_counts else 1
for tt in sorted(type_counts.keys()):
count = type_counts[tt]
bar_len = int(count / max_count * 30)
bar = "█" * bar_len + "░" * (30 - bar_len)
pct = count / total * 100
print(f" {tt:<12s} {count:>2d} ({pct:>5.1f}%) {CYAN}{bar}{RESET}")
# ── Step 11: Tile instantiation ─────────────────────────────────
section("Step 11: Tile Instantiation (Parameter Binding)")
if map_t:
instance = map_t.instantiate(fn="my_custom_map_fn")
info(f"Instance of 'map': {instance}")
info(f" Bound params: {instance.params}")
detail(f" fn parameter overridden: 'my_custom_map_fn'")
print()
print(f"{BOLD}{GREEN}── Done! ──{RESET}")
print()