### StorageView Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Example demonstrating how to create a StorageView for mel-spectrogram features. ```rust use ct2rs::sys::StorageView; let data = vec![0.1, 0.2, 0.3, ...]; let shape = vec![1, 80, 3000]; // [batch, freq, time] let storage = StorageView::new(&shape, &mut data, Device::CPU)?; ``` -------------------------------- ### Running ctranslate2-rs Examples via Cargo Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/examples.md Command-line examples for running built-in ctranslate2-rs examples using Cargo. Note that the whisper example requires the 'whisper' feature to be enabled. ```bash cargo run --example nllb -- /path/to/model ``` ```bash cargo run --example gpt-2 -- /path/to/model ``` ```bash cargo run --example whisper -- /path/to/model # Requires whisper feature ``` ```bash cargo run --example stream -- /path/to/model ``` -------------------------------- ### CPU Configuration Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Demonstrates how to create a default configuration, which is set to use the CPU. ```rust use ct2rs::{Config, Device}; let config = Config::default(); // Defaults to CPU ``` -------------------------------- ### Conditional Device Configuration Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md An example showing how to conditionally set the device to CUDA if available, otherwise defaulting to CPU. ```rust use ct2rs::{Config, Device}; let config = if cuda_available() { Config { device: Device::CUDA, ..Default::default() } } else { Config::default() }; ``` -------------------------------- ### Single GPU Configuration Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Shows how to configure the system to use a single GPU by specifying the device and index. ```rust use ct2rs::{Config, Device}; let config = Config { device: Device::CUDA, device_indices: vec![0], ..Default::default() }; ``` -------------------------------- ### Device Enum Display Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Demonstrates how to print the string representation of Device enum variants. ```rust use ct2rs::Device; println!("{}", Device::CPU); // Prints "CPU" println!("{}", Device::CUDA); // Prints "CUDA" ``` -------------------------------- ### Custom Tokenization Pipeline Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Demonstrates using the sys API with a custom tokenization and detokenization pipeline. ```rust use ct2rs::sys::{Translator, TranslationOptions}; // Custom tokenization let source_text = "Hello, world!"; let tokens = my_custom_tokenizer(source_text); // Use sys::Translator directly let translator = Translator::new("/path/to/model", &Config::default())?; let results = translator.translate_batch( &vec![tokens], &TranslationOptions::default(), None )?; // Custom detokenization let output_tokens = &results[0].hypotheses[0]; let translated = my_custom_detokenizer(output_tokens); println!("{}", translated); ``` -------------------------------- ### Example Usage of TranslationOptions Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md Demonstrates how to create a TranslationOptions instance with custom settings, such as increasing the beam size for translation. ```rust use ct2rs::TranslationOptions; let opts = TranslationOptions { beam_size: 4, ``` -------------------------------- ### Output with Timestamps Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/whisper.md Example of the output format when timestamps are enabled during transcription. ```text <|0:00|> こんにちは <|1:23|> 世界 ``` -------------------------------- ### Quantized (INT8) Configuration Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Shows how to configure the model to use INT8 quantization for reduced precision and potentially faster inference. ```rust use ct2rs::{Config, ComputeType}; let config = Config { compute_type: ComputeType::INT8, ..Default::default() }; ``` -------------------------------- ### Instantiate GenerationOptions Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md Example of creating a GenerationOptions instance with specific generation parameters. Uses default values for unspecified fields. ```rust use ct2rs::GenerationOptions; let opts = GenerationOptions { beam_size: 5, max_length: 100, sampling_temperature: 0.8, return_scores: true, ..Default::default() }; ``` -------------------------------- ### Multi-GPU with Tensor Parallelism Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Illustrates configuring multiple GPUs and enabling tensor parallelism for distributed computation. ```rust use ct2rs::{Config, Device}; let config = Config { device: Device::CUDA, device_indices: vec![0, 1, 2, 3], // Use 4 GPUs tensor_parallel: true, ..Default::default() }; ``` -------------------------------- ### Using ct2rs-platform for Automatic Setup Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Leverage the 'ct2rs-platform' package to automatically detect the target platform and select appropriate features for optimal performance. ```toml [dependencies] ct2rs = { version = "0.9", package = "ct2rs-platform" } ``` -------------------------------- ### ScoringResult Methods and Example Usage Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md Demonstrates how to use the `cumulated_score` and `normalized_score` methods of ScoringResult, and shows a typical scoring workflow. ```rust use ct2rs::ScoringOptions; let result = generator.score_batch(&["Hello world"], &ScoringOptions::default())?; println!("Tokens: {:?}", result[0].tokens); println!("Cumulated: {}", result[0].cumulated_score()); println!("Normalized: {}", result[0].normalized_score()); ``` -------------------------------- ### Optimized CPU Configuration Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Demonstrates optimizing CPU performance by setting the number of threads per replica, limiting queued batches, and configuring core pinning. ```rust use ct2rs::Config; let config = Config { num_threads_per_replica: 8, // 8 threads per replica max_queued_batches: 10, // Limit queue cpu_core_offset: 0, // Pin to cores 0,1,2... ..Default::default() }; ``` -------------------------------- ### Handle Configuration Errors Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/errors.md Illustrates handling configuration errors, such as when a specified GPU is unavailable or CUDA is not properly installed. ```rust use ct2rs::{Config, Device}; let config = Config { device: Device::CUDA, device_indices: vec![5], // GPU 5 doesn't exist ..Default::default() }; // May fail during first inference if GPU unavailable match translator.translate_batch(&sources, &Default::default(), None) { Err(e) => eprintln!("Device error: {}", e), _ => {} } ``` -------------------------------- ### Error Handling Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/whisper.md An example demonstrating how to handle errors during the transcription process using `anyhow::Result`. ```APIDOC ## Error Handling ```rust use anyhow::Result; fn transcribe(model_path: &str, audio_path: &str) -> Result { let whisper = Whisper::new(model_path, Config::default())?; let samples = load_audio(audio_path, whisper.sampling_rate())?; let mut results = whisper.generate(&samples, None, false, &Default::default())?; Ok(results.pop().unwrap_or_default()) } ``` ``` -------------------------------- ### Sys API Error Handling Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Demonstrates error handling for sys API functions, which all return a Result. ```rust use ct2rs::sys::Translator; use anyhow::Result; fn translate_tokens() -> Result>> { let translator = Translator::new("/path/to/model", &Config::default())?; let tokens = vec![vec!["Hello", "world"]]; let results = translator.translate_batch(&tokens, &Default::default(), None)?; Ok(results.into_iter().map(|r| r.hypotheses[0].clone()).collect()) } ``` -------------------------------- ### Built-in Tokenizer Implementations Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md Provides examples for using the recommended built-in tokenizer implementations: Auto Tokenizer, HuggingFace Tokenizers, and SentencePiece. ```APIDOC ## Built-in Tokenizer Implementations **Auto Tokenizer (Recommended):** ```rust use ct2rs::tokenizers::auto::Tokenizer; let tokenizer = Tokenizer::new("/path/to/model")?; ``` **HuggingFace Tokenizers:** ```rust use ct2rs::tokenizers::hf::Tokenizer; let tokenizer = Tokenizer::from_file("/path/to/tokenizer.json")?; ``` **SentencePiece:** ```rust use ct2rs::tokenizers::sentencepiece::Tokenizer; let tokenizer = Tokenizer::from_file( "/path/to/source.spm", "/path/to/target.spm" )?; ``` ``` -------------------------------- ### ComputeType Enum Display Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Demonstrates how to print the string representation of ComputeType enum variants. ```rust use ct2rs::ComputeType; println!("{}", ComputeType::DEFAULT); // "default" println!("{}", ComputeType::INT8); // "int8" println!("{}", ComputeType::FLOAT16); // "float16" ``` -------------------------------- ### TranslationOptions Configuration Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md Example of setting various translation options. Use this to customize translation behavior like length penalty, sampling temperature, and score returns. ```rust TranslationOptions { length_penalty: 0.6, sampling_temperature: 0.7, return_scores: true, ..Default::default() }; ``` -------------------------------- ### Custom Tokenizer Implementation Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/tokenizers.md Example of implementing the `Tokenizer` trait for custom tokenization logic, such as whitespace splitting. ```APIDOC ## Custom Tokenizer Implementation ### Description Demonstrates how to implement the `Tokenizer` trait for custom logic. ### Example ```rust use ct2rs::Tokenizer as TokenizerTrait; struct WhitespaceTokenizer; impl TokenizerTrait for WhitespaceTokenizer { fn encode(&self, input: &str) -> anyhow::Result> { Ok(input .split_whitespace() .map(String::from) .collect()) } fn decode(&self, tokens: Vec) -> anyhow::Result { Ok(tokens.join(" ")) } } // Use with Translator let translator = Translator::with_tokenizer( "/path/to/model", WhitespaceTokenizer, &Config::default() )?; ``` ``` -------------------------------- ### Basic Translation Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/index.md Performs batch text translation using the Translator API. Ensure the model path is correct and the default configuration is suitable for your environment. ```rust use ct2rs::{Config, Translator}; let translator = Translator::new("/path/to/model", &Config::default())?; let results = translator.translate_batch( &["Hello, world!"], &Default::default(), None )?; println!("{}", results[0].0); // Translated text ``` -------------------------------- ### Use system-installed CTranslate2 Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Use system-installed CTranslate2 instead of building from source. This eliminates the C++ compilation step but requires the CTranslate2 library to be installed system-wide and the CT2_HOME environment variable to be set. ```toml [dependencies] ct2rs = { version = "0.9", features = ["system"] } ``` -------------------------------- ### Enable Intel MKL for CPU Optimization Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Enables Intel Math Kernel Library (oneAPI MKL) for CPU optimization. Requires Intel oneAPI MKL installed or it will be auto-downloaded during build. Provides a 2-10x speedup on CPU inference and increases binary size. ```toml [dependencies] ct2rs = { version = "0.9", features = ["mkl"] } ``` -------------------------------- ### Streaming Translation Example with GenerationStepResult Callback Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md Illustrates how to perform streaming translation using a callback function that processes each GenerationStepResult. Requires beam_size to be 1. ```rust use ct2rs::{TranslationOptions, GenerationStepResult}; let mut callback = |step: GenerationStepResult| -> anyhow::Result<()> { if step.is_last { println!(" [{:.3}]", step.score.unwrap_or(0.0)); } else { print!("{}", step.text); } Ok(()) }; let options = TranslationOptions { beam_size: 1, // Required for streaming ..Default::default() }; translator.translate_batch(&["text"], &options, Some(&mut callback))?; ``` -------------------------------- ### Generate Text with Configurable Parameters Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/examples.md Generate text from prompts using a GPT-2 model with configurable parameters such as beam size, max length, top-k sampling, and temperature. This example demonstrates batch generation and score retrieval. ```rust use ct2rs::{Config, Device, Generator, GenerationOptions}; use anyhow::Result; fn main() -> Result<()> { let generator = Generator::new( "/path/to/gpt2-model", &Config { device: Device::CUDA, ..Default::default() } )?; let prompts = vec![ "Once upon a time", "In a galaxy far away", "The future is", ]; let options = GenerationOptions { beam_size: 4, max_length: 100, sampling_topk: 50, sampling_temperature: 0.8, return_scores: true, ..Default::default() }; let results = generator.generate_batch(&prompts, &options, None)?; for (i, (sequences, scores)) in results.iter().enumerate() { println!("\nPrompt {}: {}", i + 1, prompts[i]); for (seq, score) in sequences.iter().zip(scores.iter()) { println!(" Score {:.4}: {}", score, seq); } } Ok(()) } ``` -------------------------------- ### Instantiate ScoringOptions Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md Example of creating a ScoringOptions instance, setting a custom maximum input length. Other fields use default values. ```rust use ct2rs::ScoringOptions; let opts = ScoringOptions { max_input_length: 512, ..Default::default() }; ``` -------------------------------- ### Download and Use Generation Model Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/hub.md Downloads a CTranslate2 GPT-2 model from HuggingFace Hub and uses it for text generation. This example shows how to set generation options like maximum length. ```rust #[cfg(feature = "hub")] use ct2rs::{download_model, Generator, GenerationOptions, Config}; #[tokio::main] async fn main() -> anyhow::Result<()> { let model_path = download_model("jkawamoto/gpt2-ct2")?; let generator = Generator::new(&model_path, &Config::default())?; let results = generator.generate_batch( &["Once upon a time"], &GenerationOptions { max_length: 50, ..Default::default() }, None )?; for sequence in results[0].0 { println!("{}", sequence); } Ok(()) } ``` -------------------------------- ### Development Configuration Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Configure ctranslate2-rs for development with CPU and debug logging enabled. This setup is useful for testing and debugging. ```rust use ct2rs::{Config, LogLevel, sys::set_log_level}; set_log_level(LogLevel::Debug); let config = Config::default(); ``` -------------------------------- ### Enable OpenBLAS for CPU Matrix Operations Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Enables OpenBLAS for CPU matrix operations. Requires the OpenBLAS library installed. Offers CPU optimization, though typically slower than MKL, and provides broader platform support. ```toml [dependencies] ct2rs = { version = "0.9", features = ["openblas"] } ``` -------------------------------- ### Speech Recognition with Whisper Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/examples.md Transcribe audio files using the Whisper model. This example demonstrates model initialization, loading audio, and performing transcription with language detection and timestamp inclusion. ```rust #[cfg(feature = "whisper")] use ct2rs::{Config, Device, Whisper, WhisperOptions}; #[cfg(feature = "whisper")] use std::fs::File; #[cfg(feature = "whisper")] fn main() -> anyhow::Result<()> { // Initialize Whisper let whisper = Whisper::new( "/path/to/whisper-model", Config { device: Device::CUDA, ..Default::default() } )?; println!("Model info:"); println!(" Multilingual: {}", whisper.is_multilingual()); println!(" Languages: {}", whisper.num_languages()); println!(" Sample rate: {} Hz", whisper.sampling_rate()); println!(" Max samples: {}", whisper.n_samples()); // Load and process audio let samples = load_audio_file("speech.wav", whisper.sampling_rate())?; // Transcribe with language detection let options = WhisperOptions { beam_size: 5, sampling_temperature: 0.0, // Deterministic ..Default::default() }; let transcripts = whisper.generate( &samples, None, // Auto-detect language true, // Include timestamps &options )?; println!("Transcription:"); for transcript in transcripts { println!("{}", transcript); } Ok(()) } #[cfg(feature = "whisper")] fn load_audio_file(path: &str, target_sr: usize) -> anyhow::Result> { // Simplified: in real code, use hound or similar // This would load WAV, normalize to [-1, 1], and resample if needed Ok(vec![]) } ``` -------------------------------- ### Custom Tokenizer Implementation Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md An example implementation of the `Tokenizer` trait using basic string splitting and joining. This serves as a template for creating custom tokenizers. ```rust use ct2rs::Tokenizer; struct MyTokenizer; impl Tokenizer for MyTokenizer { fn encode(&self, input: &str) -> anyhow::Result> { Ok(input.split_whitespace().map(String::from).collect()) } fn decode(&self, tokens: Vec) -> anyhow::Result { Ok(tokens.join(" ")) } } ``` -------------------------------- ### Batch Processing with Queue Monitoring Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/examples.md Process a large batch of sentences and monitor the translation queue status. This example demonstrates how to check the number of queued and active batches before and after translation. ```rust use ct2rs::{Config, Translator}; use anyhow::Result; fn main() -> Result<()> { let translator = Translator::new("/path/to/model", &Config::default())?; // Generate large batch let large_batch: Vec<&str> = (0..10000) .map(|i| &format!("Sentence {}", i)) .collect(); // Check queue status println!("Before translation:"); println!(" Queued: {}", translator.num_queued_batches()?); println!(" Active: {}", translator.num_active_batches()?); println!(" Replicas: {}", translator.num_replicas()?); let results = translator.translate_batch(&large_batch, &Default::default(), None)?; println!("Translated {} sentences", results.len()); Ok(()) } ``` -------------------------------- ### Download and Use Translation Model Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/hub.md Downloads a CTranslate2 model from HuggingFace Hub, caches it locally, and then uses it for translation. This example demonstrates basic translation usage. ```rust use ct2rs::{download_model, Translator, Config}; #[tokio::main] async fn main() -> anyhow::Result<()> { // Download model (cached after first download) let model_path = download_model("jkawamoto/nllb-200-distilled-600M-ct2")?; // Use downloaded model let translator = Translator::new(&model_path, &Config::default())?; let results = translator.translate_batch( &["Hello, world!"], &Default::default(), None )?; println!("{}", results[0].0); Ok(()) } ``` -------------------------------- ### Text Generation Example Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/index.md Generates text sequences using the Generator API. This is useful for tasks like autocompletion or open-ended text generation. The model path and configuration should be set appropriately. ```rust use ct2rs::{Config, Generator, GenerationOptions}; let generator = Generator::new("/path/to/model", &Config::default())?; let results = generator.generate_batch( &["Once upon a time"], &GenerationOptions::default(), None )?; for sequence in results[0].0 { println!("{}", sequence); } ``` -------------------------------- ### HuggingFace Tokenizer with Translator (NLLB Example) Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/tokenizers.md Translates text using a HuggingFace tokenizer loaded from a file with a Translator. Supports multilingual models like NLLB. ```rust use ct2rs::{Translator, TranslationOptions}; use ct2rs::tokenizers::hf::Tokenizer as HFTokenizer; let tokenizer = HFTokenizer::from_file( "/path/to/nllb-model/tokenizer.json" )?; let translator = Translator::with_tokenizer( "/path/to/nllb-model", tokenizer, &Config::default() )?; let results = translator.translate_batch_with_target_prefix( &["Hello"], &vec![vec!["jpn_Jpan"]], &Default::default(), None )?; println!("{}", results[0].0); // Japanese translation ``` -------------------------------- ### WhisperOptions Configuration Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/whisper.md Example of configuring Whisper transcription options, including beam size, sampling temperature, and max length. Uses default values for other options. ```rust use ct2rs::WhisperOptions; let options = WhisperOptions { beam_size: 10, // Larger beam for higher quality sampling_temperature: 0.8, // More deterministic max_length: 448, // Max tokens in Whisper ..Default::default() }; ``` -------------------------------- ### Generate Token Sequences with sys::Generator Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Generate token sequences from provided starting prompts using the sys::Generator. Supports custom options and an optional streaming callback. ```rust let prompts = vec![ vec!["Hello", ""], vec!["Goodbye", ""], ]; let results = generator.generate_batch(&prompts, &Default::default(), None)?; for result in results { for seq in result.sequences { println!("Generated: {:?}", seq); } } ``` -------------------------------- ### Multi-GPU Translation with Tensor Parallelism Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/examples.md Utilize multiple GPUs for translation by enabling tensor parallelism. This example configures the translator to use a specified list of device indices and sets a high number of queued batches for optimal GPU utilization. ```rust use ct2rs::{Config, Device, Translator}; use anyhow::Result; fn main() -> Result<()> { let config = Config { device: Device::CUDA, device_indices: vec![0, 1, 2, 3], // Use 4 GPUs tensor_parallel: true, max_queued_batches: 100, ..Default::default() }; let translator = Translator::new("/path/to/large-model", &config)?; // Large batch for optimal GPU utilization let sources: Vec<&str> = (0..1000) .map(|_| "Hello world") .collect(); let results = translator.translate_batch( &sources, &Default::default(), None )?; println!("Translated {} sentences", results.len()); Ok(()) } ``` -------------------------------- ### HuggingFace Hub Model Integration Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/examples.md Download and use models directly from the HuggingFace Hub. Models are automatically cached after the first download, simplifying model management. This example shows downloading a model and performing a translation task. ```rust #[cfg(feature = "hub")] use ct2rs::{download_model, Translator, Config}; #[cfg(feature = "hub")] fn main() -> anyhow::Result<()> { // Models are cached after first download let model_path = download_model("jkawamoto/nllb-200-distilled-600M-ct2")?; println!("Model path: {}", model_path.display()); let translator = Translator::new(&model_path, &Config::default())?; let results = translator.translate_batch_with_target_prefix( &["Hello"], &vec![vec!["jpn_Jpan"]], &Default::default(), None )?; println!("Translation: {}", results[0].0); Ok(()) } ``` -------------------------------- ### num_replicas Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/translator.md Gets the number of parallel replicas configured for the translator, indicating the level of parallel processing. ```APIDOC ## num_replicas ### Description Returns the number of parallel replicas. ### Method `num_replicas` ### Returns `Result` - Number of translator copies running in parallel. ### Example: ```rust let replicas = translator.num_replicas()?; println!("Parallel replicas: {}", replicas); ``` ``` -------------------------------- ### Use System-Installed Intel MKL Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Uses system-installed Intel MKL instead of downloading it. Requires Intel oneAPI MKL pre-installed on the system and the 'intel-onemkl-prebuild' crate configured. Results in a smaller binary as MKL is not embedded, but introduces a deployment dependency on MKL. ```toml [dependencies] ct2rs = { version = "0.9", features = ["system-mkl"] } ``` -------------------------------- ### Initialize and Debug Generator Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/generator.md Demonstrates how to initialize a Generator with a model path and default configuration, and how to print its debug representation. ```rust let generator = Generator::new("/path/to/model", &Config::default())?; println!("{:?}", generator); // Output: Generator { generator: Generator { model: "model_name" }, tokenizer: ... } ``` -------------------------------- ### Initialize BPE Tokenizer Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/tokenizers.md Creates a new BPE tokenizer instance. Requires the path to the model's vocabulary files and an optional configuration path. ```rust use ct2rs::tokenizers::bpe; let tokenizer = bpe::new("/path/to/model", None)?; ``` -------------------------------- ### Common Tokenizer Patterns Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/tokenizers.md Illustrates common patterns for initializing tokenizers, including auto-detection, explicit HuggingFace, and explicit SentencePiece. ```APIDOC ## Common Patterns ### Auto-Detect (Most Cases) ```rust let translator = Translator::new("/path/to/model", &Config::default())?; // Automatically uses auto::Tokenizer ``` ### Explicit HuggingFace (Best for Modern Models) ```rust use ct2rs::tokenizers::hf::Tokenizer; let tokenizer = Tokenizer::new("/path/to/model")?; let translator = Translator::with_tokenizer( "/path/to/model", tokenizer, &Config::default() )?; ``` ### Explicit SentencePiece (Legacy Models) ```rust use ct2rs::tokenizers::sentencepiece::Tokenizer; let tokenizer = Tokenizer::from_file( "/path/to/source.spm", "/path/to/target.spm" )?; let translator = Translator::with_tokenizer( "/path/to/model", tokenizer, &Config::default() )?; ``` ``` -------------------------------- ### Building with Specific Features Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Compile the project with a custom set of features, such as 'cuda' and 'all-tokenizers', using the 'cargo build' command. ```bash # Build with specific features cargo build --features "cuda,all-tokenizers" ``` -------------------------------- ### Get Number of Queued Batches Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/generator.md Retrieves the count of batches currently waiting in the processing queue. Use this to monitor the workload. ```rust let num_queued = generator.num_queued_batches()?; println!("Queued batches: {}", num_queued); ``` -------------------------------- ### Get sys::Translator Queue Status Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Retrieve the number of queued batches, active batches, and replicas for the sys::Translator. ```rust pub fn num_queued_batches(&self) -> Result pub fn num_active_batches(&self) -> Result pub fn num_replicas(&self) -> Result ``` -------------------------------- ### Import Download Model Function Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/hub.md Import the `download_model` function when the 'hub' feature is enabled. ```rust #[cfg(feature = "hub")] use ct2rs::download_model; ``` -------------------------------- ### Convert and Upload Model using CLI Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/hub.md Provides command-line instructions for converting a HuggingFace model (like NLLB or GPT-2) to CTranslate2 format using `ct2-transformers-converter` and then uploading it to HuggingFace Hub using `huggingface-cli`. ```bash pip install ctranslate2 huggingface_hub torch transformers # For NLLB ct2-transformers-converter \ --model facebook/nllb-200-distilled-600M \ --output_dir ./nllb-200-distilled-600M-ct2 \ --copy_files tokenizer.json # For other models ct2-transformers-converter \ --model model/path/or/id \ --output_dir ./converted-model-ct2 huggingface-cli upload owner/model-ct2 ./converted-model-ct2 ``` -------------------------------- ### Get Random Seed Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Retrieve the current random number generator seed using get_random_seed. This can be used to verify the seed that has been set. ```rust use ct2rs::sys::{set_random_seed, get_random_seed}; set_random_seed(12345); let seed = get_random_seed(); assert_eq!(seed, 12345); ``` -------------------------------- ### Set CUDNN_HOME environment variable Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Set the CUDNN_HOME environment variable to specify the path to your cuDNN installation. This is required for cuDNN-related features. ```bash export CUDNN_HOME=/usr/local/cudnn cargo build --features cudnn ``` -------------------------------- ### Set CUDA_TOOLKIT_ROOT_DIR environment variable Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Set the CUDA_TOOLKIT_ROOT_DIR environment variable to specify the path to your CUDA installation. This is required for CUDA-related features. ```bash export CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda cargo build --features cuda ``` -------------------------------- ### sys::Generator::generate_batch Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Generates token sequences from provided starting token sequences. Supports streaming results via a callback. ```APIDOC ## sys::Generator::generate_batch ### Description Generates token sequences from provided starting token sequences. Supports streaming results via a callback. ### Method `generate_batch` ### Parameters #### Path Parameters - **start_tokens** (&[Vec]) - Required - Starting token sequences. - **options** (&GenerationOptions) - Required - Generation options. - **callback** (Option<&mut dyn FnMut(GenerationStepResult) -> bool>) - Optional - Streaming callback. ### Response #### Success Response - **Vec** - A vector of generation results, where each result contains generated sequences, their IDs, and scores. ### Response Body Example ```rust pub struct GenerationResult { pub sequences: Vec>, pub sequences_ids: Vec>, pub scores: Vec, } ``` ### Example ```rust let prompts = vec![ vec!["Hello", ""], vec!["Goodbye", ""], ]; let results = generator.generate_batch(&prompts, &Default::default(), None)?; for result in results { for seq in result.sequences { println!("Generated: {:?}", seq); } } ``` ``` -------------------------------- ### Initialize SentencePiece Tokenizer Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md Loads a SentencePiece tokenizer using source and target model files. ```rust use ct2rs::tokenizers::sentencepiece::Tokenizer; let tokenizer = Tokenizer::from_file( "/path/to/source.spm", "/path/to/target.spm" )?; ``` -------------------------------- ### BatchType Enum Definition Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Defines how batch size limits are calculated. Use Examples to count sequences or Tokens to count total tokens. ```rust pub enum BatchType { Examples, // Count based on number of sequences Tokens, // Count based on total tokens } impl Default for BatchType { fn default() -> Self { Self::Examples } } ``` -------------------------------- ### Get Number of Generator Replicas Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/generator.md Retrieves the number of parallel generator instances. Use this to understand the model's parallel processing capacity. ```rust let num_replicas = generator.num_replicas()?; println!("Number of replicas: {}", num_replicas); ``` -------------------------------- ### Get Sampling Rate Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/whisper.md Retrieve the expected audio sampling rate for the Whisper model. This is crucial for correctly loading and processing audio input. ```rust let sr = whisper.sampling_rate(); println!("Expected sample rate: {} Hz", sr); // Resample input audio if needed let resampled = resample_audio(&raw_audio, 48000, sr)?; ``` -------------------------------- ### Set CT2_HOME environment variable Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Set the CT2_HOME environment variable to specify the CTranslate2 installation path. This is required when using the `system` feature. ```bash export CT2_HOME=/opt/ctranslate2 ``` -------------------------------- ### Convert Model for CTranslate2 Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/README.md Use this command to convert Hugging Face models to the CTranslate2 format. Ensure you have the necessary Python packages installed. ```shell pip install ctranslate2 huggingface_hub torch transformers ct2-transformers-converter --model facebook/nllb-200-distilled-600M --output_dir nllb-200-distilled-600M \ --copy_files tokenizer.json ``` -------------------------------- ### Whisper::new Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/whisper.md Initializes the Whisper transcriber with a model path and runtime configuration. ```APIDOC ## Whisper::new ### Description Initializes the Whisper transcriber. ### Signature ```rust pub fn new>( model_path: T, config: Config ) -> Result ``` ### Parameters | Parameter | Type | Default | Description | |-----------|------|---------|-------------| | `model_path` | `AsRef` | — | Path to model directory containing `model.bin`, `vocabulary.json`, and `preprocessor_config.json` | | `config` | `Config` | — | Runtime configuration (device, compute type) | ### Returns `Result` — Initialized Whisper instance. ### Errors - Model files not found - `preprocessor_config.json` missing or malformed - Tokenizer initialization failed - Device configuration error ### Example ```rust use ct2rs::{Whisper, Config, Device}; let whisper = Whisper::new( "/path/to/whisper-model", Config { device: Device::CUDA, ..Default::default() } )?; ``` ``` -------------------------------- ### SentencePiece Tokenizer Constructor (from_file) Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/tokenizers.md Loads a SentencePiece tokenizer from explicit source and target model file paths. Requires the 'sentencepiece' feature. ```rust use ct2rs::tokenizers::sentencepiece::Tokenizer; let tokenizer = Tokenizer::from_file( "/path/to/source.spm", "/path/to/target.spm" )?; let translator = Translator::with_tokenizer( "/path/to/model", tokenizer, &Config::default() )?; ``` -------------------------------- ### Production Configuration Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Configure ctranslate2-rs for production using multiple GPUs with INT8 compute type. This setup optimizes for performance and multi-GPU utilization. ```rust use ct2rs::{Config, Device, ComputeType}; let config = Config { device: Device::CUDA, compute_type: ComputeType::INT8, device_indices: vec![0, 1], tensor_parallel: true, max_queued_batches: 100, ..Default::default() }; ``` -------------------------------- ### Generate Batch with Streaming Callback Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/generator.md Generates text continuations for a single prompt with token-by-token streaming. Requires `beam_size` to be 1 and uses a callback to process each generated token as it arrives. ```rust use std::io::{stdout, Write}; use ct2rs::GenerationOptions; let options = GenerationOptions { beam_size: 1, // Required for streaming max_length: 100, ..Default::default() }; let mut callback = |step| -> anyhow::Result<()> { print!("{}", step.text); stdout().flush()?; Ok(()) }; generator.generate_batch( &["prompt"], &options, Some(&mut callback) )?; ``` -------------------------------- ### Translator::new Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/translator.md Initializes a translator with automatic tokenizer detection. It takes a path to the model directory and runtime configuration. ```APIDOC ## Translator::new ### Description Initializes a translator with automatic tokenizer detection. ### Method ```rust pub fn new>( path: U, config: &Config ) -> anyhow::Result ``` ### Parameters #### Path Parameters - **path** (AsRef) - Required - Path to the directory containing the model and tokenizer files - **config** (Config) - Required - Runtime configuration including device, compute type, and threading ### Returns `Result>` — Initialized translator with auto-detected tokenizer, or an error if model loading or tokenizer detection fails. ### Errors - Invalid model path - Model file not found or corrupted - No compatible tokenizer found in the model directory - Configuration error (e.g., requested GPU not available) ### Request Example ```rust use ct2rs::{Config, Translator}; let translator = Translator::new( "/path/to/nllb-model", &Config::default() )?; ``` ``` -------------------------------- ### Get Device Count Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Query the number of available devices of a specific type (CPU or CUDA) using get_device_count. This helps in determining hardware capabilities. ```rust use ct2rs::{Device, sys::get_device_count}; let cpu_count = get_device_count(Device::CPU); let gpu_count = get_device_count(Device::CUDA); println!("CPUs: {}, GPUs: {}", cpu_count, gpu_count); ``` -------------------------------- ### Get Current Log Level Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Query the current global log level using the get_log_level function. This can be used to check the active logging verbosity. ```rust use ct2rs::sys::{get_log_level, LogLevel}; let current = get_log_level(); assert_eq!(current, LogLevel::Warning); ``` -------------------------------- ### Enable dynamic CUDA loading Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Load CUDA libraries dynamically at runtime. This results in a smaller binary size and more flexible deployment but requires CUDA runtime libraries to be available. ```toml [dependencies] ct2rs = { version = "0.9", features = ["cuda", "cuda-dynamic-loading"] } ``` -------------------------------- ### Get Number of Queued Batches Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/translator.md Retrieves the count of translation batches currently waiting in the work queue. This helps in monitoring the workload and potential bottlenecks. ```rust let queued = translator.num_queued_batches()?; println!("Queued batches: {}", queued); ``` -------------------------------- ### Mobile/Embedded Configuration Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Configure ctranslate2-rs for mobile or embedded systems with INT8 compute type and limited threads. This setup prioritizes low resource usage. ```rust use ct2rs::{Config, ComputeType}; let config = Config { compute_type: ComputeType::INT8, num_threads_per_replica: 2, max_queued_batches: 1, ..Default::default() }; ``` -------------------------------- ### Use Custom Tokenizer with Translator Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/README.md Shows how to initialize a Translator with a custom tokenizer implementation. This allows for flexible tokenization strategies beyond the built-in options. ```rust let translator = Translator::with_tokenizer( "/path/to/model", MyCustomTokenizer::new()?, &Config::default() )?; ``` -------------------------------- ### Import Core Types Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/config.md Imports necessary types for configuration, device selection, compute types, batching, and logging. ```rust use ct2rs::{Config, Device, ComputeType, BatchType, LogLevel}; use ct2rs::sys::{set_log_level, set_random_seed, get_log_level, get_random_seed, get_device_count}; ``` -------------------------------- ### Instantiate sys::Translator Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Create a new sys::Translator instance by providing the model path and configuration. ```rust use ct2rs::sys::Translator; let translator = Translator::new("/path/to/model", &Config::default())?; ``` -------------------------------- ### Initialize Auto Tokenizer Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/types.md Recommended way to initialize a tokenizer. Loads the appropriate tokenizer based on the model path. ```rust use ct2rs::tokenizers::auto::Tokenizer; let tokenizer = Tokenizer::new("/path/to/model")?; ``` -------------------------------- ### Instantiate sys::Generator Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Create a new sys::Generator instance by providing the model path and configuration. ```rust pub fn new>( model_path: T, config: &Config ) -> Result ``` -------------------------------- ### Validating Inputs Before Operation Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/errors.md Prevent errors by performing input validation before attempting an operation. This example checks if a model path exists and is a directory before initializing the translator. ```rust use std::path::Path; use anyhow::bail; use ct2rs::Translator; fn safe_translator_new(model_path: &str) -> anyhow::Result { if !Path::new(model_path).exists() { bail!("Model directory not found: {}", model_path); } if !Path::new(model_path).is_dir() { bail!("Model path is not a directory: {}", model_path); } Translator::new(model_path, &Config::default()) } ``` -------------------------------- ### Translate Text with Streaming Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/README.md Demonstrates how to perform batch translation with token-by-token streaming. Ensure `beam_size` is set to 1 for streaming to function correctly. ```rust let options = TranslationOptions { beam_size: 1, // Required for streaming ..Default::default() }; translator.translate_batch( &["text"], &options, Some(&mut |step| { /* process token */ Ok(()) }) )?; ``` -------------------------------- ### Get Number of Active and Queued Batches Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/generator.md Retrieves the total count of batches that are either actively being processed or are waiting in the queue. Use this for a comprehensive view of the workload. ```rust let num_active = generator.num_active_batches()?; println!("Active and queued batches: {}", num_active); ``` -------------------------------- ### StorageView Constructor Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Creates a new StorageView instance with specified shape, data, and device. ```rust pub fn new( shape: &[usize], data: &mut [f32], device: Device ) -> Result ``` -------------------------------- ### Get Number of Parallel Replicas Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/translator.md Retrieves the number of parallel translator instances running. This is configured via `Config::num_replicas` and indicates the level of parallelism for translation tasks. ```rust let replicas = translator.num_replicas()?; println!("Parallel replicas: {}", replicas); ``` -------------------------------- ### Listing All Available Features Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Retrieve a list of all available features for the 'ct2rs' package by parsing the output of 'cargo metadata'. ```bash # Show all available features cargo metadata --no-deps --format-version 1 | jq '.packages[] | select(.name=="ct2rs") | .features' ``` -------------------------------- ### Handle HuggingFace Hub Download Errors Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/errors.md Demonstrates error handling for downloading models from the HuggingFace Hub, covering network issues, model not found, or disk space problems. ```rust use ct2rs::download_model; match download_model("invalid/model-name") { Ok(path) => println!("Downloaded to: {}", path.display()), Err(e) => eprintln!("Download failed: {}", e), } ``` -------------------------------- ### Transcribe Audio (Auto-detect Language) Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/whisper.md Use the `generate` method to transcribe audio samples. Set language to `None` for auto-detection and `false` for no timestamps. The audio samples should be normalized and sampled at the model's rate. ```rust use ct2rs::WhisperOptions; // Load audio (16 kHz, mono, normalized to [-1, 1]) let samples = load_audio("speech.wav", whisper.sampling_rate())?; let transcripts = whisper.generate( &samples, None, // Auto-detect language false, // No timestamps &Default::default() )?; for transcript in transcripts { println!("{}", transcript); } ``` -------------------------------- ### SentencePiece Tokenizer - From File Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/tokenizers.md Loads a SentencePiece tokenizer from explicit source and target model file paths. ```APIDOC ## SentencePiece Tokenizer - From File Load from explicit file paths. ### Constructor: ```rust pub fn from_file>( source_path: T, target_path: T ) -> Result ``` #### Parameters: - **source_path** (`AsRef`) - Path to encoder model (.spm) - **target_path** (`AsRef`) - Path to decoder model (.spm) ### Example: ```rust let tokenizer = Tokenizer::from_file( "/path/to/source.spm", "/path/to/target.spm" )?; ``` ``` -------------------------------- ### Configure RUSTFLAGS for optimization and targeting Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/features.md Configure RUSTFLAGS for compiler optimizations or architecture targeting. Examples include enabling SSE4.1, targeting native CPU, or enabling static linking on Windows. ```bash # Enable SSE4.1 export RUSTFLAGS="-C target-feature=+sse4.1" # Target specific CPU export RUSTFLAGS="-C target-cpu=native" # Windows static linking export RUSTFLAGS="-C target-feature=+crt-static" cargo build ``` -------------------------------- ### Generate Batch with Advanced Options Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/generator.md Generates text continuations using advanced configuration options like beam search, sampling, and length constraints. Use this to fine-tune the generation process for specific requirements. ```rust use ct2rs::{GenerationOptions, GenerationStepResult}; let options = GenerationOptions { beam_size: 4, // Beam search with 4 hypotheses max_length: 256, // Maximum output length min_length: 10, // Minimum output length sampling_topk: 50, // Sample from top-50 tokens sampling_temperature: 0.8, // Randomness control return_scores: true, // Return sequence scores ..Default::default() }; let results = generator.generate_batch(&prompts, &options, None)?; for (sequences, scores) in results { for (seq, score) in sequences.iter().zip(scores.iter()) { println!("Score: {:.4} | Text: {}", score, seq); } } ``` -------------------------------- ### Score Text with Language Model Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/examples.md Score a batch of sentences using a language model to evaluate their likelihood. This example shows how to use the Generator for scoring and retrieve token-level and cumulated scores. ```rust use ct2rs::{Config, Generator, ScoringOptions}; use anyhow::Result; fn main() -> Result<()> { let generator = Generator::new( "/path/to/model", &Config::default() )?; let sentences = vec![ "The cat is on the mat.", "Cat the on is mat the.", // Grammatically incorrect ]; let options = ScoringOptions::default(); let results = generator.score_batch(&sentences, &options)?; println!("Sentence Scores:"); for (sentence, result) in sentences.iter().zip(results.iter()) { println!("Text: {}", sentence); println!("Tokens: {:?}", result.tokens); println!("Cumulated Score: {:.4}", result.cumulated_score()); println!("Normalized Score: {:.4}\n", result.normalized_score()); } Ok(()) } ``` -------------------------------- ### HuggingFace Tokenizer - From File Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/tokenizers.md Loads a HuggingFace tokenizer from an explicit `tokenizer.json` file path. ```APIDOC ## HuggingFace Tokenizer - From File Load tokenizer from explicit file path. ### Constructor: ```rust pub fn from_file>(path: T) -> Result ``` #### Parameters: - **path** (`AsRef`) - Path to `tokenizer.json` file ### Example: ```rust let tokenizer = Tokenizer::from_file("/path/to/tokenizer.json")?; ``` ``` -------------------------------- ### Import sys API Components Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/sys-api.md Import necessary components from the sys module for translator, generator, and configuration. ```rust use ct2rs::sys::{Translator, Generator, Whisper, TranslationOptions, GenerationOptions}; ``` -------------------------------- ### Login to Hugging Face CLI Source: https://github.com/jkawamoto/ctranslate2-rs/blob/main/_autodocs/hub.md Log in to the Hugging Face CLI to authenticate requests for downloading models from the Hub. This is necessary for high-volume downloads and to bypass rate limits. ```bash huggingface-cli login ```