Skip to content

Commit

Permalink
Add parameter to set number of bits in fractional
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei-grovety committed Jan 16, 2024
1 parent 6cf49b6 commit cb2faa8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
7 changes: 7 additions & 0 deletions python/tvm/relay/backend/contrib/ethosu/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ def is_fixed_point_enabled() -> bool:
return bool(compiler_attrs.enable_fixed_point)


def get_fixed_point_fraction_size() -> int:
"""Function to get the number of bits used to
represent the fractional part of a fixed point number"""
compiler_attrs = tvm.get_global_func("relay.ext.ethos-u.get_compiler_attrs")()
return int(compiler_attrs.fixed_point_fraction_size)


def is_striping_enabled() -> bool:
"""Determine whether the cascader is enabled"""
compiler_attrs = tvm.get_global_func("relay.ext.ethos-u.get_compiler_attrs")()
Expand Down
6 changes: 4 additions & 2 deletions python/tvm/relay/op/contrib/ethosu.py
Original file line number Diff line number Diff line change
Expand Up @@ -1811,10 +1811,12 @@ def __init__(self, func_body):
from tvm.relay.backend.contrib.ethosu.util import QDenseArgs # type: ignore
from tvm.relay.backend.contrib.ethosu.util import BiasAddArgs, RequantArgs

fract_scale = tvm.relay.Constant(tvm.nd.array(np.array(1 / 2**15)))
fract_zero_point = tvm.relay.Constant(tvm.nd.array(np.array(0, dtype="int32")))
self.activation = None
if util.is_fixed_point_enabled():
fract_scale = tvm.relay.Constant(
tvm.nd.array(np.array(1 / 2 ** util.get_fixed_point_fraction_size()))
)
fract_zero_point = tvm.relay.Constant(tvm.nd.array(np.array(0, dtype="int32")))
qnn_dense = func_body
bias_add = None
else:
Expand Down
6 changes: 6 additions & 0 deletions src/relay/backend/contrib/ethosu/compiler_attrs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct EthosUCompilerConfigNode : public tvm::AttrsNode<EthosUCompilerConfigNode
Bool enable_striping = Bool(false);
Bool disable_copying_constants = Bool(false);
Bool enable_fixed_point = Bool(false);
Integer fixed_point_fraction_size = Integer(0);
String dev_force_block_config;
String dev_max_open_plans;
String dev_max_closed_plans;
Expand Down Expand Up @@ -80,6 +81,11 @@ struct EthosUCompilerConfigNode : public tvm::AttrsNode<EthosUCompilerConfigNode
"enabled, it is assumed that input data should be converted to fixed point "
"representation")
.set_default(Bool(false));
TVM_ATTR_FIELD(fixed_point_fraction_size)
.describe(
"Fraction size refers to the number of bits used to represent the fractional part of a "
"fixed point number")
.set_default(Integer(0));
TVM_ATTR_FIELD(dev_force_block_config)
.describe((dev_warning + String("Force the block config to a given value; format = "
"\"[BLK_HEIGHT]x[BLK_WIDTH]x[BLK_DEPTH]\""))
Expand Down
4 changes: 4 additions & 0 deletions tests/python/contrib/test_ethosu/infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def create_test_runner(
enable_striping=False,
workspace_pools=None,
enable_fixed_point=False,
fixed_point_fraction_size=0,
):

file_dir = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -170,6 +171,7 @@ def create_test_runner(
"enable_cascader": enable_cascader,
"enable_striping": enable_striping,
"enable_fixed_point": enable_fixed_point,
"fixed_point_fraction_size": fixed_point_fraction_size,
},
"tir.usmp.enable": enable_usmp,
"tir.usmp.algorithm": "hill_climb",
Expand Down Expand Up @@ -336,6 +338,7 @@ def compare_ethosu_with_reference(
print_cmm=False,
enable_cascader=None,
enable_fixed_point=False,
fixed_point_fraction_size=0,
):
if enable_cascader is None:
enable_cascader = "u65" not in accel_type
Expand Down Expand Up @@ -363,6 +366,7 @@ def compare_ethosu_with_reference(
enable_striping=False,
workspace_pools=workspace_pools,
enable_fixed_point=enable_fixed_point,
fixed_point_fraction_size=fixed_point_fraction_size,
)
compiled_models = build_source(
mod,
Expand Down
5 changes: 3 additions & 2 deletions tests/python/contrib/test_ethosu/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ def subtract_sigmoid_function(lhs, rhs):
@pytest.mark.parametrize("accel_type", ["ethos-u55-256", "ethos-u65-256"])
@pytest.mark.parametrize(
"ifm_shape,ofm_channels,fract_size,tolerance",
[[(1, 16), 8, 15, 0.001], [(2, 8), 16, 14, 0.3]],
[[(1, 16), 8, 15, 0.001], [(2, 8), 16, 14, 0.001], [(4, 8), 16, 12, 0.001]],
)
def test_ethosu_matmul_fixed_point(accel_type, ifm_shape, ofm_channels, fract_size, tolerance):
np.random.seed(0)
Expand Down Expand Up @@ -1661,7 +1661,7 @@ def convert_to_fixed_point(arr, fract_size):
output_data = {"output": convert_to_fixed_point(output_data, fract_size)}
tolerance = convert_to_fixed_point(tolerance, fract_size)

config = {"enable_fixed_point": True}
config = {"enable_fixed_point": True, "fixed_point_fraction_size": fract_size}
with tvm.transform.PassContext(config={"relay.ext.ethos-u.options": config}):
ethosu_mod = partition_for_ethosu(cpu_mod)

Expand All @@ -1673,6 +1673,7 @@ def convert_to_fixed_point(arr, fract_size):
enable_cascader=False,
output_tolerance=tolerance,
enable_fixed_point=True,
fixed_point_fraction_size=fract_size,
)


Expand Down

0 comments on commit cb2faa8

Please sign in to comment.