Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
makslevental committed Dec 5, 2023
1 parent 3322185 commit 76f35ab
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 107 deletions.
1 change: 1 addition & 0 deletions .github/workflows/buildAndTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
- name: Install Python packages
run: |
pip install cmake numpy psutil pybind11 rich pkginfo lit PyYAML
pip install -r python/requirements.txt
- name: Install packages
run: sudo apt-get install -y ninja-build clang lld
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/lintAndFormat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- name: Install Python packages
run: |
pip install cmake numpy psutil pybind11 rich pkginfo lit PyYAML requests
pip install -r python/requirements.txt
- name: Get MLIR
id: mlir-wheels
Expand Down Expand Up @@ -270,6 +271,7 @@ jobs:
- name: Install Python and other packages
run: |
pip install cmake numpy psutil pybind11 rich lit
pip install -r python/requirements.txt
- name: Install Ninja
run: sudo apt-get install -y ninja-build clang lld llvm
Expand Down
5 changes: 4 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ check-str-concat-over-line-jumps=yes
# C0114: Missing module docstring (missing-module-docstring)
# E0402: Attempted relative import beyond top-level package (relative-beyond-top-level)
# C0115: Missing class docstring (missing-class-docstring)
disable=C0116,C0114,E0402,C0115
# C0415: Import outside toplevel
# R0913: Too many arguments
# R0914: Too many local variables
disable=C0116,C0114,E0402,C0115,C0415,R0913,R0914
4 changes: 3 additions & 1 deletion python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ PyYAML>=5.3.1, <=6.0.1
dataclasses>=0.6, <=0.8
networkx[default]
ortools
gurobipy
gurobipy
matplotlib
ortools
40 changes: 22 additions & 18 deletions python/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def infer_mlir_type(
"""
if isinstance(py_val, bool):
return T.bool()
elif isinstance(py_val, int):
if isinstance(py_val, int):
# no clue why but black can't decide which it wants the **
# fmt: off
if -(2 ** 31) <= py_val < 2 ** 31:
return T.i32()
elif 2 ** 31 <= py_val < 2 ** 32:
Expand All @@ -72,7 +74,9 @@ def infer_mlir_type(
return T.ui64()
else:
raise RuntimeError(f"Nonrepresentable integer {py_val}.")
elif isinstance(py_val, float):
# fmt: on

if isinstance(py_val, float):
if (
abs(py_val) == float("inf")
or abs(py_val) == 0.0
Expand All @@ -82,13 +86,13 @@ def infer_mlir_type(
return T.f32()
else:
return T.f64()
elif isinstance(py_val, np.ndarray):
if isinstance(py_val, np.ndarray):
dtype = np_dtype_to_mlir_type(py_val.dtype.type)
return RankedTensorType.get(py_val.shape, dtype)
else:
raise NotImplementedError(
f"Unsupported Python value {py_val=} with type {type(py_val)}"
)

raise NotImplementedError(
f"Unsupported Python value {py_val=} with type {type(py_val)}"
)


def mlir_type_to_np_dtype(mlir_type):
Expand Down Expand Up @@ -218,17 +222,17 @@ def route_using_cp(

# what goes in must come out
model.Add(
sum([flow_var[e] for e in DG.in_edges(nbunch=n)])
== sum([flow_var[e] for e in DG.out_edges(nbunch=n)])
sum(flow_var[e] for e in DG.in_edges(nbunch=n))
== sum(flow_var[e] for e in DG.out_edges(nbunch=n))
)

# flow must leave src, and must not enter src
model.Add(sum([flow_var[src, j] for j in DG.neighbors(src)]) == 1)
model.Add(sum([flow_var[i, src] for i in DG.neighbors(src)]) == 0)
model.Add(sum(flow_var[src, j] for j in DG.neighbors(src)) == 1)
model.Add(sum(flow_var[i, src] for i in DG.neighbors(src)) == 0)

# flow must enter tgt, and must not leave tgt
model.Add(sum([flow_var[tgt, j] for j in DG.neighbors(tgt)]) == 0)
model.Add(sum([flow_var[i, tgt] for i in DG.neighbors(tgt)]) == 1)
model.Add(sum(flow_var[tgt, j] for j in DG.neighbors(tgt)) == 0)
model.Add(sum(flow_var[i, tgt] for i in DG.neighbors(tgt)) == 1)

# Create demand variables
total_demand = {
Expand All @@ -241,7 +245,7 @@ def route_using_cp(

# Add demand/flow relationship
for i, j, attrs in DG.edges(data=True):
model.Add(total_demand[i, j] == sum([f[i, j] for f in flat_flow_vars]))
model.Add(total_demand[i, j] == sum(f[i, j] for f in flat_flow_vars))
model.Add(total_demand[i, j] <= attrs["capacity"])

if min_edges:
Expand All @@ -268,15 +272,15 @@ def route_using_cp(
)
)

obj = sum([total_demand[i, j] for i, j in DG.edges])
obj = sum(total_demand[i, j] for i, j in DG.edges)
if min_edges:
obj += sum([used_edges[i, j] for i, j in DG.edges])
obj += sum(used_edges[i, j] for i, j in DG.edges)
else:
obj += sum([overlapping_demands[i, j] for i, j in DG.edges])
obj += sum(overlapping_demands[i, j] for i, j in DG.edges)
model.Minimize(obj)

status = solver.Solve(model)
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
if status in {cp_model.OPTIMAL, cp_model.FEASIBLE}:
flow_paths = {}
for flow, flow_varss in flow_vars.items():
flow_paths[flow] = [
Expand Down
Loading

0 comments on commit 76f35ab

Please sign in to comment.