ONNX Runtime generate() C# API

注意:此 API 處於預覽階段,可能會發生變化。

概述

Model 類

建構函式

public Model(string modelPath)

Generate 方法

public Sequences Generate(GeneratorParams generatorParams)

Tokenizer 類

建構函式

public Tokenizer(Model model)

Encode 方法

public Sequences Encode(string str)

Encode batch 方法

public Sequences EncodeBatch(string[] strings)

Decode 方法

public string Decode(ReadOnlySpan<int> sequence)

Decode batch 方法

public string[] DecodeBatch(Sequences sequences)

Create stream 方法

public TokenizerStream CreateStream()

TokenizerStream 類

Decode 方法

public string Decode(int token)

GeneratorParams 類

建構函式

public GeneratorParams(Model model)

設定搜尋選項(double)

public void SetSearchOption(string searchOption, double value)

設定搜尋選項(bool)方法

public void SetSearchOption(string searchOption, bool value)

嘗試以最大批處理大小進行圖捕獲

 public void TryGraphCaptureWithMaxBatchSize(int maxBatchSize)

設定輸入 ID 方法

public void SetInputIDs(ReadOnlySpan<int> inputIDs, ulong sequenceLength, ulong batchSize)

設定輸入序列方法

public void SetInputSequences(Sequences sequences)

設定模型輸入

public void SetModelInput(string name, Tensor value)

Generator 類

建構函式

public Generator(Model model, GeneratorParams generatorParams)

Is done 方法

public bool IsDone()

計算 logits

public void ComputeLogits()

生成下一個 token 方法

public void GenerateNextToken()

獲取序列

public ReadOnlySpan<int> GetSequence(ulong index)

設定活動介面卡

在此 Generator 例項上設定活動介面卡。

using var model = new Model(modelPath);
using var genParams = new GeneratorParams(model);
using var generator = new Generator(model, genParams);
using var adapters = new Adapters(model);
string adapterName = "..."

generator.SetActiveAdapter(adapters, adapterName);

引數

  • adapters:先前建立的 Adapter 物件
  • adapterName:要啟用的介面卡名稱

返回值

void

異常

出錯時丟擲異常。

Sequences 類

Num sequences 成員

public ulong NumSequences { get { return _numSequences; } }

[] 運算子

public ReadOnlySpan<int> this[ulong sequenceIndex]

Adapter 類

此 API 用於載入和切換微調適配器,例如 LoRA 介面卡。

建構函式

構造 Adapter 類例項。

using var model = new Model(modelPath);

using var adapters = new Adapters(model);

引數

  • model:先前構造的模型類

Load Adapter 方法

從磁碟載入介面卡檔案。

string adapterPath = Path()
string adapterName = ...

adapters.LoadAdapter(adapterPath, adapterName);

引數

  • adapterPath:介面卡檔案在磁碟上的路徑
  • adapterName:一個字串識別符號,用於在後續方法中引用介面卡

返回值

void

Unload Adapter 方法

從記憶體中解除安裝介面卡檔案。

adapters.UnLoadAdapter(adapterName);

引數

  • adapterName:要解除安裝的介面卡名稱

返回值

void

異常

出錯時丟擲異常。