forked from tinygrad/tinygrad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
diff LazyBuffer schedules in process replay (tinygrad#5996)
* start diff printing * this should be 2 * add to process_replay.py * enable schedule capture * arange diff is process replay
- Loading branch information
Showing
9 changed files
with
95 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,4 @@ weights | |
comgr_* | ||
*.pkl | ||
site/ | ||
master_schedule.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,57 @@ | ||
# create a diff of two schedule graphs | ||
import difflib #, ocdiff | ||
import shutil, importlib, uuid, os | ||
from collections import defaultdict | ||
from typing import DefaultDict, List, Set, Tuple | ||
from test.external.process_replay.utils import print_diff | ||
from tinygrad.engine.schedule import LBScheduleItem, ScheduleItem | ||
from tinygrad.helpers import Context, colored | ||
from tinygrad.helpers import DEBUG, Context, colored, dedup, diskcache_put, fetch, getenv | ||
from tinygrad.lazy import LazyBuffer | ||
from tinygrad.ops import LazyOp | ||
from tinygrad.engine.realize import CompiledRunner, lower_schedule_item | ||
|
||
def process_replay(outs:List[LazyBuffer], graph:DefaultDict[LBScheduleItem, List[LBScheduleItem]], in_degree:DefaultDict[LBScheduleItem, int]): | ||
# copy the reference module | ||
fp = __file__.replace("diff_schedule", "master_schedule") | ||
if not os.path.isfile(fp): shutil.copyfile(fetch("https://raw.githubusercontent.com/tinygrad/tinygrad/master/tinygrad/engine/schedule.py"), fp) | ||
# create the reference graph | ||
ref_graph, ref_in_degree = importlib.import_module("test.external.process_replay.master_schedule")._graph_schedule(outs, set()) | ||
# compare | ||
diff_schedule([(ref_graph, ref_in_degree), (graph, in_degree)]) | ||
|
||
def diff_schedule(s:List[Tuple[DefaultDict[LBScheduleItem, List[LBScheduleItem]], DefaultDict[LBScheduleItem, int]]]) -> int: | ||
si_for_buf: DefaultDict[LazyBuffer, List[ScheduleItem]] = defaultdict(list) | ||
for _,in_degree in s: | ||
for lsi in in_degree: | ||
for buf in lsi.outputs: | ||
si_for_buf[buf].append(ScheduleItem(lsi.ast, tuple(x.buffer for x in lsi.outputs+lsi.inputs if x.size != 0), lsi.metadata)) | ||
changed = 0 | ||
seen_diff: Set[Tuple[LazyOp, LazyOp]] = set() | ||
seen_diffs: Set[Tuple[LazyOp, ...]] = set() | ||
for buf, si in si_for_buf.items(): | ||
asts = [x.ast for x in si] | ||
if len(set(asts)) == 1: continue | ||
if (asts[0], asts[1]) in seen_diff: continue | ||
seen_diff.add((asts[0], asts[1])) | ||
asts = tuple(dedup([x.ast for x in si])) | ||
# kernels didn't change | ||
if len(si) > 1 and len(asts) == 1: continue | ||
if asts in seen_diffs: continue | ||
seen_diffs.add(asts) | ||
changed += 1 | ||
#print(ocdiff.console_diff(render(ast[0]), render(ast[1]))) | ||
ei0 = lower_schedule_item(si[0]) | ||
ei1 = lower_schedule_item(si[1]) | ||
assert isinstance(ei0.prg, CompiledRunner) and isinstance(ei1.prg, CompiledRunner) | ||
diff = list(difflib.unified_diff(ei0.prg.p.src.splitlines(), ei1.prg.p.src.splitlines())) | ||
unified_diff = "\n".join(colored(line, "red" if line.startswith("-") else "green" if line.startswith("+") else None) for line in diff) | ||
print(unified_diff) | ||
if getenv("RUN_PROCESS_REPLAY"): diskcache_put("schedule_diff", str(uuid.uuid4()), (str(buf), asts)) | ||
if len(asts) == 1: | ||
print(f"{buf} folded in the second schedule") | ||
else: print_si_diff(si[0], si[1]) | ||
if DEBUG >= 1: print(f"*** process replay: {changed} unique kernel{'s' if changed>1 else ''} changed") | ||
return changed | ||
|
||
def print_si_diff(si0:ScheduleItem, si1:ScheduleItem): | ||
ei0 = lower_schedule_item(si0) | ||
ei1 = lower_schedule_item(si1) | ||
assert isinstance(ei0.prg, CompiledRunner) and isinstance(ei1.prg, CompiledRunner) | ||
print_diff(si0.ast, si1.ast) | ||
print_diff(ei0.prg.p.src, ei1.prg.p.src) | ||
# TODO: create new Buffers for process replay | ||
if getenv("TIMING"): | ||
with Context(DEBUG=2): | ||
tm0 = ei0.run(wait=True) | ||
tm1 = ei1.run(wait=True) | ||
assert tm0 is not None and tm1 is not None | ||
tm_diff = ((tm0 - tm1) / tm0) * 100 | ||
if tm_diff > 0: print(colored(f"{tm_diff:.2f}% faster", "green")) | ||
else: print(colored(f"{tm_diff:,.2f}% slower", "red")) | ||
print(f"{changed} unique kernel{'s' if changed>1 else ''} changed") | ||
return changed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import difflib, logging | ||
from tinygrad.helpers import colored, getenv | ||
|
||
def print_diff(s0, s1, unified=getenv("UNIFIED_DIFF",1)): | ||
if unified: | ||
lines = list(difflib.unified_diff(str(s0).splitlines(), str(s1).splitlines())) | ||
diff = "\n".join(colored(line, "red" if line.startswith("-") else "green" if line.startswith("+") else None) for line in lines) | ||
else: | ||
import ocdiff | ||
diff = ocdiff.console_diff(str(s0), str(s1)) | ||
logging.info(diff) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters