Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:LaurentMazare Tch rs VarStore Set Kind

From Leeroopedia


Knowledge Sources
Domains Deep_Learning, Memory_Optimization
Last Updated 2026-02-08 14:00 GMT

Overview

Concrete tool for casting all VarStore parameters to a target precision provided by the tch nn module.

Description

VarStore::set_kind casts all existing variables in the VarStore to the specified Kind (dtype) and updates the default kind for newly created variables. For float-specific conversion, convenience methods half(), bfloat16(), float(), and double() ensure only float-like variables are converted.

Usage

Call after constructing the model and before loading weights. For LLM inference with float16 weights, call vs.set_kind(Kind::Half) or vs.half().

Code Reference

Source Location

  • Repository: tch-rs
  • File: src/nn/var_store.rs
  • Lines: 337-340

Signature

pub fn set_kind(&mut self, kind: Kind)

Import

use tch::{nn, Kind};

I/O Contract

Inputs

Name Type Required Description
kind Kind Yes Target dtype (Kind::Half, Kind::BFloat16, Kind::Float, Kind::Double)

Outputs

Name Type Description
() unit All variables converted in-place; default kind updated

Usage Examples

use tch::{nn, Kind, Device};

let mut vs = nn::VarStore::new(Device::Cpu);
// ... build model ...

// Convert all parameters to float16
vs.set_kind(Kind::Half);

// Or use convenience method
// vs.half();

Related Pages

Implements Principle

Page Connections

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