torch.backends.mkldnn 的源代码
# mypy: 允许未类型化定义
导入
系统
来自 contextlib
导入 contextmanager
来自
打字
导入
类型检查
导入
火炬
来自
PyTorch 后端
导入
__允许非括号突变, ContextProp,
模块属性
[文档]def is_available():
r"""返回 PyTorch 是否构建了 MKL-DNN 支持。"""
return torch._C._has_mkldnn
VERBOSE_OFF = 0
VERBOSE_ON = 1
VERBOSE_ON_CREATION = 2
[文档]class verbose:
"""
按需 oneDNN(前身为 MKL-DNN)动词功能。
为了更容易调试性能问题,oneDNN 可以输出详细日志
包含诸如内核大小、输入数据大小等信息的消息
执行内核时的执行持续时间。动词化功能
可以通过名为 `DNNL_VERBOSE` 的环境变量来调用。然而,
这种方法会将消息输出到所有步骤。这些是大量的
详细信息。此外,为了调查性能问题,
通常只需要取一次单次迭代的详细信息就足够了。
这项按需动词化功能使得控制范围成为可能
用于详细消息输出的。在下面的示例中,只有第二次推理会输出详细消息。
将仅对第二次推理输出的详细消息进行输出。
.. 突出显示:: python
.. 代码块 :: python
导入 torch
model(data)
with torch.backends.mkldnn.verbose(torch.backends.mkldnn.VERBOSE_ON):
模型(数据)
Args:
级别:详细级别
- ``VERBOSE_OFF``: 禁用详细输出
- ``VERBOSE_ON``: 启用详细输出
- ``VERBOSE_ON_CREATION``: 启用详细输出,包括 oneDNN 内核创建
"""
def __init__(self, level):
self.level = level
def __enter__(self):
if self.level == VERBOSE_OFF:
return
st = torch._C._verbose.mkldnn_set_verbose(self.level)
assert (
st
), "无法将 MKLDNN 设置为详细模式。请考虑禁用此详细范围。"
返回自身
def __exit__(self, exc_type, exc_val, exc_tb):
torch._C._verbose.mkldnn_set_verbose(VERBOSE_OFF)
返回 False
def 设置标志(
启用=
无,
确定性=
无,
允许使用 TF32=
无):
原始标志 = (
PyTorch._C._get_mkldnn_enabled(),
PyTorch._C._get_mkldnn_deterministic(),
PyTorch._C._get_onednn_allow_tf32(),
)
如果
启用
是
不
无:
PyTorch._C._set_mkldnn_enabled(
启用)
如果
_确定性
是
不
无:
PyTorch._C.
_设置 mkldnn 确定性(
确定性)
如果
允许 tf32
是
不
无:
PyTorch._C.
_设置 onednn 允许 tf32(
允许使用 TF32)
返回
原始标志
@contextmanager
def 标志(
启用=False,
确定性=False,
允许使用 TF32=True):
替换为
__允许非括号突变():
原始标志 =
设置标志(
启用,
确定性,
允许使用 TF32)
尝试:
产生
最后:
替换为
__允许非括号突变():
设置标志(*
原始标志)
类
Mkldnn 模块(
Prop 模块):
def __init__(self, m, 名称):
超级().__init__(m,
名称)
启用 = ContextProp(
PyTorch._C._get_mkldnn_enabled,
PyTorch._C._set_mkldnn_enabled)
确定性 = ContextProp(
PyTorch._C._get_mkldnn_deterministic,
PyTorch._C._set_mkldnn_deterministic
)
允许 TF32 = ContextProp(
PyTorch._C._get_onednn_allow_tf32,
PyTorch._C._set_onednn_allow_tf32
)
如果
类型检查:
启用: ContextProp
确定性: ContextProp
允许使用 TF32:
上下文属性
系统模块.
模块[__name__] =
MKLDNN 模块(
系统模块.
模块[__name__
] __name__)