Skip to content

推論をキャンセル可能にする #687

@qryxip

Description

@qryxip

内容

asyncなタスクに一般的に期待される機能の一つとして、キャンセル可能であるというのがあると思います。しかし現状の実装では、ONNX Runtimeの推論はasyncの文脈でキャンセルすることができません。何故ならスレッドは外から停止できないからです。ここでONNX RuntimeのRunOptions::SetTerminateによって、推論のキャンセルを可能することを提案します。

terminateというプロパティですが、次のように説明されています。

If a currently executing session needs to be force terminated, this can be called from another thread to force it to fail with an error.

(C API)

Set to True to terminate any currently executing calls that are using this RunOptions instance. The individual calls will exit gracefully and return an error status.

(Python API)

意味を掴みかねていたのですが、コードを見るとbool terminateconst bool&で引き回してそれをチェックし続けていました。試してはいないのですが、あるRunOptionsで推論を開始したあと、そのRunOptionsterminatetrueにセットすると、"Exiting due to terminate flag being set to true."というメッセージと共に推論が止まってくれるということだと予想しています。

https://github.com/microsoft/onnxruntime/blob/v1.16.2/onnxruntime/core/framework/stream_execution_context.cc#L211-L216

APIとしては次のような形がよいのかなと思っています。

  • 同期/非同期APIにおいて、Synthesizerの各操作はclass SynthesisTaskを返すようにする。SynthesisTask.wait()で続行、.cancel()でキャンセルする。
  • 同期/非同期APIにおいて、Synthesizerの各メソッドに推論キャンセルのためのオプションを一つ追加する。例えば#[repr(C)] struct VoicevoxInferenceCanceller
    (追記) 直にboolでもいけるかもしれない
  • 非同期APIにおいては、さらにasync文脈でのキャンセルでも推論をキャンセルするようにする。

Pros 良くなる点

  • asyncなAPIとして完成に近づく
  • 推論の中止ができる

Cons 悪くなる点

実現方法

VOICEVOXのバージョン

N/A

OSの種類/ディストリ/バージョン

  • Windows
  • macOS
  • Linux

その他

TFLiteにもキャンセル機構があるようです。

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions