2022-11-08 05:59:20 +00:00
|
|
|
from typing import Any, Callable, Dict, Optional, Union
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
from colossalai.fx import ColoGraphModule
|
|
|
|
from colossalai.fx._compatibility import compatibility
|
|
|
|
|
|
|
|
from .tracer import ColoTracer
|
|
|
|
|
|
|
|
|
|
|
|
@compatibility(is_backward_compatible=True)
|
|
|
|
def symbolic_trace(
|
|
|
|
root: Union[torch.nn.Module, Callable[..., Any]],
|
|
|
|
concrete_args: Optional[Dict[str, Any]] = None,
|
|
|
|
meta_args: Optional[Dict[str, Any]] = None,
|
2023-01-11 05:49:59 +00:00
|
|
|
trace_act_ckpt=False,
|
2022-11-08 05:59:20 +00:00
|
|
|
) -> ColoGraphModule:
|
|
|
|
"""
|
|
|
|
Symbolic tracing API
|
|
|
|
|
|
|
|
Given an ``nn.Module`` or function instance ``root``, this function will return a ``ColoGraphModule``
|
|
|
|
constructed by recording operations seen while tracing through ``root``.
|
|
|
|
|
2022-11-23 02:55:46 +00:00
|
|
|
With ``meta_args``, we can trace the model that are untraceable subject to control flow. If specified using
|
|
|
|
``meta_args`` only, the tracing can be done ahead of time.
|
2022-11-08 05:59:20 +00:00
|
|
|
|
2022-11-23 02:55:46 +00:00
|
|
|
Note that ``meta_args`` are kwargs, which contains the key of the argument's names and the value of the
|
|
|
|
argument's values.
|
2022-11-08 05:59:20 +00:00
|
|
|
|
|
|
|
Uses:
|
|
|
|
>>> model = ...
|
|
|
|
|
|
|
|
# if this works
|
2022-11-23 02:55:46 +00:00
|
|
|
>>> gm = symbolic_trace(model, concrete_args=concrete_args)
|
2022-11-08 05:59:20 +00:00
|
|
|
|
|
|
|
# else try this
|
2022-11-23 02:55:46 +00:00
|
|
|
>>> gm = symbolic_trace(model, concrete_args=concrete_args, meta_args={'x': torch.rand(1, 3, 224, 224, device='meta')})
|
2022-11-08 05:59:20 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
root (Union[torch.nn.Module, Callable[..., Any]]): Module or function to be traced and converted
|
|
|
|
into a Graph representation.
|
2022-11-23 02:55:46 +00:00
|
|
|
concrete_args (Optional[Dict[str, Any]], optional): Concrete arguments to be used for tracing.
|
2022-11-08 05:59:20 +00:00
|
|
|
meta_args (Optional[Dict[str, Any]], optional): Inputs to be partially specialized, special for ``ColoTracer``.
|
|
|
|
Defaults to None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
ColoGraphModule: A ``ColoGraphModule`` created from the recorded operations from ``root``.
|
|
|
|
|
|
|
|
Warnings:
|
|
|
|
This API is still under development and can incur some bugs. Feel free to report any bugs to the Colossal-AI team.
|
|
|
|
|
|
|
|
"""
|
2023-01-11 05:49:59 +00:00
|
|
|
graph = ColoTracer(trace_act_ckpt=trace_act_ckpt).trace(root, concrete_args=concrete_args, meta_args=meta_args)
|
2022-11-23 02:55:46 +00:00
|
|
|
name = root.__class__.__name__ if isinstance(root, torch.nn.Module) else root.__name__
|
|
|
|
return ColoGraphModule(root, graph, name)
|