Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler_gym/datasets/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ def dataset_from_parsed_uri(self, uri: BenchmarkUri) -> Dataset:
key = self._dataset_key_from_uri(uri)

if key not in self._datasets:
print("datasets are ", str(self._datasets))
raise LookupError(f"Dataset not found: {key}")

return self._datasets[key]
Expand Down
4 changes: 0 additions & 4 deletions compiler_gym/envs/cgra/.gitignore

This file was deleted.

4 changes: 3 additions & 1 deletion compiler_gym/envs/cgra/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#
# 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_test")
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "cgra",
srcs = [
"__init__.py",
"cgra_rewards.py",
"Operations.py",
"compile_settings.py",
"DFG.py"
],
data = [
Expand All @@ -18,6 +19,7 @@ py_library(
visibility = ["//visibility:public"],
deps = [
"//compiler_gym/envs/cgra/datasets",
"//compiler_gym/envs/cgra/architectures",
"//compiler_gym/errors",
"//compiler_gym/service:client_service_compiler_env",
"//compiler_gym/service/runtime", # Implicit dependency of service.
Expand Down
14 changes: 8 additions & 6 deletions compiler_gym/envs/cgra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ cg_add_all_subdirs()

cg_py_library(
NAME
gcc
cgra
SRCS
"__init__.py"
"gcc.py"
"gcc_env.py"
"gcc_rewards.py"
"cgra_rewards.py"
"Operations.py"
"compile_settings.py"
"DFG.py"
DATA
compiler_gym::envs::gcc::service::service
compiler_gym::envs::cgra::service::service
DEPS
compiler_gym::service::client_service_compiler_env
compiler_gym::envs::gcc::datasets::datasets
compiler_gym::envs::cgra::datasets::datasets
compiler_gym::engs::cgra::architectures::architectures
compiler_gym::errors::errors
compiler_gym::service::runtime::runtime
compiler_gym::util::util
Expand Down
63 changes: 42 additions & 21 deletions compiler_gym/envs/cgra/DFG.py
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
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a final else branch to raise an error if neither is set?

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 = []
Expand Down Expand Up @@ -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):
Expand All @@ -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:]
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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, sys.exit(1).


# Generate a test DFG using the operations in
# 'operations'.
Expand Down Expand Up @@ -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)
Expand Down
98 changes: 0 additions & 98 deletions compiler_gym/envs/cgra/Driver.py

This file was deleted.

2 changes: 0 additions & 2 deletions compiler_gym/envs/cgra/Model.py

This file was deleted.

8 changes: 6 additions & 2 deletions compiler_gym/envs/cgra/Operations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 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.

class Operation(object):
def __init__(self, name, inputs, outputs, latency):
Expand All @@ -10,7 +14,7 @@ def __str__(self):
return self.name

Operations = [
# TODO --- should we support more operations as heterogeneous?
# TODO(jcw) --- should we support more operations as heterogeneous?
# IMO most of the other things that are scheduled are
# pretty vacuous, although we could explore supporting those.
# Operation is: name, inputs, outputs, cycles.
Expand Down Expand Up @@ -44,7 +48,7 @@ def operation_index_of(op):
return -1

def operation_latency(op):
# TODO --- model latency --- or at least expost this
# TODO(jcw) --- model latency --- or at least expost this
# to a configuration.
return op.latency

Expand Down
15 changes: 15 additions & 0 deletions compiler_gym/envs/cgra/architectures/BUILD
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"]
)
Loading