Skip to content

Commit

Permalink
Revert "Move numeric_types.py from fud/verilator to calyx-py (#…
Browse files Browse the repository at this point in the history
…1715)" (#1718)

This reverts commit 1edf3ec.
  • Loading branch information
calebmkim authored Sep 7, 2023
1 parent 1edf3ec commit 26a11b8
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:
- name: Run Python Tests
working-directory: /home/calyx
run: pytest calyx-py/test/numeric_types.py
run: pytest fud/fud/stages/verilator/tests/numeric_types.py

evaluation:
name: Polybench Integration
Expand Down
30 changes: 20 additions & 10 deletions calyx-py/calyx/gen_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
)
from calyx.utils import float_to_fixed_point
from math import factorial, log2
from calyx.numeric_types import FixedPoint
from fud.stages.verilator import numeric_types
from calyx.gen_ln import generate_ln

from calyx.builder import (
Builder,
ComponentBuilder,
Expand All @@ -18,6 +19,7 @@
if_with,
invoke,
CellBuilder,
ExprBuilder,
const,
HI,
par,
Expand Down Expand Up @@ -50,7 +52,7 @@ def generate_fp_pow_component(

# groups
with comp.group("init") as init:
pow.in_ = FixedPoint(
pow.in_ = numeric_types.FixedPoint(
"1.0", width, int_width, is_signed=is_signed
).unsigned_integer()
pow.write_en = 1
Expand Down Expand Up @@ -103,12 +105,14 @@ def generate_cells(
comp.const(
"one",
width,
FixedPoint("1.0", width, int_width, is_signed=is_signed).unsigned_integer(),
numeric_types.FixedPoint(
"1.0", width, int_width, is_signed=is_signed
).unsigned_integer(),
)
comp.const(
"e",
width,
FixedPoint(
numeric_types.FixedPoint(
str(float_to_fixed_point(2.7182818284, frac_width)),
width,
int_width,
Expand All @@ -120,7 +124,7 @@ def generate_cells(
comp.const(
"negative_one",
width,
FixedPoint(
numeric_types.FixedPoint(
"-1.0", width, int_width, is_signed=is_signed
).unsigned_integer(),
)
Expand Down Expand Up @@ -164,7 +168,7 @@ def generate_cells(
# reciprocal factorials
for i in range(2, degree + 1):
fixed_point_value = float_to_fixed_point(1.0 / factorial(i), frac_width)
value = FixedPoint(
value = numeric_types.FixedPoint(
str(fixed_point_value), width, int_width, is_signed=is_signed
).unsigned_integer()
comp.const(f"reciprocal_factorial{i}", width, value)
Expand Down Expand Up @@ -536,7 +540,9 @@ def gen_constant_cell(
return comp.const(
name,
width,
FixedPoint(value, width, int_width, is_signed=is_signed).unsigned_integer(),
numeric_types.FixedPoint(
value, width, int_width, is_signed=is_signed
).unsigned_integer(),
)


Expand Down Expand Up @@ -565,12 +571,16 @@ def generate_fp_pow_full(
const_one = comp.const(
"one",
width,
FixedPoint("1.0", width, int_width, is_signed=is_signed).unsigned_integer(),
numeric_types.FixedPoint(
"1.0", width, int_width, is_signed=is_signed
).unsigned_integer(),
)
const_zero = comp.const(
"zero",
width,
FixedPoint("0.0", width, int_width, is_signed=is_signed).unsigned_integer(),
numeric_types.FixedPoint(
"0.0", width, int_width, is_signed=is_signed
).unsigned_integer(),
)
mult = comp.cell(
"mult",
Expand All @@ -587,7 +597,7 @@ def generate_fp_pow_full(
const_neg_one = comp.const(
"neg_one",
width,
FixedPoint(
numeric_types.FixedPoint(
"-1.0", width, int_width, is_signed=is_signed
).unsigned_integer(),
)
Expand Down
2 changes: 1 addition & 1 deletion calyx-py/calyx/gen_ln.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Import,
)
from calyx.utils import float_to_fixed_point
from calyx import numeric_types
from fud.stages.verilator import numeric_types
from calyx.gen_msb import gen_msb_calc

from calyx.builder import Builder, ComponentBuilder, CellBuilder, HI, par, invoke
Expand Down
15 changes: 15 additions & 0 deletions calyx-py/calyx/gen_msb.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
from typing import List
from calyx.py_ast import (
Connect,
CompVar,
Cell,
Group,
ConstantPort,
CompPort,
Stdlib,
Component,
ThisPort,
HolePort,
PortDef,
SeqComp,
Enable,
While,
Control,
CombGroup,
)
from calyx.builder import (
Builder,
CellAndGroup,
ComponentBuilder,
const,
HI,
while_with,
Expand Down
2 changes: 1 addition & 1 deletion frontends/relay/relay_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Component,
)
from calyx.utils import float_to_fixed_point
from calyx import numeric_types
from fud.stages.verilator import numeric_types
from dahlia_impl import emit_components

calyx_keywords_list = ["input"]
Expand Down
2 changes: 1 addition & 1 deletion frontends/systolic-lang/convert-mems.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse
import json
from calyx import numeric_types
from fud.stages.verilator import numeric_types


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion frontends/systolic-lang/gen_array_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
from gen_pe import pe, PE_NAME, BITWIDTH
from calyx import builder as cb
import calyx.builder as cb
from calyx import py_ast

# Global constant for the current bitwidth.
Expand Down
4 changes: 2 additions & 2 deletions frontends/systolic-lang/gen_post_op.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python3

from calyx import builder as cb
import calyx.builder as cb
from calyx import py_ast
from gen_array_component import NAME_SCHEME
from gen_pe import BITWIDTH, INTWIDTH, FRACWIDTH
from calyx import numeric_types
from fud.stages.verilator import numeric_types
from calyx.utils import float_to_fixed_point


Expand Down
10 changes: 10 additions & 0 deletions fud/fud/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,16 @@ def __init__(self, path):
super().__init__(msg)


class InvalidNumericType(FudError):
"""
An error raised when an invalid numeric type is provided.
"""

def __init__(self, msg):
msg = f"""Invalid Numeric Type: {msg}"""
super().__init__(msg)


class Malformed(FudError):
"""
An error raised when the input to a stage is malformed in some manner.
Expand Down
20 changes: 11 additions & 9 deletions fud/fud/stages/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from pathlib import Path
import simplejson as sjson
import numpy as np
from calyx.numeric_types import FixedPoint, Bitnum, InvalidNumericType
from fud.stages.verilator.numeric_types import FixedPoint, Bitnum
from fud.errors import InvalidNumericType
from fud.stages.verilator.json_to_dat import parse_fp_widths, float_to_fixed
from fud.utils import shell, TmpDir, unwrap_or, transparent_shell
from fud import config as cfg
Expand Down Expand Up @@ -87,9 +88,8 @@ def _is_data_converter(self):

def _define_steps(self, input_data, builder, config):
script = config["stages", self.name, "exec"]
data_path_exists: bool = config["stages", "verilog", "data"] or config.get(
["stages", "mrxl", "data"]
)
data_path_exists: bool = (config["stages", "verilog", "data"] or
config.get(["stages", "mrxl", "data"]))

cmd = [
script,
Expand Down Expand Up @@ -118,9 +118,7 @@ def mktmp() -> SourceType.Directory:
"""
return TmpDir()

@builder.step(
description="Dynamically retrieve the value of stages.verilog.data"
)
@builder.step(description="Dynamically retrieve the value of stages.verilog.data")
def get_verilog_data() -> SourceType.Path:
data_path = config.get(["stages", "verilog", "data"])
path = Path(data_path) if data_path else None
Expand Down Expand Up @@ -199,7 +197,9 @@ def parse_output(
data_path = get_verilog_data()

if data_path_exists:
convert_json_to_interp_json(tmpdir, data_path)
convert_json_to_interp_json(
tmpdir, data_path
)

if self._is_data_converter():
if data_path_exists:
Expand All @@ -213,7 +213,9 @@ def parse_output(
result = interpret(input_data, tmpdir)

if "--raw" in cmd:
return parse_output(result, data_path, tmpdir)
return parse_output(
result, data_path, tmpdir
)
else:
return result

Expand Down
4 changes: 2 additions & 2 deletions fud/fud/stages/verilator/json_to_dat.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import simplejson as sjson
import numpy as np
from calyx.numeric_types import FixedPoint, Bitnum, InvalidNumericType
from .numeric_types import FixedPoint, Bitnum
from pathlib import Path
from fud.errors import Malformed
from fud.errors import InvalidNumericType, Malformed
import logging as log


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@
from fractions import Fraction
from dataclasses import dataclass
from decimal import Decimal, getcontext
from fud.errors import InvalidNumericType
import math
import logging as log


class InvalidNumericType(Exception):
"""
An error raised when an invalid numeric type is provided.
"""

def __init__(self, msg):
msg = f"""Invalid Numeric Type: {msg}"""
super().__init__(msg)


@dataclass
class NumericType:
"""Interface for a numeric type.
Expand Down
2 changes: 1 addition & 1 deletion fud/fud/stages/verilator/tables.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from itertools import product
from decimal import Decimal
from calyx.numeric_types import FixedPoint
from fud.stages.verilator.numeric_types import FixedPoint


def compute_exp_frac_table(frac_width: int):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from random import randint
from calyx.numeric_types import FixedPoint, Bitnum, InvalidNumericType
from fud.stages.verilator.numeric_types import FixedPoint, Bitnum
from fud.errors import InvalidNumericType
from hypothesis import given, strategies as st # type: ignore
import numpy as np
import pytest # type: ignore
Expand Down
25 changes: 11 additions & 14 deletions fud/fud/xclrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from typing import Mapping, Any, Dict
from pathlib import Path
from fud.stages.verilator.json_to_dat import parse_fp_widths, float_to_fixed
from calyx.numeric_types import InvalidNumericType
from fud.errors import InvalidNumericType


def mem_to_buf(mem):
Expand Down Expand Up @@ -96,9 +96,8 @@ def run(xclbin: Path, data: Mapping[str, Any]) -> Dict[str, Any]:
# Collect the output data.
for buf in buffers:
buf.sync_from_device()
mems = {
name: buf_to_mem(data[name]["format"], buf) for name, buf in zip(data, buffers)
}
mems = {name: buf_to_mem(data[name]["format"], buf)
for name, buf in zip(data, buffers)}

# PYNQ recommends explicitly freeing its resources.
del buffers
Expand All @@ -119,16 +118,14 @@ def _dtype(fmt) -> np.dtype:
def xclrun():
# Parse command-line arguments.
parser = argparse.ArgumentParser(
description="run a compiled XRT program",
)
parser.add_argument("bin", metavar="XCLBIN", help="the .xclbin binary file to run")
parser.add_argument("data", metavar="DATA", help="the JSON input data file")
parser.add_argument(
"--out",
"-o",
metavar="FILE",
help="write JSON results to a file instead of stdout",
description='run a compiled XRT program',
)
parser.add_argument('bin', metavar='XCLBIN',
help='the .xclbin binary file to run')
parser.add_argument('data', metavar='DATA',
help='the JSON input data file')
parser.add_argument('--out', '-o', metavar='FILE',
help='write JSON results to a file instead of stdout')
args = parser.parse_args()

# Load the input JSON data file.
Expand All @@ -139,7 +136,7 @@ def xclrun():
out_data = run(Path(args.bin), in_data)

# Dump the output JSON data.
outfile = open(args.out, "w") if args.out else sys.stdout
outfile = open(args.out, 'w') if args.out else sys.stdout
sjson.dump(out_data, outfile, indent=2, use_decimal=True)


Expand Down

0 comments on commit 26a11b8

Please sign in to comment.