-
Notifications
You must be signed in to change notification settings - Fork 136
Add initial cgra compiler #704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| # Copyright (c) Facebook, Inc. and its affiliates. | ||
| # | ||
| # This source code is licensed under the MIT license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
| import random | ||
|
|
@@ -24,41 +29,26 @@ def __str__(self): | |
| return "Node with name " + self.name + " and op " + str(self.operation) | ||
|
|
||
| class DFG(object): | ||
| def __init__(self, working_directory: Optional[Path] = None, benchmark: Optional[Benchmark] = None, from_json: Optional[Path] = None, from_text: Optional[str] = None): | ||
| def __init__(self, working_directory: Optional[Path] = None, from_json: Optional[Path] = None, from_text: Optional[str] = None): | ||
| # Copied from here: https://github.com/facebookresearch/CompilerGym/blob/development/examples/loop_optimizations_service/service_py/loops_opt_service.py | ||
| # self.inst2vec = _INST2VEC_ENCODER | ||
|
|
||
| if from_json is not None: | ||
| self.load_dfg_from_json(from_json) | ||
| elif from_text is not None: | ||
| self.load_dfg_from_text(from_text) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need a final |
||
| elif benchmark is not None: | ||
| # Only re-create the JSON file if we aren't providing an existing one. | ||
| # The existing ones are mostly a debugging functionality. | ||
| with open(self.working_directory / "benchmark.c", "wb") as f: | ||
| f.write(benchmark.program.contents) | ||
|
|
||
| # We use CGRA-Mapper to produce a DFG in JSON. | ||
| run_command( | ||
| ["cgra-mapper", self.src_path, self.dfg_path] | ||
| ) | ||
|
|
||
| # Now, load in the DFG. | ||
| self.load_dfg_from_json(self.dfg_path) | ||
|
|
||
| def __str__(self): | ||
| res = "nodes are: " + str(self.nodes) + " and edges are " + str(self.adj) | ||
| return res | ||
|
|
||
| def load_dfg_from_json(self, path): | ||
| import json | ||
| with open(path, 'r') as p: | ||
| # This isnt' text, but I think the json.loads | ||
| # that this calls just works? | ||
| self.load_dfg_from_text(p) | ||
|
|
||
| def load_dfg_from_text(self, text): | ||
| import json | ||
| f = json.loads(text) | ||
| self.nodes = {} | ||
| self.node_names = [] | ||
|
|
@@ -97,7 +87,13 @@ def get_succs(self, node): | |
| succs.append(self.nodes[n]) | ||
| return succs | ||
|
|
||
| # TODO -- fix this, because for a graph with multiple entry nodes, | ||
| def build_preds_lookup(self): | ||
| preds_lookup = {} | ||
| for n in self.node_names: | ||
| preds_lookup[n] = self.get_preds(self.nodes[n]) | ||
| return preds_lookup | ||
|
|
||
| # TODO(jcw) -- fix this, because for a graph with multiple entry nodes, | ||
| # this doesn't actually give the right answer :) | ||
| # (should do in most cases) | ||
| def bfs(self): | ||
|
|
@@ -106,6 +102,10 @@ def bfs(self): | |
| print(self.entry_points) | ||
| seen = set() | ||
|
|
||
| # build a lookup based on the predecessors | ||
| # for each node. | ||
| preds_lookup = self.build_preds_lookup() | ||
|
|
||
| while len(to_explore) > 0: | ||
| head = to_explore[0] | ||
| to_explore = to_explore[1:] | ||
|
|
@@ -114,9 +114,30 @@ def bfs(self): | |
| seen.add(head) | ||
| yield self.nodes[head] | ||
|
|
||
| # Get the following nodes. | ||
| following_nodes = self.adj[head] | ||
| to_explore += following_nodes | ||
| # Add the next batch of nodes that we have | ||
| # visited all the preds for if there are more | ||
| # nodes to explore. | ||
| if len(to_explore) == 0 and len(seen) != len(self.node_names): | ||
| for node_name in self.node_names: | ||
| if node_name in seen: | ||
| continue | ||
| else: | ||
| # Unseen --- have we seen all th preds? | ||
| failed = False | ||
| for p in preds_lookup[node_name]: | ||
| if p.name not in seen: | ||
| failed = True | ||
| if not failed: | ||
| to_explore.append(node_name) | ||
| if len(to_explore) == 0: # We added nothing despite trying | ||
| # to. | ||
|
|
||
| # TODO(jcw) -- Fix this, as support for cyclical DFGs | ||
| # is important to be able to support loops with | ||
| # cross-loop dependencies. | ||
| print("Cyclical DFG --- Impossible to do a true BFS") | ||
| print("DFG is ", str(self)) | ||
| assert False | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: don't assert False, raise an exception. Or, if that's not possible, |
||
|
|
||
| # Generate a test DFG using the operations in | ||
| # 'operations'. | ||
|
|
@@ -175,7 +196,7 @@ def generate_DFG(operations: List[Operation], size, seed=0): | |
| else: | ||
| inputs.append(random.choice(nodes_list)) | ||
| # If the node has no arguments, then we should add it | ||
| # as an entry point. --- todo --- should we just skip | ||
| # as an entry point. --- todo(jcw) --- should we just skip | ||
| # this avoid creating graphs with too many constant loads? | ||
| if operation.inputs == 0: | ||
| entry_points.append(name) | ||
|
|
||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Copyright (c) Facebook, Inc. and its affiliates. | ||
| # | ||
| # This source code is licensed under the MIT license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "architectures", | ||
| srcs = [ | ||
| "__init__.py", | ||
| "CGRA.py" | ||
| ], | ||
| visibility = ["//visibility:public"] | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.