Yuniko.Software.Qwen3Tokenizer
1.0.0
See the version list below for details.
dotnet add package Yuniko.Software.Qwen3Tokenizer --version 1.0.0
NuGet\Install-Package Yuniko.Software.Qwen3Tokenizer -Version 1.0.0
<PackageReference Include="Yuniko.Software.Qwen3Tokenizer" Version="1.0.0" />
<PackageVersion Include="Yuniko.Software.Qwen3Tokenizer" Version="1.0.0" />
<PackageReference Include="Yuniko.Software.Qwen3Tokenizer" />
paket add Yuniko.Software.Qwen3Tokenizer --version 1.0.0
#r "nuget: Yuniko.Software.Qwen3Tokenizer, 1.0.0"
#:package Yuniko.Software.Qwen3Tokenizer@1.0.0
#addin nuget:?package=Yuniko.Software.Qwen3Tokenizer&version=1.0.0
#tool nuget:?package=Yuniko.Software.Qwen3Tokenizer&version=1.0.0
Yuniko.Software.Qwen3Tokenizer
Native .NET tokenizer implementation for Qwen3 models. Lightweight byte-pair encoding with HuggingFace integration.
Features
- Native .NET Implementation: Qwen3 tokenizer built specifically for .NET applications
- Identical to HuggingFace: Produces the same token IDs and outputs as the official HuggingFace tokenizer
- Qwen Model Family Support: Compatible with all Qwen3 variants (LLM, Embedding, Reranker, and Vision-Language models)
- HuggingFace Integration: Load tokenizer files directly from HuggingFace model repositories
- Configurable: Customize tokenization behavior through options and custom file providers
- No External Dependencies: Does not require Python or other runtime dependencies
Installation
dotnet add package Yuniko.Software.Qwen3Tokenizer
Or via Package Manager:
Install-Package Yuniko.Software.Qwen3Tokenizer
Quick Start
using Yuniko.Software.Qwen3Tokenizer;
// Load from HuggingFace model
var tokenizer = await Qwen3Tokenizer.FromHuggingFaceAsync("Qwen/Qwen3-0.6B");
// Encode text
int[] tokenIds = tokenizer.Encode("Hello, world!");
Console.WriteLine($"Token IDs: [{string.Join(", ", tokenIds)}]");
Console.WriteLine($"Token count: {tokenIds.Length}");
// Decode tokens
string decodedText = tokenizer.Decode(tokenIds);
Console.WriteLine($"Decoded: {decodedText}");
Usage Examples
Basic Tokenization
var tokenizer = Qwen3Tokenizer.FromHuggingFace("Qwen/Qwen3-0.6B");
// Encode text into token IDs
int[] ids = tokenizer.Encode("The quick brown fox jumps over the lazy dog");
// Decode token IDs back to text
string text = tokenizer.Decode(ids);
// Count tokens without full encoding
int tokenCount = tokenizer.CountTokens("Some text to count");
Working with Special Tokens
// Encode text with special tokens
string chatMessage = "<|im_start|>user\nHello!<|im_end|>";
int[] tokens = tokenizer.Encode(chatMessage);
// Decode with special tokens preserved
string withSpecial = tokenizer.Decode(tokens, skipSpecialTokens: false);
// Decode with special tokens removed (default behavior)
string withoutSpecial = tokenizer.Decode(tokens, skipSpecialTokens: true);
Detailed Encoding Information
// Get detailed information about tokens
var result = tokenizer.EncodeDetailed("Hello, world!");
for (int i = 0; i < result.Ids.Length; i++)
{
Console.WriteLine($"Token: '{result.Tokens[i]}' | ID: {result.Ids[i]} | " +
$"Offset: {result.Offsets[i].Index}, Length: {result.Offsets[i].Length}");
}
ONNX Runtime Integration
// Prepare inputs for ONNX Runtime inference
var inputs = tokenizer.PrepareForOnnx("Your input text here", maxLength: 512);
// Use with ONNX Runtime
// Note: Some models (e.g., embedding models) may not require position_ids
long[] inputIds = inputs.InputIds; // Token IDs with padding
long[] attentionMask = inputs.AttentionMask; // 1 for real tokens, 0 for padding
long[] positionIds = inputs.PositionIds; // Position indices
Loading from Local Files
// Load tokenizer from local vocabulary and merges files
var tokenizer = Qwen3Tokenizer.FromFiles(
vocabPath: "/path/to/vocab.json",
mergesPath: "/path/to/merges.txt"
);
Custom File Provider
// Implement custom file provider for advanced scenarios
public class CustomFileProvider : ITokenizerFileProvider
{
public (string VocabPath, string MergesPath) GetFiles()
{
// Custom logic to provide tokenizer files
return ("/path/to/vocab.json", "/path/to/merges.txt");
}
public Task<(string VocabPath, string MergesPath)> GetFilesAsync(
CancellationToken cancellationToken = default)
{
return Task.FromResult(GetFiles());
}
}
// Use custom provider
var tokenizer = Qwen3Tokenizer.FromProvider(new CustomFileProvider());
Accessing Vocabulary and Token Information
// Get vocabulary size
int vocabSize = tokenizer.VocabularySize;
// Access full vocabulary
IReadOnlyDictionary<string, int> vocab = tokenizer.Vocabulary;
// Access added tokens (special tokens and others)
IReadOnlyDictionary<string, int> addedTokens = tokenizer.AddedTokens;
// Access special token IDs
IReadOnlySet<int> specialTokenIds = tokenizer.SpecialTokenIds;
// Use predefined token constants
Console.WriteLine($"EOS token: {Qwen3Tokens.ImEnd} (ID: {Qwen3Tokens.ImEndTokenId})");
Console.WriteLine($"PAD token: {Qwen3Tokens.EndOfText} (ID: {Qwen3Tokens.EndOfTextTokenId})");
For more examples, see the sample project.
Supported Models
Works with all Qwen3 model variants:
- Qwen3 LLM models (text generation)
- Qwen3-Embedding models (text embeddings)
- Qwen3-Reranker models (document reranking)
- Qwen3-VL models (vision-language)
Requirements
- .NET 10.0 or later
License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please visit the GitHub repository for more information.
Support
For issues, questions, or suggestions, please open an issue on GitHub.
⭐ If you find this project useful, please consider giving it a star on GitHub! ⭐
Your support helps make this project more visible to other developers who might benefit from a native .NET implementation of the Qwen3 tokenizer.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.ML.Tokenizers (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.0 | 2,528 | 11/17/2025 |
| 1.1.0 | 373 | 11/13/2025 |
| 1.0.0 | 376 | 11/11/2025 |
| 0.1.3-preview | 230 | 11/2/2025 |
| 0.1.2-preview | 175 | 11/1/2025 |
| 0.1.1-preview | 155 | 11/1/2025 |
## What's Changed
* Bump actions/checkout from 4 to 5 by @dependabot[bot] in https://github.com/yuniko-software/qwen3-tokenizer-dotnet/pull/13
* Bump actions/setup-dotnet from 4 to 5 by @dependabot[bot] in https://github.com/yuniko-software/qwen3-tokenizer-dotnet/pull/12
* Bump actions/upload-artifact from 4 to 5 by @dependabot[bot] in https://github.com/yuniko-software/qwen3-tokenizer-dotnet/pull/11
* Update package versions in Directory.Packages.props by @demid-ns in https://github.com/yuniko-software/qwen3-tokenizer-dotnet/pull/14
* Release v1.0.0 and update README by @demid-ns in https://github.com/yuniko-software/qwen3-tokenizer-dotnet/pull/15
## New Contributors
* @dependabot[bot] made their first contribution in https://github.com/yuniko-software/qwen3-tokenizer-dotnet/pull/13
**Full Changelog**: https://github.com/yuniko-software/qwen3-tokenizer-dotnet/compare/0.1.3-preview...1.0.0