Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:LaurentMazare Tch rs FFI Error Handling

From Leeroopedia


Knowledge Sources
Domains System Utilities, FFI, Configuration
Last Updated 2026-02-08 00:00 GMT

Overview

The utils module provides system-level utility functions including random seed control, thread management, hardware/backend feature detection, path conversion helpers, error handling macros, and the QEngine quantization engine selector.

Description

This module contains infrastructure functions that support the rest of the tch crate. It is organized into several categories:

Error handling infrastructure: The ptr_to_string function safely converts a C string pointer to a Rust String, freeing the pointer afterward, and returning None for null pointers. The read_and_clean_error function checks for pending C++ errors via torch_sys::get_and_reset_last_err and converts them to TchError::Torch. Two macros, unsafe_torch! (panics on error) and unsafe_torch_err! (propagates error with ?), wrap every FFI call with automatic error checking.

Path conversion: The path_to_cstring function converts a Rust Path to a CString, returning an error if the path cannot be represented as UTF-8.

Random seed: manual_seed(seed: i64) sets the global random seed via torch_sys::at_manual_seed.

Thread management: Four functions control parallelism:

  • get_num_interop_threads() -> i32 and set_num_interop_threads(n: i32) for inter-op parallelism
  • get_num_threads() -> i32 and set_num_threads(n: i32) for intra-op parallelism

Feature detection: A comprehensive set of boolean functions query library availability: has_openmp, has_mkl, has_lapack, has_mkldnn, has_magma, has_cuda, has_cudart, has_cusolver, has_hip, has_ipu, has_xla, has_lazy, has_mps, and has_vulkan. Version queries version_cudnn() -> i64 and version_cudart() -> i64 return cuDNN and CUDA runtime versions.

QEngine: An enum with three variants (NoQEngine, FBGEMM, QNNPACK) for selecting the quantization backend. The set method applies the selection via torch_sys::at_set_qengine.

Usage

Use these utilities for configuring the PyTorch runtime environment (seeds, threads, quantization engines) and for querying available hardware backends before performing device-specific operations.

Code Reference

Source Location

Signature

// Random seed
pub fn manual_seed(seed: i64);

// Thread management
pub fn get_num_interop_threads() -> i32;
pub fn get_num_threads() -> i32;
pub fn set_num_interop_threads(n_threads: i32);
pub fn set_num_threads(n_threads: i32);

// Feature detection
pub fn has_openmp() -> bool;
pub fn has_mkl() -> bool;
pub fn has_lapack() -> bool;
pub fn has_mkldnn() -> bool;
pub fn has_magma() -> bool;
pub fn has_cuda() -> bool;
pub fn has_cudart() -> bool;
pub fn has_cusolver() -> bool;
pub fn has_hip() -> bool;
pub fn has_ipu() -> bool;
pub fn has_xla() -> bool;
pub fn has_lazy() -> bool;
pub fn has_mps() -> bool;
pub fn has_vulkan() -> bool;
pub fn version_cudnn() -> i64;
pub fn version_cudart() -> i64;

// Quantization engine
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum QEngine {
    NoQEngine,
    FBGEMM,
    QNNPACK,
}

impl QEngine {
    pub fn set(self) -> Result<(), TchError>;
}

// Internal utilities (crate-private)
pub(super) unsafe fn ptr_to_string(ptr: *mut c_char) -> Option<String>;
pub(super) fn read_and_clean_error() -> Result<(), TchError>;
pub(super) fn path_to_cstring<T: AsRef<Path>>(path: T) -> Result<CString, TchError>;

Import

use tch::{utils, manual_seed, QEngine};
use tch::{get_num_threads, set_num_threads};
use tch::{get_num_interop_threads, set_num_interop_threads};

I/O Contract

Function Input Output Side Effect
manual_seed i64 seed value () Sets global RNG seed
set_num_threads i32 thread count () Changes intra-op parallelism
set_num_interop_threads i32 thread count () Changes inter-op parallelism
get_num_threads None i32 None (query only)
get_num_interop_threads None i32 None (query only)
QEngine::set self Result<(), TchError> Changes active quantization engine
Feature Detection Function Checks For
has_cuda CUDA support
has_cudart CUDA runtime
has_cusolver cuSOLVER library
has_mkl Intel MKL
has_mkldnn Intel MKL-DNN / oneDNN
has_openmp OpenMP parallelism
has_lapack LAPACK linear algebra
has_magma MAGMA GPU linear algebra
has_hip AMD ROCm HIP
has_ipu Graphcore IPU
has_xla XLA (TPU) backend
has_lazy Lazy tensor backend
has_mps Apple Metal Performance Shaders
has_vulkan Vulkan compute backend
QEngine Variant C int Value Description
NoQEngine 0 No quantization engine
FBGEMM 1 Facebook GEMM library (x86)
QNNPACK 2 Quantized Neural Network Package (ARM/mobile)

Usage Examples

use tch::{utils, manual_seed, QEngine};

// Set reproducible random seed
manual_seed(42);

// Configure threading
tch::set_num_threads(4);
tch::set_num_interop_threads(2);
println!("Intra-op threads: {}", tch::get_num_threads());
println!("Inter-op threads: {}", tch::get_num_interop_threads());

// Check available backends
if utils::has_cuda() {
    println!("CUDA available, cuDNN version: {}", utils::version_cudnn());
}
if utils::has_mps() {
    println!("Apple MPS available");
}
if utils::has_vulkan() {
    println!("Vulkan backend available");
}

// Set quantization engine
QEngine::FBGEMM.set().expect("failed to set FBGEMM");

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment