快捷方式

torch.compiler.set_stance

torch.compiler.set_stance(stance='default', *, skip_guard_eval_unsafe=False, force_backend=None)[源代码][源代码]

设置编译器的当前姿态。可以用作函数、上下文管理器或装饰器。请勿在 torch.compile 区域内使用此函数 - 否则会引发错误。

@torch.compile
def foo(x):
    ...

@torch.compiler.set_stance("force_eager")
def bar():
    # will not be compiled
    foo(...)

bar()

with torch.compiler.set_stance("force_eager"):
    # will also not be compiled
    foo(...)

torch.compiler.set_stance("force_eager")
# will also not be compiled
foo(...)
torch.compiler.set_stance("default")

# will be compiled
foo(...)
参数
  • stance (str) –

    要将编译器设置为的姿态。有效值包括

    • “default”: 默认姿态,用于正常编译。

    • “force_eager”: 忽略所有 torch.compile 指令。

    • “eager_on_recompile”: 当需要重新编译时,以 eager 模式运行代码。如果存在对输入有效的缓存编译代码,仍将使用它。

    • “fail_on_recompile”: 重新编译函数时引发错误。

  • skip_guard_eval_unsafe

    一个仅运行区分性 guard 的标志。注意 - 此标志是不安全的,仅当您的设置满足以下条件时才应使用。

    torch.compile 使用 guard 系统来支持重新编译,并选择在运行时运行哪个编译后的工件。这些 guard 虽然高效,但会增加一些开销,这可能会影响您需要优化以获得最小 guard 处理时间的场景中的性能。此 API 使您能够禁用 guard 评估,假设您已使用足够多的输入预热了编译后的模型。此假设意味着,在预热阶段之后,将不再需要进一步的重新编译。如果此假设失败,则存在静默产生不正确结果的风险(因此 API 名称中使用了“unsafe”一词)。

  • force_backend – 如果 stance 是 “default”,则可以使用此参数强制 torch.compile 使用特定的后端。否则,将引发错误。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得您的问题解答

查看资源