Skip to content
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

[TVMC][TRANSFORMS] ToMixedPrecision transform support with custom options enabled #14010

Merged
merged 5 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 38 additions & 29 deletions python/tvm/driver/tvmc/autotuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=unused-argument
"""
Provides support to auto-tuning networks using AutoTVM.
"""
Expand All @@ -39,7 +40,7 @@
from .model import TVMCModel
from .target import target_from_cli, generate_target_args, reconstruct_target_args
from .shape_parser import parse_shape_string
from .transform import convert_graph_layout
from .transform import generate_transform_args, parse_graph_transform_args, apply_graph_transforms


# pylint: disable=invalid-name
Expand Down Expand Up @@ -127,12 +128,7 @@ def add_tune_parser(subparsers, _, json_params):
metavar="PATH",
help="path to an auto-tuning log file by AutoTVM.",
)
parser.add_argument(
"--desired-layout",
choices=["NCHW", "NHWC"],
default=None,
help="change the data layout of the whole graph",
)
generate_transform_args(parser)
parser.add_argument(
"--enable-autoscheduler",
help="enable tuning the graph through the AutoScheduler tuner",
Expand Down Expand Up @@ -269,6 +265,8 @@ def drive_tune(args):
rpc_hostname = None
rpc_port = None

transform_args = parse_graph_transform_args(args)

tune_model(
tvmc_model,
args.target,
Expand All @@ -283,7 +281,6 @@ def drive_tune(args):
tuner=args.tuner,
min_repeat_ms=args.min_repeat_ms,
early_stopping=args.early_stopping,
desired_layout=args.desired_layout,
timeout=args.timeout,
repeat=args.repeat,
number=args.number,
Expand All @@ -292,6 +289,7 @@ def drive_tune(args):
include_simple_tasks=args.include_simple_tasks,
log_estimated_latency=args.log_estimated_latency,
additional_target_options=reconstruct_target_args(args),
**transform_args,
)


Expand All @@ -309,7 +307,6 @@ def tune_model(
tuner: str = "xgb",
min_repeat_ms: Optional[int] = None,
early_stopping: Optional[int] = None,
desired_layout: Optional[str] = None,
timeout: int = 10,
repeat: int = 1,
number: int = 10,
Expand All @@ -318,6 +315,12 @@ def tune_model(
include_simple_tasks: bool = False,
log_estimated_latency: bool = False,
additional_target_options: Optional[Dict[str, Dict[str, Any]]] = None,
desired_layout: Optional[str] = None,
desired_layout_ops: Optional[List[str]] = None,
mixed_precision: bool = False,
mixed_precision_ops: Optional[List[str]] = None,
mixed_precision_calculation_type: Optional[str] = None,
mixed_precision_acc_type: Optional[str] = None,
):
"""Use tuning to automatically optimize the functions in a model.

Expand Down Expand Up @@ -354,10 +357,6 @@ def tune_model(
Minimum time to run each trial. Defaults to 0 on x86 and 1000 on other targets.
early_stopping : int, optional
When specified, stop tuning after this number of trials if results aren't improving.
desired_layout : str, optional
srkreddy1238 marked this conversation as resolved.
Show resolved Hide resolved
Can be one of "NCHW" or "NHWC". When specified, compatible operations in the graph
will have their layout set to this format. Tasks will then be tuned using this
specified layout.
timeout : int, optional,
If a kernel trial lasts longer than this duration in seconds, it will be
considered a failure.
Expand All @@ -376,12 +375,28 @@ def tune_model(
If using the autoscheduler, write the estimated latency at each step of tuning to file.
additional_target_options: Optional[Dict[str, Dict[str, Any]]]
Additional target options in a dictionary to combine with initial Target arguments
desired_layout: str, optional
Can be one of "NCHW" or "NHWC". When specified, compatible operations in the graph
will have their layout set to this format. Tasks will then be tuned using this
specified layout.
desired_layout_ops: list[str], optional
srkreddy1238 marked this conversation as resolved.
Show resolved Hide resolved
The list of operators to be transformed with desired layout.
mixed_precision: bool
To enable mixed precision transformation.
mixed_precision_ops: list[str], optional
The list of operators to be converted to mixed precision.
mixed_precision_calculation_type: str
The calculation dtype to be used while mixed precision.
mixed_precision_acc_type: str
The accumulation data type to be used while mixed precision.


Returns
-------
tuning_records : str
The path to the produced tuning log file.
"""
transform_args = parse_graph_transform_args(locals())
target, extra_targets = target_from_cli(target, additional_target_options)
target, target_host = Target.canon_target_and_host(target, target_host)
# TODO(jwfromm) Remove this deepcopy once AlterOpLayout bug that mutates source
Expand Down Expand Up @@ -453,7 +468,7 @@ def tune_model(
mod=mod,
params=params,
target=target,
alter_layout=desired_layout,
transform_args=transform_args,
hardware_params=hardware_params,
include_simple_tasks=include_simple_tasks,
)
Expand All @@ -475,7 +490,7 @@ def tune_model(
mod=mod,
params=params,
target=target,
alter_layout=desired_layout,
transform_args=transform_args,
)

# In autotvm, trials is specified per task. We can convert the per-model input
Expand Down Expand Up @@ -504,7 +519,7 @@ def autotvm_get_tuning_tasks(
params: Dict[str, tvm.nd.NDArray],
target: str,
target_host: Optional[str] = None,
alter_layout: Optional[str] = None,
transform_args: Optional[Dict[str, Any]] = None,
):
"""Get the autotvm tuning tasks for a given relay module.

Expand All @@ -518,10 +533,8 @@ def autotvm_get_tuning_tasks(
The compilation target.
target_host : str, optional
The compilation target for the host.
alter_layout : str, optional
The layout to convert the graph to. Note, the convert layout
pass doesn't currently guarantee the whole of the graph will
be converted to the chosen layout.
transform_args: dict, optional
Graph transformation arguments that are applied to the relay module.

Returns
-------
Expand All @@ -530,8 +543,7 @@ def autotvm_get_tuning_tasks(
"""
target, target_host = Target.canon_target_and_host(target, target_host)

if alter_layout:
mod = convert_graph_layout(mod, alter_layout)
mod = apply_graph_transforms(mod, transform_args)

tasks = autotvm.task.extract_from_program(
mod["main"],
Expand All @@ -547,7 +559,7 @@ def autoscheduler_get_tuning_tasks(
params: Dict[str, tvm.nd.NDArray],
target: str,
target_host: Optional[str] = None,
alter_layout: Optional[str] = None,
transform_args: Optional[Dict[str, Any]] = None,
hardware_params: Optional[HardwareParams] = None,
include_simple_tasks: bool = False,
):
Expand All @@ -563,10 +575,8 @@ def autoscheduler_get_tuning_tasks(
The compilation target.
target_host : str, optional
The compilation target for the host.
alter_layout : str, optional
The layout to convert the graph to. Note, the convert layout
pass doesn't currently guarantee the whole of the graph will
be converted to the chosen layout.
transform_args: dict, optional
Graph transformation arguments that are applied to the relay module.
hardware_params : Optional[HardwareParams]
Hardware parameters used for the search tasks

Expand All @@ -579,8 +589,7 @@ def autoscheduler_get_tuning_tasks(
"""
target, target_host = Target.canon_target_and_host(target, target_host)

if alter_layout:
mod = convert_graph_layout(mod, alter_layout)
mod = apply_graph_transforms(mod, transform_args)

# Extract the tasks
tasks, task_weights = auto_scheduler.extract_tasks(
Expand Down
43 changes: 28 additions & 15 deletions python/tvm/driver/tvmc/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=unused-argument
"""
Provides support to compile networks both AOT and JIT.
"""
Expand All @@ -37,7 +38,7 @@
from .target import target_from_cli, generate_target_args, reconstruct_target_args
from .pass_config import parse_configs
from .pass_list import parse_pass_list_str
from .transform import convert_graph_layout
from .transform import generate_transform_args, parse_graph_transform_args, apply_graph_transforms
from .shape_parser import parse_shape_string
from .workspace_pools import generate_workspace_pools_args, workspace_pools_recombobulate

Expand All @@ -61,12 +62,7 @@ def add_compile_parser(subparsers, _, json_params):
default="",
help="the cross compiler options to generate target libraries, e.g. '-mfpu=neon-vfpv4'.",
)
parser.add_argument(
"--desired-layout",
choices=["NCHW", "NHWC"],
default=None,
help="change the data layout of the whole graph.",
)
generate_transform_args(parser)
parser.add_argument(
"--dump-code",
metavar="FORMAT",
Expand Down Expand Up @@ -177,6 +173,7 @@ def drive_compile(args):

additional_targets = reconstruct_target_args(args)
workspace_pools_target, extra_targets = target_from_cli(args.target, additional_targets)
transform_args = parse_graph_transform_args(args)

compile_model(
tvmc_model,
Expand All @@ -191,14 +188,14 @@ def drive_compile(args):
output_format=args.output_format,
dump_code=dump_code,
target_host=None,
desired_layout=args.desired_layout,
disabled_pass=args.disabled_pass,
pass_context_configs=args.pass_config,
mod_name=args.module_name,
additional_target_options=additional_targets,
workspace_pools=(
workspace_pools_recombobulate(args, [workspace_pools_target], extra_targets)
),
**transform_args,
)

return 0
Expand All @@ -217,14 +214,19 @@ def compile_model(
output_format: str = "so",
dump_code: Optional[List[str]] = None,
target_host: Optional[str] = None,
desired_layout: Optional[str] = None,
disabled_pass: Optional[str] = None,
pass_context_configs: Optional[List[str]] = None,
additional_target_options: Optional[Dict[str, Dict[str, Any]]] = None,
use_vm: bool = False,
mod_name: Optional[str] = "default",
workspace_pools: Optional[WorkspaceMemoryPools] = None,
instruments: Optional[Sequence[PassInstrument]] = None,
desired_layout: Optional[str] = None,
desired_layout_ops: Optional[List[str]] = None,
mixed_precision: bool = False,
mixed_precision_ops: Optional[List[str]] = None,
mixed_precision_calculation_type: Optional[str] = None,
mixed_precision_acc_type: Optional[str] = None,
):
"""Compile a model from a supported framework into a TVM module.

Expand Down Expand Up @@ -260,10 +262,6 @@ def compile_model(
target_host : str, optional
The target of the host machine if host-side code
needs to be generated.
desired_layout: str, optional
srkreddy1238 marked this conversation as resolved.
Show resolved Hide resolved
The layout to convert the graph to. Note, the convert layout
pass doesn't currently guarantee the whole of the graph will
be converted to the chosen layout.
disabled_pass: str, optional
Comma-separated list of passes which needs to be disabled
during compilation
Expand All @@ -281,6 +279,21 @@ def compile_model(
compilation.
instruments: Optional[Sequence[PassInstrument]]
The list of pass instrument implementations.
desired_layout: str, optional
Can be one of "NCHW" or "NHWC". When specified, compatible operations in the graph
will have their layout set to this format. Tasks will then be tuned using this
specified layout.
desired_layout_ops: list[str], optional
The list of operators to be transformed with desired layout.
mixed_precision: bool
To enable mixed precision transformation. Disabled by default.
mixed_precision_ops: list[str], optional
The list of operators to be converted to mixed precision.
Set to ["nn.conv2d", "nn.dense"] by default
mixed_precision_calculation_type: str
The calculation dtype to be used while mixed precision. Set to "float16" by default.
mixed_precision_acc_type: str
The accumulation data type to be used while mixed precision. Set to "float16" by default.

Returns
-------
Expand Down Expand Up @@ -310,8 +323,8 @@ def compile_model(
disabled_pass=disabled_pass,
instruments=instruments,
):
if desired_layout:
mod = convert_graph_layout(mod, desired_layout)
transform_args = parse_graph_transform_args(locals())
mod = apply_graph_transforms(mod, transform_args)

for partition_function, opts in zip(partition_functions, partition_opts):
mod = partition_function(mod, params, mod_name=mod_name, **opts)
Expand Down
Loading