Tokenizersο
All tokenizers subclass BaseTokenizer and share a common
interface (encode / decode / tokenize / detokenize /
reconstruct / forward / from_pretrained / save_pretrained).
- class medtokenizers.BaseTokenizer(dim=None, name='BaseTokenizer')[source][source]ο
Bases:
abc.ABC,torch.nn.modules.module.ModuleAbstract base class for all tokenizers with HuggingFace Hub integration.
Tokenizers transform input images/volumes into latent representations (continuous or discrete) and back. This class provides:
Abstract encode/decode interface - Subclasses implement specifics
HuggingFace Hub integration - save_pretrained, from_pretrained, push_to_hub
Batch processing utilities - For large datasets
Sliding window inference - For volumes larger than GPU memory
Architecture Overviewο
A tokenizer consists of: - Encoder: Compresses input to low-dimensional latent - Quantizer (discrete only): Maps continuous latent to discrete codes - Decoder: Reconstructs input from latent
` Input -> Encoder -> [Quantizer] -> Latent -> Decoder -> Reconstruction `HuggingFace Hub Integrationο
All tokenizers can be saved and loaded from the HuggingFace Hub:
>>> model.save_pretrained("./my-tokenizer") >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer") >>> model.push_to_hub("username/my-tokenizer")
Thread Safetyο
Models should be used in single-threaded contexts or with appropriate synchronization. The eval()/train() mode switching is NOT thread-safe.
- type dim:
- param dim:
Spatial dimensionality (2 for images, 3 for volumes)
- type name:
str, default:'BaseTokenizer'- param name:
Human-readable model name
- abstractmethod decode(z)[source][source]ο
Decode latent representation to output.
- Parameters:
z (
Float[Tensor, 'batch channels *spatial']) β Latent tensor (continuous codes or quantized codes)- Return type:
Float[Tensor, 'batch channels *spatial']- Returns:
Reconstructed output with same spatial shape as original input
- abstractmethod forward(x)[source][source]ο
Full forward pass: encode -> [quantize] -> decode.
- Parameters:
x (
Float[Tensor, 'batch channels *spatial']) β Input tensor- Returns:
βreconstructionsβ: Reconstructed output
Additional keys vary by subclass (posteriors, quant_loss, etc.)
- Return type:
Dictionary containing at minimum
- inference_mode()[source][source]ο
Context manager for optimized inference.
Configures the model for maximum inference performance: - Sets model to eval mode - Disables gradient computation - Uses torch.inference_mode for additional optimizations
The model is restored to its previous state upon exit.
- Return type:
Example
>>> with model.inference_mode(): ... latents = model.tokenize(volume) ... recon = model.detokenize(latents)
- Yields:
self β The model instance for method chaining
- num_parameters()[source][source]ο
Get total number of learnable parameters.
- Return type:
- Returns:
Sum of numel() for all parameters
- tokenize(x)[source][source]ο
Encode input to latent space (convenience method).
Subclasses should override with appropriate return type.
- compile(mode='reduce-overhead', fullgraph=False, **kwargs)[source][source]ο
Compile model with torch.compile() for faster inference.
Uses PyTorch 2.0+ compilation to optimize the model graph. The compiled model maintains the same interface but runs faster, especially for repeated inference calls.
- Parameters:
mode (
str, default:'reduce-overhead') β Compilation mode. Options: - βreduce-overheadβ: Best for small batches (default) - βmax-autotuneβ: Best throughput, longer warmup - βdefaultβ: Balanced compilationfullgraph (
bool, default:False) β If True, require full graph compilation (stricter)**kwargs β Additional arguments passed to torch.compile()
- Return type:
- Returns:
Compiled model (self, for method chaining)
Example
>>> model = ContinuousTokenizer.from_pretrained("path/to/model") >>> model = model.compile(mode="reduce-overhead") >>> # First call triggers compilation (slower) >>> latents = model.tokenize(batch) >>> # Subsequent calls are faster >>> latents = model.tokenize(batch2)
Note
Compilation happens on first forward pass
Different input shapes may trigger recompilation
Use dynamic=False (default) for fixed input sizes
- save_pretrained(save_directory, push_to_hub=False, **kwargs)[source][source]ο
Save model weights and configuration to directory.
Creates a directory structure compatible with from_pretrained():
` save_directory/ βββ config.json # Model configuration βββ pytorch_model.bin # Model weights `- Parameters:
- Return type:
Example
>>> model.save_pretrained("./my-tokenizer") >>> # Later... >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer")
- classmethod from_pretrained(model_name_or_path, map_location=None, **kwargs)[source][source]ο
Load model from local directory or HuggingFace Hub.
Automatically detects whether path is local or a Hub repository. For Hub repos, downloads config and weights to cache.
- Parameters:
- Return type:
- Returns:
Loaded model instance
Example
>>> # From local path >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer") >>> >>> # From HuggingFace Hub >>> model = ContinuousTokenizer.from_pretrained("username/my-tokenizer") >>> >>> # Override config >>> model = ContinuousTokenizer.from_pretrained( ... "./my-tokenizer", ... dropout=0.1 # Override saved dropout value ... )
- push_to_hub(repo_id, save_directory=None, commit_message='Upload model', private=False, **kwargs)[source][source]ο
Upload model to HuggingFace Hub.
Creates or updates a repository on the Hub with model weights and configuration.
- Parameters:
repo_id (
str) β Repository ID (e.g., βusername/model-nameβ)save_directory (
Optional[str], default:None) β Local directory to save before upload (auto-created if None)commit_message (
str, default:'Upload model') β Commit message for the uploadprivate (
bool, default:False) β Whether to create a private repository**kwargs β Additional arguments for HfApi.upload_folder
- Return type:
Example
>>> model.push_to_hub("username/my-vae-tokenizer") >>> # Creates https://huggingface.co/username/my-vae-tokenizer
- load_encoder_decoder_weights(pretrained_path, strict=False, verbose=True)[source][source]ο
Load encoder/decoder weights from a pretrained checkpoint.
This method enables transfer learning by loading encoder and decoder weights from any compatible tokenizer (VAE, VQ-VAE, FSQ, etc.) while leaving quantizer-specific layers randomly initialized.
Use Casesο
Initialize discrete tokenizer from VAE: Load MAISI VAE encoder/decoder, then train only the quantizer layers (VQ, FSQ, etc.)
Fine-tune on new domain: Start from pretrained weights, fine-tune all
Encoder-only transfer: Use pretrained encoder for downstream tasks
How It Worksο
The method identifies encoder/decoder weights by key prefixes and loads only those that match. Quantizer-specific layers (quant_conv, post_quant_conv, quantizer) may or may not be loaded depending on architecture compatibility.
Weight Matching Strategy: - encoder.* keys: Always attempted - decoder.* keys: Always attempted - quant_conv.* keys: Loaded if shapes match - post_quant_conv.* keys: Loaded if shapes match - quantizer.* keys: Skipped (architecture-specific)
- type pretrained_path:
- param pretrained_path:
Path to pretrained weights file (.pt, .bin) or directory containing βpytorch_model.binβ
- type strict:
bool, default:False- param strict:
If True, raise error on shape mismatches. If False (default), skip mismatched weights and log warnings.
- type verbose:
bool, default:True- param verbose:
If True, print summary of loaded/skipped weights
- rtype:
- returns:
Tuple of (loaded_keys, skipped_keys) for inspection
Example
>>> # Initialize FSQ tokenizer from MAISI VAE weights >>> model = DiscreteTokenizer( ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... # ... other args matching MAISI architecture ... ) >>> loaded, skipped = model.load_encoder_decoder_weights( ... 'weights/maisi_converted.pt', ... verbose=True ... ) >>> print(f"Loaded {len(loaded)} keys, skipped {len(skipped)}") >>> >>> # Now train with frozen encoder if desired >>> for param in model.encoder.parameters(): ... param.requires_grad = False
Note
For best results, ensure the pretrained model has the same: - channels, channels_mult, num_res_blocks - spatial_compression
The following can differ: - z_channels, latent_channels, embedding_dim - Quantizer type and configuration
- classmethod from_pretrained_encoder_decoder(pretrained_path, strict=False, verbose=True, **kwargs)[source][source]ο
Create model and load encoder/decoder weights from pretrained checkpoint.
Factory method that creates a new model instance and loads encoder/decoder weights in one step. Useful for initializing new architectures from pretrained backbones.
- Parameters:
- Return type:
- Returns:
New model instance with loaded encoder/decoder weights
Example
>>> # Create FSQ model initialized from VAE weights >>> model = DiscreteTokenizer.from_pretrained_encoder_decoder( ... 'weights/maisi_converted.pt', ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... z_channels=256, ... channels=64, ... channels_mult=(1, 2, 4), ... )
- encode_batch(x, batch_size=8, show_progress=False)[source][source]ο
Encode large batch with automatic mini-batching.
Processes input in chunks to avoid OOM for large datasets.
- Parameters:
- Return type:
- Returns:
Concatenated latents for all inputs
Example
>>> dataset = torch.randn(1000, 1, 64, 64, 64) >>> latents = model.encode_batch(dataset, batch_size=4)
- decode_batch(z, batch_size=8, show_progress=False)[source][source]ο
Decode large batch with automatic mini-batching.
- reconstruct(x, roi_size=None, overlap=0.5, sw_batch_size=1)[source][source]ο
Full encode-decode reconstruction with optional sliding window.
For large 3D volumes that exceed GPU memory, this method implements sliding window inference with Gaussian importance weighting to seamlessly blend overlapping patches.
The Algorithmο
Pad input to multiple of stride + window size
Extract overlapping windows with specified stride
Process windows in batches through tokenize -> detokenize
Weight each windowβs contribution by Gaussian importance
Normalize by accumulated importance and crop to original size
Gaussian Weightingο
Uses a Gaussian importance map (2D or 3D based on input) that gives higher weight to the center of each window, reducing boundary artifacts when blending.
- type x:
- param x:
Input tensor of shape (B, C, H, W, D) for 3D or (B, C, H, W) for 2D
- type roi_size:
- param roi_size:
Size of sliding window. If None, processes entire volume. Can be int (isotropic) or tuple (anisotropic).
- type overlap:
float, default:0.5- param overlap:
Fraction of overlap between windows (0.0 to 0.9). Higher overlap = smoother blending but more compute.
- type sw_batch_size:
int, default:1- param sw_batch_size:
Number of windows to process in parallel per batch. Higher values use more GPU memory but are faster. Default is 1 (sequential processing).
- rtype:
- returns:
Reconstructed tensor with same shape as input
Example
>>> volume = torch.randn(1, 1, 256, 256, 256) # 256Β³ volume >>> # Process in 128Β³ windows with 50% overlap, 4 windows at a time >>> recon = model.reconstruct(volume, roi_size=128, overlap=0.5, sw_batch_size=4)
Note
For volumes that fit in memory, omit roi_size for faster processing
Overlap of 0.5 is a good default; higher values reduce artifacts but increase computation proportionally
sw_batch_size > 1 can significantly speed up inference on GPUs with sufficient memory
- T_destination = ~T_destinationο
- add_module(name, module)[source]ο
Add a child module to the current module.
The module can be accessed as an attribute using the given name.
- apply(fn)[source]ο
Apply
fnrecursively to every submodule (as returned by.children()) as well as self.Typical use includes initializing the parameters of a model (see also torch.nn.init).
- Parameters:
fn (
Module-> None) β function to be applied to each submodule- Returns:
self
- Return type:
Module
Example:
>>> @torch.no_grad() >>> def init_weights(m): >>> print(m) >>> if type(m) is nn.Linear: >>> m.weight.fill_(1.0) >>> print(m.weight) >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )
- bfloat16()[source]ο
Casts all floating point parameters and buffers to
bfloat16datatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- buffers(recurse=True)[source]ο
Return an iterator over module buffers.
- Parameters:
recurse (bool) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module.
- Yields:
torch.Tensor β module buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for buf in model.buffers(): >>> print(type(buf), buf.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- cpu()[source]ο
Move all model parameters and buffers to the CPU.
Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- cuda(device=None)[source]ο
Move all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on GPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- double()[source]ο
Casts all floating point parameters and buffers to
doubledatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- eval()[source]ο
Set the module in evaluation mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e. whether they are affected, e.g.
Dropout,BatchNorm, etc.This is equivalent with
self.train(False).See Locally disabling gradient computation for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns:
self
- Return type:
Module
- extra_repr()[source]ο
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- Return type:
- float()[source]ο
Casts all floating point parameters and buffers to
floatdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- get_buffer(target)[source]ο
Return the buffer given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the buffer to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The buffer referenced by
target- Return type:
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not a buffer
- get_extra_state()[source]ο
Return any extra state to include in the moduleβs state_dict.
Implement this and a corresponding
set_extra_state()for your module if you need to store extra state. This function is called when building the moduleβs state_dict().Note that extra state should be picklable to ensure working serialization of the state_dict. We only provide backwards compatibility guarantees for serializing Tensors; other objects may break backwards compatibility if their serialized pickled form changes.
- Returns:
Any extra state to store in the moduleβs state_dict
- Return type:
- get_parameter(target)[source]ο
Return the parameter given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the Parameter to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The Parameter referenced by
target- Return type:
torch.nn.Parameter
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not an
nn.Parameter
- get_submodule(target)[source]ο
Return the submodule given by
targetif it exists, otherwise throw an error.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(16, 33, kernel_size=(3, 3), stride=(2, 2)) ) (linear): Linear(in_features=100, out_features=200, bias=True) ) )(The diagram shows an
nn.ModuleA.Awhich has a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To check whether or not we have the
linearsubmodule, we would callget_submodule("net_b.linear"). To check whether we have theconvsubmodule, we would callget_submodule("net_b.net_c.conv").The runtime of
get_submoduleis bounded by the degree of module nesting intarget. A query againstnamed_modulesachieves the same result, but it is O(N) in the number of transitive modules. So, for a simple check to see if some submodule exists,get_submoduleshould always be used.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)- Returns:
The submodule referenced by
target- Return type:
- Raises:
AttributeError β If at any point along the path resulting from the target string the (sub)path resolves to a non-existent attribute name or an object that is not an instance of
nn.Module.
- half()[source]ο
Casts all floating point parameters and buffers to
halfdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- ipu(device=None)[source]ο
Move all model parameters and buffers to the IPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on IPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- load_state_dict(state_dict, strict=True, assign=False)[source]ο
Copy parameters and buffers from
state_dictinto this module and its descendants.If
strictisTrue, then the keys ofstate_dictmust exactly match the keys returned by this moduleβsstate_dict()function.Warning
If
assignisTruethe optimizer must be created after the call toload_state_dictunlessget_swap_module_params_on_conversion()isTrue.- Parameters:
state_dict (dict) β a dict containing parameters and persistent buffers.
strict (bool, optional) β whether to strictly enforce that the keys in
state_dictmatch the keys returned by this moduleβsstate_dict()function. Default:Trueassign (bool, optional) β When set to
False, the properties of the tensors in the current module are preserved whereas setting it toTruepreserves properties of the Tensors in the state dict. The only exception is therequires_gradfield ofParameterfor which the value from the module is preserved. Default:False
- Returns:
missing_keysis a list of str containing any keys that are expectedby this module but missing from the provided
state_dict.
unexpected_keysis a list of str containing the keys that are notexpected by this module but present in the provided
state_dict.
- Return type:
NamedTuplewithmissing_keysandunexpected_keysfields
Note
If a parameter or buffer is registered as
Noneand its corresponding key exists instate_dict,load_state_dict()will raise aRuntimeError.
- modules(remove_duplicate=True)[source]ο
Return an iterator over all modules in the network.
- Parameters:
remove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not.- Yields:
Module β a module in the network
- Return type:
Note
Duplicate modules are returned only once by default. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): ... print(idx, '->', m) 0 -> Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) ) 1 -> Linear(in_features=2, out_features=2, bias=True)
- mtia(device=None)[source]ο
Move all model parameters and buffers to the MTIA.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on MTIA while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- named_buffers(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
- Parameters:
prefix (str) β prefix to prepend to all buffer names.
recurse (bool, optional) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module. Defaults to True.
remove_duplicate (bool, optional) β whether to remove the duplicated buffers in the result. Defaults to True.
- Yields:
(str, torch.Tensor) β Tuple containing the name and buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, buf in self.named_buffers(): >>> if name in ['running_var']: >>> print(buf.size())
- named_children()[source]ο
Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
- Yields:
(str, Module) β Tuple containing a name and child module
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module)
- named_modules(memo=None, prefix='', remove_duplicate=True)[source]ο
Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
- Parameters:
memo (
set[Module] |None, default:None) β a memo to store the set of modules already added to the resultprefix (
str, default:'') β a prefix that will be added to the name of the moduleremove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not
- Yields:
(str, Module) β Tuple of name and module
Note
Duplicate modules are returned only once. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): ... print(idx, '->', m) 0 -> ('', Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )) 1 -> ('0', Linear(in_features=2, out_features=2, bias=True))
- named_parameters(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
- Parameters:
prefix (str) β prefix to prepend to all parameter names.
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
remove_duplicate (bool, optional) β whether to remove the duplicated parameters in the result. Defaults to True.
- Yields:
(str, Parameter) β Tuple containing the name and parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size())
- parameters(recurse=True)[source]ο
Return an iterator over module parameters.
This is typically passed to an optimizer.
- Parameters:
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
- Yields:
Parameter β module parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for param in model.parameters(): >>> print(type(param), param.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- register_backward_hook(hook)[source]ο
Register a backward hook on the module.
This function is deprecated in favor of
register_full_backward_hook()and the behavior of this function will change in future versions.
- register_buffer(name, tensor, persistent=True)[source]ο
Add a buffer to the module.
This is typically used to register a buffer that should not be considered a model parameter. For example, BatchNormβs
running_meanis not a parameter, but is part of the moduleβs state. Buffers, by default, are persistent and will be saved alongside parameters. This behavior can be changed by settingpersistenttoFalse. The only difference between a persistent buffer and a non-persistent buffer is that the latter will not be a part of this moduleβsstate_dict.Buffers can be accessed as attributes using given names.
- Parameters:
name (str) β name of the buffer. The buffer can be accessed from this module using the given name
tensor (Tensor or None) β buffer to be registered. If
None, then operations that run on buffers, such ascuda, are ignored. IfNone, the buffer is not included in the moduleβsstate_dict.persistent (bool) β whether the buffer is part of this moduleβs
state_dict.
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> self.register_buffer('running_mean', torch.zeros(num_features))
- register_forward_hook(hook, *, prepend=False, with_kwargs=False, always_call=False)[source]ο
Register a forward hook on the module.
The hook will be called every time after
forward()has computed an output.If
with_kwargsisFalseor not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the output. It can modify the input inplace but it will not have effect on forward since this is called afterforward()is called. The hook should have the following signature:hook(module, args, output) -> None or modified output
If
with_kwargsisTrue, the forward hook will be passed thekwargsgiven to the forward function and be expected to return the output possibly modified. The hook should have the following signature:hook(module, args, kwargs, output) -> None or modified output
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If
True, the providedhookwill be fired before all existingforwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforwardhooks on thistorch.nn.Module. Note that globalforwardhooks registered withregister_module_forward_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If
True, thehookwill be passed the kwargs given to the forward function. Default:Falsealways_call (bool) β If
Truethehookwill be run regardless of whether an exception is raised while calling the Module. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_forward_pre_hook(hook, *, prepend=False, with_kwargs=False)[source]ο
Register a forward pre-hook on the module.
The hook will be called every time before
forward()is invoked.If
with_kwargsis false or not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the input. User can either return a tuple or a single modified value in the hook. We will wrap the value into a tuple if a single value is returned (unless that value is already a tuple). The hook should have the following signature:hook(module, args) -> None or modified input
If
with_kwargsis true, the forward pre-hook will be passed the kwargs given to the forward function. And if the hook modifies the input, both the args and kwargs should be returned. The hook should have the following signature:hook(module, args, kwargs) -> None or a tuple of modified input and kwargs
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingforward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforward_prehooks on thistorch.nn.Module. Note that globalforward_prehooks registered withregister_module_forward_pre_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If true, the
hookwill be passed the kwargs given to the forward function. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_hook(hook, prepend=False)[source]ο
Register a backward hook on the module.
The hook will be called every time the gradients with respect to a module are computed, and its firing rules are as follows:
Ordinarily, the hook fires when the gradients are computed with respect to the module inputs.
If none of the module inputs require gradients, the hook will fire when the gradients are computed with respect to module outputs.
If none of the module outputs require gradients, then the hooks will not fire.
The hook should have the following signature:
hook(module, grad_input, grad_output) -> tuple(Tensor) or None
The
grad_inputandgrad_outputare tuples that contain the gradients with respect to the inputs and outputs respectively. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the input that will be used in place ofgrad_inputin subsequent computations.grad_inputwill only correspond to the inputs given as positional arguments and all kwarg arguments are ignored. Entries ingrad_inputandgrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs or outputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackwardhooks on thistorch.nn.Module. Note that globalbackwardhooks registered withregister_module_full_backward_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_pre_hook(hook, prepend=False)[source]ο
Register a backward pre-hook on the module.
The hook will be called every time the gradients for the module are computed. The hook should have the following signature:
hook(module, grad_output) -> tuple[Tensor, ...], Tensor or None
The
grad_outputis a tuple. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the output that will be used in place ofgrad_outputin subsequent computations. Entries ingrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackward_prehooks on thistorch.nn.Module. Note that globalbackward_prehooks registered withregister_module_full_backward_pre_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_post_hook(hook)[source]ο
Register a post-hook to be run after moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, incompatible_keys) -> None
The
moduleargument is the current module that this hook is registered on, and theincompatible_keysargument is aNamedTupleconsisting of attributesmissing_keysandunexpected_keys.missing_keysis alistofstrcontaining the missing keys andunexpected_keysis alistofstrcontaining the unexpected keys.The given incompatible_keys can be modified inplace if needed.
Note that the checks performed when calling
load_state_dict()withstrict=Trueare affected by modifications the hook makes tomissing_keysorunexpected_keys, as expected. Additions to either set of keys will result in an error being thrown whenstrict=True, and clearing out both missing and unexpected keys will avoid an error.- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_pre_hook(hook)[source]ο
Register a pre-hook to be run before moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) -> None # noqa: B950
- Parameters:
hook (Callable) β Callable hook that will be invoked before loading the state dict.
- register_module(name, module)[source]ο
Alias for
add_module().
- register_parameter(name, param)[source]ο
Add a parameter to the module.
The parameter can be accessed as an attribute using given name.
- Parameters:
name (str) β name of the parameter. The parameter can be accessed from this module using the given name
param (Parameter or None) β parameter to be added to the module. If
None, then operations that run on parameters, such ascuda, are ignored. IfNone, the parameter is not included in the moduleβsstate_dict.
- Return type:
- register_state_dict_post_hook(hook)[source]ο
Register a post-hook for the
state_dict()method.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata) -> None
The registered hooks can modify the
state_dictinplace.
- register_state_dict_pre_hook(hook)[source]ο
Register a pre-hook for the
state_dict()method.- It should have the following signature::
hook(module, prefix, keep_vars) -> None
The registered hooks can be used to perform pre-processing before the
state_dictcall is made.
- requires_grad_(requires_grad=True)[source]ο
Change if autograd should record operations on parameters in this module.
This method sets the parametersβ
requires_gradattributes in-place.This method is helpful for freezing part of the module for finetuning or training parts of a model individually (e.g., GAN training).
See Locally disabling gradient computation for a comparison between .requires_grad_() and several similar mechanisms that may be confused with it.
- Parameters:
requires_grad (bool) β whether autograd should record operations on parameters in this module. Default:
True.- Returns:
self
- Return type:
Module
- set_extra_state(state)[source]ο
Set extra state contained in the loaded state_dict.
This function is called from
load_state_dict()to handle any extra state found within the state_dict. Implement this function and a correspondingget_extra_state()for your module if you need to store extra state within its state_dict.
- set_submodule(target, module, strict=False)[source]ο
Set the submodule given by
targetif it exists, otherwise throw an error.Note
If
strictis set toFalse(default), the method will replace an existing submodule or create a new submodule if the parent module exists. Ifstrictis set toTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule does not exist.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(3, 3, 3) ) (linear): Linear(3, 3) ) )(The diagram shows an
nn.ModuleA.Ahas a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To override the
Conv2dwith a new submoduleLinear, you could callset_submodule("net_b.net_c.conv", nn.Linear(1, 1))wherestrictcould beTrueorFalseTo add a new submodule
Conv2dto the existingnet_bmodule, you would callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1)).In the above if you set
strict=Trueand callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1), strict=True), an AttributeError will be raised becausenet_bdoes not have a submodule namedconv.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)module (
Module) β The module to set the submodule to.strict (
bool, default:False) β IfFalse, the method will replace an existing submodule or create a new submodule if the parent module exists. IfTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule doesnβt already exist.
- Raises:
ValueError β If the
targetstring is empty or ifmoduleis not an instance ofnn.Module.AttributeError β If at any point along the path resulting from the
targetstring the (sub)path resolves to a non-existent attribute name or an object that is not an instance ofnn.Module.
- Return type:
See
torch.Tensor.share_memory_().- Return type:
Self
- state_dict(*args, destination=None, prefix='', keep_vars=False)[source]ο
Return a dictionary containing references to the whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. Parameters and buffers set to
Noneare not included.Note
The returned object is a shallow copy. It contains references to the moduleβs parameters and buffers.
Warning
Currently
state_dict()also accepts positional arguments fordestination,prefixandkeep_varsin order. However, this is being deprecated and keyword arguments will be enforced in future releases.Warning
Please avoid the use of argument
destinationas it is not designed for end-users.- Parameters:
destination (dict, optional) β If provided, the state of module will be updated into the dict and the same object is returned. Otherwise, an
OrderedDictwill be created and returned. Default:None.prefix (str, optional) β a prefix added to parameter and buffer names to compose the keys in state_dict. Default:
''.keep_vars (bool, optional) β by default the
Tensors returned in the state dict are detached from autograd. If itβs set toTrue, detaching will not be performed. Default:False.
- Returns:
a dictionary containing a whole state of the module
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> module.state_dict().keys() ['bias', 'weight']
- to(*args, **kwargs)[source]ο
Move and/or cast the parameters and buffers.
This can be called as
- to(device=None, dtype=None, non_blocking=False)[source]
- to(dtype, non_blocking=False)[source]
- to(tensor, non_blocking=False)[source]
- to(memory_format=torch.channels_last)[source]
Its signature is similar to
torch.Tensor.to(), but only accepts floating point or complexdtypes. In addition, this method will only cast the floating point or complex parameters and buffers todtype(if given). The integral parameters and buffers will be moveddevice, if that is given, but with dtypes unchanged. Whennon_blockingis set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.See below for examples.
Note
This method modifies the module in-place.
- Parameters:
device (
torch.device) β the desired device of the parameters and buffers in this moduledtype (
torch.dtype) β the desired floating point or complex dtype of the parameters and buffers in this moduletensor (torch.Tensor) β Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module
memory_format (
torch.memory_format) β the desired memory format for 4D parameters and buffers in this module (keyword only argument)
- Returns:
self
- Return type:
Module
Examples:
>>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16) >>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble) >>> linear.weight Parameter containing: tensor([[ 0.3741+0.j, 0.2382+0.j], [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128) >>> linear(torch.ones(3, 2, dtype=torch.cdouble)) tensor([[0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
- to_empty(*, device, recurse=True)[source]ο
Move the parameters and buffers to the specified device without copying storage.
- Parameters:
device (
torch.device) β The desired device of the parameters and buffers in this module.recurse (bool) β Whether parameters and buffers of submodules should be recursively moved to the specified device.
- Returns:
self
- Return type:
Module
- train(mode=True)[source]ο
Set the module in training mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e., whether they are affected, e.g.
Dropout,BatchNorm, etc.- Parameters:
mode (bool) β whether to set training mode (
True) or evaluation mode (False). Default:True.- Returns:
self
- Return type:
Module
- type(dst_type)[source]ο
Casts all parameters and buffers to
dst_type.Note
This method modifies the module in-place.
- Parameters:
dst_type (type or string) β the desired type
- Returns:
self
- Return type:
Module
- xpu(device=None)[source]ο
Move all model parameters and buffers to the XPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on XPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- zero_grad(set_to_none=True)[source]ο
Reset gradients of all model parameters.
See similar function under
torch.optim.Optimizerfor more context.- Parameters:
set_to_none (bool) β instead of setting to zero, set the grads to None. See
torch.optim.Optimizer.zero_grad()for details.- Return type:
- class medtokenizers.ContinuousTokenizer(dim, in_channels=1, out_channels=1, z_channels=4, z_factor=None, latent_channels=4, channels=64, channels_mult=(1, 2, 4), num_res_blocks=2, attn_resolutions=(), dropout=0.0, resolution=256, spatial_compression=4, formulation='VAE', use_encoder_mid=False, use_output_nonlinearity=False, decoder_blocks_per_stage=None, separate_quant_conv=True, name='ContinuousTokenizer', **kwargs)[source][source]ο
Bases:
medtokenizers.modules.base.BaseTokenizerContinuous latent tokenizer for medical imaging (VAE/AE).
This tokenizer learns a continuous latent representation using either: - VAE: Variational Autoencoder with KL divergence regularization - AE: Standard Autoencoder without probabilistic modeling
The VAE variant is particularly useful for: - Latent diffusion models (LDM) - Interpolation in latent space - Generative modeling with controllable sampling
Architecture Detailsο
The network follows a symmetric encoder-decoder design:
Encoder Path: Input(H,W,D) -> Conv_in -> ResBlocks -> Downsample -> ... -> Conv_out -> mu, sigma^2 Decoder Path: z -> Conv_in -> ResBlocks -> Upsample -> ... -> Conv_out -> Output(H,W,D)
Key architectural choices: - quant_conv: 1x1 conv reducing encoder output to latent dimension - post_quant_conv: 1x1 conv expanding latent to decoder input - GroupNorm: Batch-size independent normalization - Swish activation: Smooth, non-monotonic activation
Memory Optimizationο
For 3D volumes, the model automatically: - Uses channels_last_3d memory format for better cache efficiency - Supports gradient checkpointing (via use_checkpointing kwarg)
- type dim:
- param dim:
Spatial dimensionality (2 for 2D images, 3 for 3D volumes)
- type in_channels:
int, default:1- param in_channels:
Number of input channels (1 for grayscale, 3 for RGB)
- type out_channels:
int, default:1- param out_channels:
Number of output channels (usually same as in_channels)
- type z_channels:
int, default:4- param z_channels:
Intermediate channels after encoder, before quant_conv
- type z_factor:
- param z_factor:
Multiplier for encoder output channels. Default: 2 for VAE (outputs ΞΌ and ΟΒ²), 1 for AE (outputs z directly)
- type latent_channels:
int, default:4- param latent_channels:
Final latent dimension (e.g., 4 for SD-style VAE)
- type channels:
int, default:64- param channels:
Base channel count (scaled by channels_mult)
- type channels_mult:
- param channels_mult:
Channel multipliers at each resolution level. Example: (1, 2, 4) means channels β 2*channels β 4*channels
- type num_res_blocks:
int, default:2- param num_res_blocks:
Number of residual blocks per resolution level
- type attn_resolutions:
- param attn_resolutions:
Spatial resolutions where self-attention is applied
- type dropout:
float, default:0.0- param dropout:
Dropout probability in residual blocks
- type resolution:
int, default:256- param resolution:
Input spatial resolution (for attention position info)
- type spatial_compression:
int, default:4- param spatial_compression:
Total downsampling factor (e.g., 8 = 3 downsamples)
- type formulation:
Literal['VAE','AE'], default:'VAE'- param formulation:
βVAEβ for variational, βAEβ for deterministic
- type name:
str, default:'ContinuousTokenizer'- param name:
Model identifier for saving/loading
- type **kwargs:
- param **kwargs:
Additional args passed to Encoder/Decoder (e.g., use_checkpointing)
Example
>>> # Create a 3D VAE with 4-channel latent (like Stable Diffusion) >>> model = ContinuousTokenizer( ... dim=3, ... in_channels=1, ... out_channels=1, ... z_channels=128, ... latent_channels=4, ... channels=64, ... channels_mult=(1, 2, 4), ... spatial_compression=8, ... formulation='VAE' ... ) >>> >>> # Forward pass returns dict with reconstructions and KL loss >>> volume = torch.randn(1, 1, 128, 128, 128) >>> output = model(volume) >>> recon = output['reconstructions'] >>> kl_loss = output.get('kl_loss') # Only for VAE >>> >>> # For inference, use tokenize/detokenize >>> with model.inference_mode(): ... latents = model.tokenize(volume) # (1, 4, 16, 16, 16) ... reconstructed = model.detokenize(latents)
References
Kingma & Welling βAuto-Encoding Variational Bayesβ (2013) Rombach et al. βHigh-Resolution Image Synthesis with Latent Diffusion Modelsβ
- param use_encoder_mid:
- type use_encoder_mid:
bool, default:False- param use_output_nonlinearity:
- type use_output_nonlinearity:
bool, default:False- param decoder_blocks_per_stage:
- type decoder_blocks_per_stage:
- param separate_quant_conv:
- type separate_quant_conv:
bool, default:True
- __init__(dim, in_channels=1, out_channels=1, z_channels=4, z_factor=None, latent_channels=4, channels=64, channels_mult=(1, 2, 4), num_res_blocks=2, attn_resolutions=(), dropout=0.0, resolution=256, spatial_compression=4, formulation='VAE', use_encoder_mid=False, use_output_nonlinearity=False, decoder_blocks_per_stage=None, separate_quant_conv=True, name='ContinuousTokenizer', **kwargs)[source][source]ο
- Parameters:
dim (
int)in_channels (
int, default:1)out_channels (
int, default:1)z_channels (
int, default:4)latent_channels (
int, default:4)channels (
int, default:64)num_res_blocks (
int, default:2)dropout (
float, default:0.0)resolution (
int, default:256)spatial_compression (
int, default:4)formulation (
Literal['VAE','AE'], default:'VAE')use_encoder_mid (
bool, default:False)use_output_nonlinearity (
bool, default:False)decoder_blocks_per_stage (
Optional[list[int]], default:None)separate_quant_conv (
bool, default:True)name (
str, default:'ContinuousTokenizer')kwargs (
Any)
- encode(x)[source][source]ο
Encode input to latent representation.
For VAE: - Encoder outputs (ΞΌ, log ΟΒ²) - Samples z using reparameterization: z = ΞΌ + Ο * Ξ΅ - Returns (z, (kl_loss, (mean, logvar)))
For AE: - Encoder outputs z directly - Returns (z, (zero_kl, zero_logvar))
- Parameters:
x (
Float[Tensor, 'batch channels *spatial']) β Input tensor of shape(B, C, *spatial)where: - B: batch size - C: number of channels (must match modelβs in_channels) - spatial: (H, W) for 2D or (H, W, D) for 3D- Returns:
latent: Sampled or deterministic latent tensor
distribution_output: KL loss and posterior parameters
- Return type:
Tuple of
- Raises:
TypeError β If x is not a floating point tensor
ValueError β If x has wrong shape or contains NaN/Inf
- T_destination = ~T_destinationο
- add_module(name, module)[source]ο
Add a child module to the current module.
The module can be accessed as an attribute using the given name.
- apply(fn)[source]ο
Apply
fnrecursively to every submodule (as returned by.children()) as well as self.Typical use includes initializing the parameters of a model (see also torch.nn.init).
- Parameters:
fn (
Module-> None) β function to be applied to each submodule- Returns:
self
- Return type:
Module
Example:
>>> @torch.no_grad() >>> def init_weights(m): >>> print(m) >>> if type(m) is nn.Linear: >>> m.weight.fill_(1.0) >>> print(m.weight) >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )
- bfloat16()[source]ο
Casts all floating point parameters and buffers to
bfloat16datatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- buffers(recurse=True)[source]ο
Return an iterator over module buffers.
- Parameters:
recurse (bool) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module.
- Yields:
torch.Tensor β module buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for buf in model.buffers(): >>> print(type(buf), buf.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- compile(mode='reduce-overhead', fullgraph=False, **kwargs)[source]ο
Compile model with torch.compile() for faster inference.
Uses PyTorch 2.0+ compilation to optimize the model graph. The compiled model maintains the same interface but runs faster, especially for repeated inference calls.
- Parameters:
mode (
str, default:'reduce-overhead') β Compilation mode. Options: - βreduce-overheadβ: Best for small batches (default) - βmax-autotuneβ: Best throughput, longer warmup - βdefaultβ: Balanced compilationfullgraph (
bool, default:False) β If True, require full graph compilation (stricter)**kwargs β Additional arguments passed to torch.compile()
- Return type:
- Returns:
Compiled model (self, for method chaining)
Example
>>> model = ContinuousTokenizer.from_pretrained("path/to/model") >>> model = model.compile(mode="reduce-overhead") >>> # First call triggers compilation (slower) >>> latents = model.tokenize(batch) >>> # Subsequent calls are faster >>> latents = model.tokenize(batch2)
Note
Compilation happens on first forward pass
Different input shapes may trigger recompilation
Use dynamic=False (default) for fixed input sizes
- cpu()[source]ο
Move all model parameters and buffers to the CPU.
Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- cuda(device=None)[source]ο
Move all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on GPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- decode(z)[source][source]ο
Decode latent representation to output.
- Parameters:
z (
Float[Tensor, 'batch latent_channels *spatial_compressed']) β Latent tensor from encode() or external source. Shape:(B, latent_channels, *spatial_compressed)- Return type:
Float[Tensor, 'batch channels *spatial']- Returns:
Reconstructed output with original spatial dimensions
- Raises:
TypeError β If z is not a floating point tensor
ValueError β If z has wrong shape or contains NaN/Inf
- decode_batch(z, batch_size=8, show_progress=False)[source]ο
Decode large batch with automatic mini-batching.
- double()[source]ο
Casts all floating point parameters and buffers to
doubledatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- encode_batch(x, batch_size=8, show_progress=False)[source]ο
Encode large batch with automatic mini-batching.
Processes input in chunks to avoid OOM for large datasets.
- Parameters:
- Return type:
- Returns:
Concatenated latents for all inputs
Example
>>> dataset = torch.randn(1000, 1, 64, 64, 64) >>> latents = model.encode_batch(dataset, batch_size=4)
- eval()[source]ο
Set the module in evaluation mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e. whether they are affected, e.g.
Dropout,BatchNorm, etc.This is equivalent with
self.train(False).See Locally disabling gradient computation for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns:
self
- Return type:
Module
- extra_repr()[source]ο
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- Return type:
- float()[source]ο
Casts all floating point parameters and buffers to
floatdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- classmethod from_pretrained(model_name_or_path, map_location=None, **kwargs)[source]ο
Load model from local directory or HuggingFace Hub.
Automatically detects whether path is local or a Hub repository. For Hub repos, downloads config and weights to cache.
- Parameters:
- Return type:
- Returns:
Loaded model instance
Example
>>> # From local path >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer") >>> >>> # From HuggingFace Hub >>> model = ContinuousTokenizer.from_pretrained("username/my-tokenizer") >>> >>> # Override config >>> model = ContinuousTokenizer.from_pretrained( ... "./my-tokenizer", ... dropout=0.1 # Override saved dropout value ... )
- classmethod from_pretrained_encoder_decoder(pretrained_path, strict=False, verbose=True, **kwargs)[source]ο
Create model and load encoder/decoder weights from pretrained checkpoint.
Factory method that creates a new model instance and loads encoder/decoder weights in one step. Useful for initializing new architectures from pretrained backbones.
- Parameters:
- Return type:
- Returns:
New model instance with loaded encoder/decoder weights
Example
>>> # Create FSQ model initialized from VAE weights >>> model = DiscreteTokenizer.from_pretrained_encoder_decoder( ... 'weights/maisi_converted.pt', ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... z_channels=256, ... channels=64, ... channels_mult=(1, 2, 4), ... )
- get_buffer(target)[source]ο
Return the buffer given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the buffer to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The buffer referenced by
target- Return type:
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not a buffer
- get_extra_state()[source]ο
Return any extra state to include in the moduleβs state_dict.
Implement this and a corresponding
set_extra_state()for your module if you need to store extra state. This function is called when building the moduleβs state_dict().Note that extra state should be picklable to ensure working serialization of the state_dict. We only provide backwards compatibility guarantees for serializing Tensors; other objects may break backwards compatibility if their serialized pickled form changes.
- Returns:
Any extra state to store in the moduleβs state_dict
- Return type:
- get_parameter(target)[source]ο
Return the parameter given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the Parameter to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The Parameter referenced by
target- Return type:
torch.nn.Parameter
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not an
nn.Parameter
- get_submodule(target)[source]ο
Return the submodule given by
targetif it exists, otherwise throw an error.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(16, 33, kernel_size=(3, 3), stride=(2, 2)) ) (linear): Linear(in_features=100, out_features=200, bias=True) ) )(The diagram shows an
nn.ModuleA.Awhich has a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To check whether or not we have the
linearsubmodule, we would callget_submodule("net_b.linear"). To check whether we have theconvsubmodule, we would callget_submodule("net_b.net_c.conv").The runtime of
get_submoduleis bounded by the degree of module nesting intarget. A query againstnamed_modulesachieves the same result, but it is O(N) in the number of transitive modules. So, for a simple check to see if some submodule exists,get_submoduleshould always be used.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)- Returns:
The submodule referenced by
target- Return type:
- Raises:
AttributeError β If at any point along the path resulting from the target string the (sub)path resolves to a non-existent attribute name or an object that is not an instance of
nn.Module.
- half()[source]ο
Casts all floating point parameters and buffers to
halfdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- inference_mode()[source]ο
Context manager for optimized inference.
Configures the model for maximum inference performance: - Sets model to eval mode - Disables gradient computation - Uses torch.inference_mode for additional optimizations
The model is restored to its previous state upon exit.
- Return type:
Example
>>> with model.inference_mode(): ... latents = model.tokenize(volume) ... recon = model.detokenize(latents)
- Yields:
self β The model instance for method chaining
- ipu(device=None)[source]ο
Move all model parameters and buffers to the IPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on IPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- load_encoder_decoder_weights(pretrained_path, strict=False, verbose=True)[source]ο
Load encoder/decoder weights from a pretrained checkpoint.
This method enables transfer learning by loading encoder and decoder weights from any compatible tokenizer (VAE, VQ-VAE, FSQ, etc.) while leaving quantizer-specific layers randomly initialized.
Use Casesο
Initialize discrete tokenizer from VAE: Load MAISI VAE encoder/decoder, then train only the quantizer layers (VQ, FSQ, etc.)
Fine-tune on new domain: Start from pretrained weights, fine-tune all
Encoder-only transfer: Use pretrained encoder for downstream tasks
How It Worksο
The method identifies encoder/decoder weights by key prefixes and loads only those that match. Quantizer-specific layers (quant_conv, post_quant_conv, quantizer) may or may not be loaded depending on architecture compatibility.
Weight Matching Strategy: - encoder.* keys: Always attempted - decoder.* keys: Always attempted - quant_conv.* keys: Loaded if shapes match - post_quant_conv.* keys: Loaded if shapes match - quantizer.* keys: Skipped (architecture-specific)
- type pretrained_path:
- param pretrained_path:
Path to pretrained weights file (.pt, .bin) or directory containing βpytorch_model.binβ
- type strict:
bool, default:False- param strict:
If True, raise error on shape mismatches. If False (default), skip mismatched weights and log warnings.
- type verbose:
bool, default:True- param verbose:
If True, print summary of loaded/skipped weights
- rtype:
- returns:
Tuple of (loaded_keys, skipped_keys) for inspection
Example
>>> # Initialize FSQ tokenizer from MAISI VAE weights >>> model = DiscreteTokenizer( ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... # ... other args matching MAISI architecture ... ) >>> loaded, skipped = model.load_encoder_decoder_weights( ... 'weights/maisi_converted.pt', ... verbose=True ... ) >>> print(f"Loaded {len(loaded)} keys, skipped {len(skipped)}") >>> >>> # Now train with frozen encoder if desired >>> for param in model.encoder.parameters(): ... param.requires_grad = False
Note
For best results, ensure the pretrained model has the same: - channels, channels_mult, num_res_blocks - spatial_compression
The following can differ: - z_channels, latent_channels, embedding_dim - Quantizer type and configuration
- load_state_dict(state_dict, strict=True, assign=False)[source]ο
Copy parameters and buffers from
state_dictinto this module and its descendants.If
strictisTrue, then the keys ofstate_dictmust exactly match the keys returned by this moduleβsstate_dict()function.Warning
If
assignisTruethe optimizer must be created after the call toload_state_dictunlessget_swap_module_params_on_conversion()isTrue.- Parameters:
state_dict (dict) β a dict containing parameters and persistent buffers.
strict (bool, optional) β whether to strictly enforce that the keys in
state_dictmatch the keys returned by this moduleβsstate_dict()function. Default:Trueassign (bool, optional) β When set to
False, the properties of the tensors in the current module are preserved whereas setting it toTruepreserves properties of the Tensors in the state dict. The only exception is therequires_gradfield ofParameterfor which the value from the module is preserved. Default:False
- Returns:
missing_keysis a list of str containing any keys that are expectedby this module but missing from the provided
state_dict.
unexpected_keysis a list of str containing the keys that are notexpected by this module but present in the provided
state_dict.
- Return type:
NamedTuplewithmissing_keysandunexpected_keysfields
Note
If a parameter or buffer is registered as
Noneand its corresponding key exists instate_dict,load_state_dict()will raise aRuntimeError.
- modules(remove_duplicate=True)[source]ο
Return an iterator over all modules in the network.
- Parameters:
remove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not.- Yields:
Module β a module in the network
- Return type:
Note
Duplicate modules are returned only once by default. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): ... print(idx, '->', m) 0 -> Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) ) 1 -> Linear(in_features=2, out_features=2, bias=True)
- mtia(device=None)[source]ο
Move all model parameters and buffers to the MTIA.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on MTIA while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- named_buffers(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
- Parameters:
prefix (str) β prefix to prepend to all buffer names.
recurse (bool, optional) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module. Defaults to True.
remove_duplicate (bool, optional) β whether to remove the duplicated buffers in the result. Defaults to True.
- Yields:
(str, torch.Tensor) β Tuple containing the name and buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, buf in self.named_buffers(): >>> if name in ['running_var']: >>> print(buf.size())
- named_children()[source]ο
Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
- Yields:
(str, Module) β Tuple containing a name and child module
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module)
- named_modules(memo=None, prefix='', remove_duplicate=True)[source]ο
Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
- Parameters:
memo (
set[Module] |None, default:None) β a memo to store the set of modules already added to the resultprefix (
str, default:'') β a prefix that will be added to the name of the moduleremove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not
- Yields:
(str, Module) β Tuple of name and module
Note
Duplicate modules are returned only once. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): ... print(idx, '->', m) 0 -> ('', Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )) 1 -> ('0', Linear(in_features=2, out_features=2, bias=True))
- named_parameters(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
- Parameters:
prefix (str) β prefix to prepend to all parameter names.
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
remove_duplicate (bool, optional) β whether to remove the duplicated parameters in the result. Defaults to True.
- Yields:
(str, Parameter) β Tuple containing the name and parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size())
- num_parameters()[source]ο
Get total number of learnable parameters.
- Return type:
- Returns:
Sum of numel() for all parameters
- parameters(recurse=True)[source]ο
Return an iterator over module parameters.
This is typically passed to an optimizer.
- Parameters:
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
- Yields:
Parameter β module parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for param in model.parameters(): >>> print(type(param), param.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- push_to_hub(repo_id, save_directory=None, commit_message='Upload model', private=False, **kwargs)[source]ο
Upload model to HuggingFace Hub.
Creates or updates a repository on the Hub with model weights and configuration.
- Parameters:
repo_id (
str) β Repository ID (e.g., βusername/model-nameβ)save_directory (
Optional[str], default:None) β Local directory to save before upload (auto-created if None)commit_message (
str, default:'Upload model') β Commit message for the uploadprivate (
bool, default:False) β Whether to create a private repository**kwargs β Additional arguments for HfApi.upload_folder
- Return type:
Example
>>> model.push_to_hub("username/my-vae-tokenizer") >>> # Creates https://huggingface.co/username/my-vae-tokenizer
- reconstruct(x, roi_size=None, overlap=0.5, sw_batch_size=1)[source]ο
Full encode-decode reconstruction with optional sliding window.
For large 3D volumes that exceed GPU memory, this method implements sliding window inference with Gaussian importance weighting to seamlessly blend overlapping patches.
The Algorithmο
Pad input to multiple of stride + window size
Extract overlapping windows with specified stride
Process windows in batches through tokenize -> detokenize
Weight each windowβs contribution by Gaussian importance
Normalize by accumulated importance and crop to original size
Gaussian Weightingο
Uses a Gaussian importance map (2D or 3D based on input) that gives higher weight to the center of each window, reducing boundary artifacts when blending.
- type x:
- param x:
Input tensor of shape (B, C, H, W, D) for 3D or (B, C, H, W) for 2D
- type roi_size:
- param roi_size:
Size of sliding window. If None, processes entire volume. Can be int (isotropic) or tuple (anisotropic).
- type overlap:
float, default:0.5- param overlap:
Fraction of overlap between windows (0.0 to 0.9). Higher overlap = smoother blending but more compute.
- type sw_batch_size:
int, default:1- param sw_batch_size:
Number of windows to process in parallel per batch. Higher values use more GPU memory but are faster. Default is 1 (sequential processing).
- rtype:
- returns:
Reconstructed tensor with same shape as input
Example
>>> volume = torch.randn(1, 1, 256, 256, 256) # 256Β³ volume >>> # Process in 128Β³ windows with 50% overlap, 4 windows at a time >>> recon = model.reconstruct(volume, roi_size=128, overlap=0.5, sw_batch_size=4)
Note
For volumes that fit in memory, omit roi_size for faster processing
Overlap of 0.5 is a good default; higher values reduce artifacts but increase computation proportionally
sw_batch_size > 1 can significantly speed up inference on GPUs with sufficient memory
- register_backward_hook(hook)[source]ο
Register a backward hook on the module.
This function is deprecated in favor of
register_full_backward_hook()and the behavior of this function will change in future versions.
- register_buffer(name, tensor, persistent=True)[source]ο
Add a buffer to the module.
This is typically used to register a buffer that should not be considered a model parameter. For example, BatchNormβs
running_meanis not a parameter, but is part of the moduleβs state. Buffers, by default, are persistent and will be saved alongside parameters. This behavior can be changed by settingpersistenttoFalse. The only difference between a persistent buffer and a non-persistent buffer is that the latter will not be a part of this moduleβsstate_dict.Buffers can be accessed as attributes using given names.
- Parameters:
name (str) β name of the buffer. The buffer can be accessed from this module using the given name
tensor (Tensor or None) β buffer to be registered. If
None, then operations that run on buffers, such ascuda, are ignored. IfNone, the buffer is not included in the moduleβsstate_dict.persistent (bool) β whether the buffer is part of this moduleβs
state_dict.
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> self.register_buffer('running_mean', torch.zeros(num_features))
- register_forward_hook(hook, *, prepend=False, with_kwargs=False, always_call=False)[source]ο
Register a forward hook on the module.
The hook will be called every time after
forward()has computed an output.If
with_kwargsisFalseor not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the output. It can modify the input inplace but it will not have effect on forward since this is called afterforward()is called. The hook should have the following signature:hook(module, args, output) -> None or modified output
If
with_kwargsisTrue, the forward hook will be passed thekwargsgiven to the forward function and be expected to return the output possibly modified. The hook should have the following signature:hook(module, args, kwargs, output) -> None or modified output
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If
True, the providedhookwill be fired before all existingforwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforwardhooks on thistorch.nn.Module. Note that globalforwardhooks registered withregister_module_forward_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If
True, thehookwill be passed the kwargs given to the forward function. Default:Falsealways_call (bool) β If
Truethehookwill be run regardless of whether an exception is raised while calling the Module. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_forward_pre_hook(hook, *, prepend=False, with_kwargs=False)[source]ο
Register a forward pre-hook on the module.
The hook will be called every time before
forward()is invoked.If
with_kwargsis false or not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the input. User can either return a tuple or a single modified value in the hook. We will wrap the value into a tuple if a single value is returned (unless that value is already a tuple). The hook should have the following signature:hook(module, args) -> None or modified input
If
with_kwargsis true, the forward pre-hook will be passed the kwargs given to the forward function. And if the hook modifies the input, both the args and kwargs should be returned. The hook should have the following signature:hook(module, args, kwargs) -> None or a tuple of modified input and kwargs
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingforward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforward_prehooks on thistorch.nn.Module. Note that globalforward_prehooks registered withregister_module_forward_pre_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If true, the
hookwill be passed the kwargs given to the forward function. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_hook(hook, prepend=False)[source]ο
Register a backward hook on the module.
The hook will be called every time the gradients with respect to a module are computed, and its firing rules are as follows:
Ordinarily, the hook fires when the gradients are computed with respect to the module inputs.
If none of the module inputs require gradients, the hook will fire when the gradients are computed with respect to module outputs.
If none of the module outputs require gradients, then the hooks will not fire.
The hook should have the following signature:
hook(module, grad_input, grad_output) -> tuple(Tensor) or None
The
grad_inputandgrad_outputare tuples that contain the gradients with respect to the inputs and outputs respectively. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the input that will be used in place ofgrad_inputin subsequent computations.grad_inputwill only correspond to the inputs given as positional arguments and all kwarg arguments are ignored. Entries ingrad_inputandgrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs or outputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackwardhooks on thistorch.nn.Module. Note that globalbackwardhooks registered withregister_module_full_backward_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_pre_hook(hook, prepend=False)[source]ο
Register a backward pre-hook on the module.
The hook will be called every time the gradients for the module are computed. The hook should have the following signature:
hook(module, grad_output) -> tuple[Tensor, ...], Tensor or None
The
grad_outputis a tuple. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the output that will be used in place ofgrad_outputin subsequent computations. Entries ingrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackward_prehooks on thistorch.nn.Module. Note that globalbackward_prehooks registered withregister_module_full_backward_pre_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_post_hook(hook)[source]ο
Register a post-hook to be run after moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, incompatible_keys) -> None
The
moduleargument is the current module that this hook is registered on, and theincompatible_keysargument is aNamedTupleconsisting of attributesmissing_keysandunexpected_keys.missing_keysis alistofstrcontaining the missing keys andunexpected_keysis alistofstrcontaining the unexpected keys.The given incompatible_keys can be modified inplace if needed.
Note that the checks performed when calling
load_state_dict()withstrict=Trueare affected by modifications the hook makes tomissing_keysorunexpected_keys, as expected. Additions to either set of keys will result in an error being thrown whenstrict=True, and clearing out both missing and unexpected keys will avoid an error.- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_pre_hook(hook)[source]ο
Register a pre-hook to be run before moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) -> None # noqa: B950
- Parameters:
hook (Callable) β Callable hook that will be invoked before loading the state dict.
- register_module(name, module)[source]ο
Alias for
add_module().
- register_parameter(name, param)[source]ο
Add a parameter to the module.
The parameter can be accessed as an attribute using given name.
- Parameters:
name (str) β name of the parameter. The parameter can be accessed from this module using the given name
param (Parameter or None) β parameter to be added to the module. If
None, then operations that run on parameters, such ascuda, are ignored. IfNone, the parameter is not included in the moduleβsstate_dict.
- Return type:
- register_state_dict_post_hook(hook)[source]ο
Register a post-hook for the
state_dict()method.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata) -> None
The registered hooks can modify the
state_dictinplace.
- register_state_dict_pre_hook(hook)[source]ο
Register a pre-hook for the
state_dict()method.- It should have the following signature::
hook(module, prefix, keep_vars) -> None
The registered hooks can be used to perform pre-processing before the
state_dictcall is made.
- requires_grad_(requires_grad=True)[source]ο
Change if autograd should record operations on parameters in this module.
This method sets the parametersβ
requires_gradattributes in-place.This method is helpful for freezing part of the module for finetuning or training parts of a model individually (e.g., GAN training).
See Locally disabling gradient computation for a comparison between .requires_grad_() and several similar mechanisms that may be confused with it.
- Parameters:
requires_grad (bool) β whether autograd should record operations on parameters in this module. Default:
True.- Returns:
self
- Return type:
Module
- save_pretrained(save_directory, push_to_hub=False, **kwargs)[source]ο
Save model weights and configuration to directory.
Creates a directory structure compatible with from_pretrained():
` save_directory/ βββ config.json # Model configuration βββ pytorch_model.bin # Model weights `- Parameters:
- Return type:
Example
>>> model.save_pretrained("./my-tokenizer") >>> # Later... >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer")
- set_extra_state(state)[source]ο
Set extra state contained in the loaded state_dict.
This function is called from
load_state_dict()to handle any extra state found within the state_dict. Implement this function and a correspondingget_extra_state()for your module if you need to store extra state within its state_dict.
- set_submodule(target, module, strict=False)[source]ο
Set the submodule given by
targetif it exists, otherwise throw an error.Note
If
strictis set toFalse(default), the method will replace an existing submodule or create a new submodule if the parent module exists. Ifstrictis set toTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule does not exist.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(3, 3, 3) ) (linear): Linear(3, 3) ) )(The diagram shows an
nn.ModuleA.Ahas a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To override the
Conv2dwith a new submoduleLinear, you could callset_submodule("net_b.net_c.conv", nn.Linear(1, 1))wherestrictcould beTrueorFalseTo add a new submodule
Conv2dto the existingnet_bmodule, you would callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1)).In the above if you set
strict=Trueand callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1), strict=True), an AttributeError will be raised becausenet_bdoes not have a submodule namedconv.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)module (
Module) β The module to set the submodule to.strict (
bool, default:False) β IfFalse, the method will replace an existing submodule or create a new submodule if the parent module exists. IfTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule doesnβt already exist.
- Raises:
ValueError β If the
targetstring is empty or ifmoduleis not an instance ofnn.Module.AttributeError β If at any point along the path resulting from the
targetstring the (sub)path resolves to a non-existent attribute name or an object that is not an instance ofnn.Module.
- Return type:
See
torch.Tensor.share_memory_().- Return type:
Self
- state_dict(*args, destination=None, prefix='', keep_vars=False)[source]ο
Return a dictionary containing references to the whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. Parameters and buffers set to
Noneare not included.Note
The returned object is a shallow copy. It contains references to the moduleβs parameters and buffers.
Warning
Currently
state_dict()also accepts positional arguments fordestination,prefixandkeep_varsin order. However, this is being deprecated and keyword arguments will be enforced in future releases.Warning
Please avoid the use of argument
destinationas it is not designed for end-users.- Parameters:
destination (dict, optional) β If provided, the state of module will be updated into the dict and the same object is returned. Otherwise, an
OrderedDictwill be created and returned. Default:None.prefix (str, optional) β a prefix added to parameter and buffer names to compose the keys in state_dict. Default:
''.keep_vars (bool, optional) β by default the
Tensors returned in the state dict are detached from autograd. If itβs set toTrue, detaching will not be performed. Default:False.
- Returns:
a dictionary containing a whole state of the module
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> module.state_dict().keys() ['bias', 'weight']
- to(*args, **kwargs)[source]ο
Move and/or cast the parameters and buffers.
This can be called as
- to(device=None, dtype=None, non_blocking=False)[source]
- to(dtype, non_blocking=False)[source]
- to(tensor, non_blocking=False)[source]
- to(memory_format=torch.channels_last)[source]
Its signature is similar to
torch.Tensor.to(), but only accepts floating point or complexdtypes. In addition, this method will only cast the floating point or complex parameters and buffers todtype(if given). The integral parameters and buffers will be moveddevice, if that is given, but with dtypes unchanged. Whennon_blockingis set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.See below for examples.
Note
This method modifies the module in-place.
- Parameters:
device (
torch.device) β the desired device of the parameters and buffers in this moduledtype (
torch.dtype) β the desired floating point or complex dtype of the parameters and buffers in this moduletensor (torch.Tensor) β Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module
memory_format (
torch.memory_format) β the desired memory format for 4D parameters and buffers in this module (keyword only argument)
- Returns:
self
- Return type:
Module
Examples:
>>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16) >>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble) >>> linear.weight Parameter containing: tensor([[ 0.3741+0.j, 0.2382+0.j], [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128) >>> linear(torch.ones(3, 2, dtype=torch.cdouble)) tensor([[0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
- to_empty(*, device, recurse=True)[source]ο
Move the parameters and buffers to the specified device without copying storage.
- Parameters:
device (
torch.device) β The desired device of the parameters and buffers in this module.recurse (bool) β Whether parameters and buffers of submodules should be recursively moved to the specified device.
- Returns:
self
- Return type:
Module
- train(mode=True)[source]ο
Set the module in training mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e., whether they are affected, e.g.
Dropout,BatchNorm, etc.- Parameters:
mode (bool) β whether to set training mode (
True) or evaluation mode (False). Default:True.- Returns:
self
- Return type:
Module
- type(dst_type)[source]ο
Casts all parameters and buffers to
dst_type.Note
This method modifies the module in-place.
- Parameters:
dst_type (type or string) β the desired type
- Returns:
self
- Return type:
Module
- xpu(device=None)[source]ο
Move all model parameters and buffers to the XPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on XPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- zero_grad(set_to_none=True)[source]ο
Reset gradients of all model parameters.
See similar function under
torch.optim.Optimizerfor more context.- Parameters:
set_to_none (bool) β instead of setting to zero, set the grads to None. See
torch.optim.Optimizer.zero_grad()for details.- Return type:
- forward(input)[source][source]ο
Full forward pass: encode -> decode.
During training, returns a dict with all outputs for loss computation. During evaluation, returns a NetworkEval namedtuple.
- Parameters:
input (
Float[Tensor, 'batch channels *spatial']) β Input tensor- Returns:
- βreconstructionsβ: Decoded output
βposteriorsβ: (mean, logvar) for VAE, or identity for AE
βlatentβ/βlatentsβ: Sampled latent tensor
βkl_lossβ: KL divergence (VAE only)
- Eval mode (NetworkEval):
reconstructions: Decoded output
posteriors: Distribution parameters
latent: Sampled latent
- Return type:
Training mode (dict)
- tokenize(x)[source][source]ο
Encode input to latent representation (convenience method).
For inference, this is the primary encoding method. For VAE, returns the sampled latent (not the mean), enabling diverse reconstructions.
- Parameters:
x (
Float[Tensor, 'batch channels *spatial']) β Input tensor- Return type:
Float[Tensor, 'batch latent_channels *spatial_compressed']- Returns:
Latent tensor suitable for storage, manipulation, or decoding
- class medtokenizers.DiscreteTokenizer(dim, in_channels=1, out_channels=1, z_channels=4, embedding_dim=6, channels=64, channels_mult=(1, 2, 4), num_res_blocks=2, attn_resolutions=(), dropout=0.0, resolution=256, spatial_compression=4, quantizer='RESFSQ', use_encoder_mid=False, use_output_nonlinearity=False, decoder_blocks_per_stage=None, num_embeddings=1024, beta=0.25, use_norm=False, use_ema=False, ema_decay=0.99, levels=None, num_codebooks=1, codebook_size=None, codebook_dim=None, entropy_loss_weight=0.1, commitment_loss_weight=0.25, quant_temp=0.01, name='DiscreteTokenizer', **kwargs)[source][source]ο
Bases:
medtokenizers.modules.base.BaseTokenizerDiscrete latent tokenizer for medical imaging.
This tokenizer learns discrete latent representations using various quantization methods, enabling the use of language models and autoregressive architectures for medical image generation.
The Key Insightο
By quantizing continuous encoder outputs to a finite vocabulary, we convert the image generation problem into a sequence modeling problem that can leverage powerful transformer architectures.
Quantization Methodsο
Choose based on your use case:
VQ: Maximum expressiveness, but requires careful training to avoid codebook collapse. Best for small codebooks (~1K).
FSQ: Stable training with implicit codebook. No collapse. Good default choice for most applications.
RESFSQ: Massive effective codebook via residual stacking. Use when you need very high fidelity reconstruction.
LFQ: Binary codes for extreme simplicity. Good for very large-scale generation with lightweight decoders.
Architecture Detailsο
Encoder Path: Input -> Conv_in -> ResBlocks -> Downsample -> Conv_out -> z_continuous Quantization: z_continuous -> quant_conv -> Quantizer -> (indices, z_quantized) Decoder Path: z_quantized -> post_quant_conv -> ResBlocks -> Upsample -> Output
Memory Optimizationο
For 3D volumes, the model automatically: - Uses channels_last_3d memory format - Supports gradient checkpointing
- type dim:
- param dim:
Spatial dimensionality (2 for 2D, 3 for 3D)
- type in_channels:
int, default:1- param in_channels:
Number of input channels
- type out_channels:
int, default:1- param out_channels:
Number of output channels
- type z_channels:
int, default:4- param z_channels:
Encoder output channels (before quant_conv)
- type embedding_dim:
int, default:6- param embedding_dim:
Dimension of quantized embeddings
- type channels:
int, default:64- param channels:
Base channel count for encoder/decoder
- type channels_mult:
- param channels_mult:
Channel multipliers per resolution
- type num_res_blocks:
int, default:2- param num_res_blocks:
Residual blocks per resolution
- type attn_resolutions:
- param attn_resolutions:
Resolutions for self-attention
- type dropout:
float, default:0.0- param dropout:
Dropout probability
- type resolution:
int, default:256- param resolution:
Input spatial resolution
- type spatial_compression:
int, default:4- param spatial_compression:
Total downsampling factor
- type quantizer:
Literal['VQ','FSQ','LFQ','RESFSQ'], default:'RESFSQ'- param quantizer:
Quantization method (βVQβ, βFSQβ, βLFQβ, βRESFSQβ)
- type num_embeddings:
int, default:1024- param num_embeddings:
Codebook size for VQ (default: 1024)
- type beta:
float, default:0.25- param beta:
Commitment loss weight for VQ (default: 0.25)
- type use_norm:
bool, default:False- param use_norm:
Normalize VQ embeddings (cosine similarity)
- type levels:
- param levels:
FSQ quantization levels (e.g., [8, 5, 5, 5])
- type num_codebooks:
int, default:1- param num_codebooks:
Number of quantizers for RESFSQ/LFQ
- type codebook_size:
- param codebook_size:
LFQ codebook size (must be power of 2)
- type codebook_dim:
- param codebook_dim:
LFQ code dimension
- type entropy_loss_weight:
float, default:0.1- param entropy_loss_weight:
LFQ entropy regularization weight
- type commitment_loss_weight:
float, default:0.25- param commitment_loss_weight:
LFQ commitment loss weight
- type quant_temp:
float, default:0.01- param quant_temp:
Temperature for soft quantization
- type name:
str, default:'DiscreteTokenizer'- param name:
Model identifier
- type **kwargs:
- param **kwargs:
Additional encoder/decoder arguments
Example
>>> # FSQ tokenizer for 3D medical volumes >>> model = DiscreteTokenizer( ... dim=3, ... in_channels=1, ... out_channels=1, ... z_channels=128, ... embedding_dim=6, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], # 1000 codes ... spatial_compression=8, ... ) >>> >>> # Tokenize to discrete codes >>> volume = torch.randn(1, 1, 128, 128, 128) >>> with model.inference_mode(): ... indices = model.tokenize(volume) # (1, 16, 16, 16) ... reconstructed = model.detokenize(indices) >>> >>> # Training forward pass >>> output = model(volume) >>> recon_loss = F.l1_loss(output['reconstructions'], volume) >>> quant_loss = output['quant_loss'].mean() >>> total_loss = recon_loss + quant_loss
References
van den Oord et al. βNeural Discrete Representation Learningβ (VQ-VAE) Mentzer et al. βFinite Scalar Quantization: VQ-VAE Made Simpleβ (FSQ) Yu et al. βLanguage Model Beats Diffusionβ (LFQ in MagViT-2)
- param use_encoder_mid:
- type use_encoder_mid:
bool, default:False- param use_output_nonlinearity:
- type use_output_nonlinearity:
bool, default:False- param decoder_blocks_per_stage:
- type decoder_blocks_per_stage:
- param use_ema:
- type use_ema:
bool, default:False- param ema_decay:
- type ema_decay:
float, default:0.99
- __init__(dim, in_channels=1, out_channels=1, z_channels=4, embedding_dim=6, channels=64, channels_mult=(1, 2, 4), num_res_blocks=2, attn_resolutions=(), dropout=0.0, resolution=256, spatial_compression=4, quantizer='RESFSQ', use_encoder_mid=False, use_output_nonlinearity=False, decoder_blocks_per_stage=None, num_embeddings=1024, beta=0.25, use_norm=False, use_ema=False, ema_decay=0.99, levels=None, num_codebooks=1, codebook_size=None, codebook_dim=None, entropy_loss_weight=0.1, commitment_loss_weight=0.25, quant_temp=0.01, name='DiscreteTokenizer', **kwargs)[source][source]ο
- Parameters:
dim (
int)in_channels (
int, default:1)out_channels (
int, default:1)z_channels (
int, default:4)embedding_dim (
int, default:6)channels (
int, default:64)num_res_blocks (
int, default:2)dropout (
float, default:0.0)resolution (
int, default:256)spatial_compression (
int, default:4)quantizer (
Literal['VQ','FSQ','LFQ','RESFSQ'], default:'RESFSQ')use_encoder_mid (
bool, default:False)use_output_nonlinearity (
bool, default:False)decoder_blocks_per_stage (
Optional[list[int]], default:None)num_embeddings (
int, default:1024)beta (
float, default:0.25)use_norm (
bool, default:False)use_ema (
bool, default:False)ema_decay (
float, default:0.99)num_codebooks (
int, default:1)entropy_loss_weight (
float, default:0.1)commitment_loss_weight (
float, default:0.25)quant_temp (
float, default:0.01)name (
str, default:'DiscreteTokenizer')kwargs (
Any)
- to(*args, **kwargs)[source][source]ο
Move and/or cast the model, keeping the quantizer dtype in sync.
The quantizer keeps its own
dtypeattribute (used by its numerical guards). It is updated only when a dtype is actually supplied, so a plain device move such asmodel.to("cuda")no longer silently resets it tofloat32. A dtype may be passed either positionally (model.to(torch.float16)) or as thedtypekeyword.- Parameters:
*args β Positional arguments forwarded to
torch.nn.Module.to().**kwargs β Keyword arguments forwarded to
torch.nn.Module.to().
- Return type:
- Returns:
self, after the move/cast has been applied.
- encode(x)[source][source]ο
Encode input to discrete codes.
Passes input through encoder, projects to embedding dimension, then quantizes to discrete codebook indices.
- Parameters:
x (
Float[Tensor, 'batch channels *spatial']) β Input tensor of shape(B, C, *spatial)where: - B: batch size - C: number of channels (must match modelβs in_channels) - spatial: (H, W) for 2D or (H, W, D) for 3D- Returns:
indices: Discrete codebook indices
quantized: Quantized continuous codes (for decoder)
loss: Quantization loss (commitment, entropy, etc.)
- Return type:
Tuple of
- Raises:
TypeError β If x is not a floating point tensor
ValueError β If x has wrong shape or contains NaN/Inf
- decode(quant)[source][source]ο
Decode from quantized continuous codes.
- Parameters:
quant (
Float[Tensor, 'batch embedding_dim *spatial_compressed']) β Quantized codes from encode() (continuous representation). Shape:(B, embedding_dim, *spatial_compressed)- Return type:
Float[Tensor, 'batch channels *spatial']- Returns:
Reconstructed output with original spatial dimensions
- Raises:
TypeError β If quant is not a floating point tensor
ValueError β If quant has wrong shape or contains NaN/Inf
- forward(input)[source][source]ο
Full forward pass: encode -> quantize -> decode.
During training, returns dict with all outputs for loss computation. During evaluation, returns NetworkEval namedtuple.
- Parameters:
input (
Float[Tensor, 'batch channels *spatial']) β Input tensor- Returns:
- βreconstructionsβ: Decoded output
βquant_lossβ: Quantization loss
βquant_infoβ: Discrete indices
βlatentsβ: Quantized codes (continuous)
- Eval mode (NetworkEval):
reconstructions: Decoded output
quant_loss: Quantization loss
quant_info: Discrete indices
- Return type:
Training mode (dict)
- tokenize(x)[source][source]ο
Encode input to discrete token indices.
This is the primary encoding method for inference and storage. Returns integer indices that can be stored efficiently or fed to autoregressive models.
- Parameters:
x (
Float[Tensor, 'batch channels *spatial']) β Input tensor- Return type:
Int[Tensor, 'batch *spatial_indices']- Returns:
Discrete indices suitable for storage or sequence modeling
- detokenize(indices, spatial_shape=None)[source][source]ο
Decode from discrete token indices.
Inverse of tokenize(). Converts discrete indices back to continuous output via codebook lookup and decoder. This is the canonical index-to-reconstruction decoding path for inference, converting stored/generated indices back to images.
- Parameters:
indices (
Int[Tensor, 'batch *spatial_indices']) β Discrete indices from tokenize(). Can be: - Spatial format: (B, Hβ, Wβ) for 2D or (B, Hβ, Wβ, Dβ) for 3D - Flattened format: (B, N) where N = Hβ * Wβ [* Dβ]spatial_shape (
tuple[int,...] |None, default:None) β Original latent spatial dimensions (Hβ, Wβ) or (Hβ, Wβ, Dβ). Required when indices are flattened to avoid incorrect cubic assumptions for anisotropic volumes.
- Return type:
Float[Tensor, 'batch channels *spatial']- Returns:
Reconstructed output
- Raises:
ValueError β If spatial_shape is required but not provided
- get_latent_shape(input_shape)[source][source]ο
Calculate discrete index shape for given input shape.
Note: Returns shape WITHOUT embedding dimension (just spatial). For RESFSQ, includes num_quantizers dimension.
- get_codebook_size()[source][source]ο
Get the total vocabulary size.
- Return type:
- Returns:
Number of discrete codes in vocabulary
- reconstruct(x, roi_size=None, overlap=0.0)[source][source]ο
Reconstruction with optional sliding window (overlap must be 0.0).
Discrete tokenizers donβt support overlapping windows because averaging discrete codes is not meaningful.
- Parameters:
- Return type:
- Returns:
Reconstructed output
- Raises:
ValueError β If overlap > 0.0
- T_destination = ~T_destinationο
- add_module(name, module)[source]ο
Add a child module to the current module.
The module can be accessed as an attribute using the given name.
- apply(fn)[source]ο
Apply
fnrecursively to every submodule (as returned by.children()) as well as self.Typical use includes initializing the parameters of a model (see also torch.nn.init).
- Parameters:
fn (
Module-> None) β function to be applied to each submodule- Returns:
self
- Return type:
Module
Example:
>>> @torch.no_grad() >>> def init_weights(m): >>> print(m) >>> if type(m) is nn.Linear: >>> m.weight.fill_(1.0) >>> print(m.weight) >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )
- bfloat16()[source]ο
Casts all floating point parameters and buffers to
bfloat16datatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- buffers(recurse=True)[source]ο
Return an iterator over module buffers.
- Parameters:
recurse (bool) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module.
- Yields:
torch.Tensor β module buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for buf in model.buffers(): >>> print(type(buf), buf.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- compile(mode='reduce-overhead', fullgraph=False, **kwargs)[source]ο
Compile model with torch.compile() for faster inference.
Uses PyTorch 2.0+ compilation to optimize the model graph. The compiled model maintains the same interface but runs faster, especially for repeated inference calls.
- Parameters:
mode (
str, default:'reduce-overhead') β Compilation mode. Options: - βreduce-overheadβ: Best for small batches (default) - βmax-autotuneβ: Best throughput, longer warmup - βdefaultβ: Balanced compilationfullgraph (
bool, default:False) β If True, require full graph compilation (stricter)**kwargs β Additional arguments passed to torch.compile()
- Return type:
- Returns:
Compiled model (self, for method chaining)
Example
>>> model = ContinuousTokenizer.from_pretrained("path/to/model") >>> model = model.compile(mode="reduce-overhead") >>> # First call triggers compilation (slower) >>> latents = model.tokenize(batch) >>> # Subsequent calls are faster >>> latents = model.tokenize(batch2)
Note
Compilation happens on first forward pass
Different input shapes may trigger recompilation
Use dynamic=False (default) for fixed input sizes
- cpu()[source]ο
Move all model parameters and buffers to the CPU.
Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- cuda(device=None)[source]ο
Move all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on GPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- decode_batch(z, batch_size=8, show_progress=False)[source]ο
Decode large batch with automatic mini-batching.
- double()[source]ο
Casts all floating point parameters and buffers to
doubledatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- encode_batch(x, batch_size=8, show_progress=False)[source]ο
Encode large batch with automatic mini-batching.
Processes input in chunks to avoid OOM for large datasets.
- Parameters:
- Return type:
- Returns:
Concatenated latents for all inputs
Example
>>> dataset = torch.randn(1000, 1, 64, 64, 64) >>> latents = model.encode_batch(dataset, batch_size=4)
- eval()[source]ο
Set the module in evaluation mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e. whether they are affected, e.g.
Dropout,BatchNorm, etc.This is equivalent with
self.train(False).See Locally disabling gradient computation for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns:
self
- Return type:
Module
- extra_repr()[source]ο
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- Return type:
- float()[source]ο
Casts all floating point parameters and buffers to
floatdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- classmethod from_pretrained(model_name_or_path, map_location=None, **kwargs)[source]ο
Load model from local directory or HuggingFace Hub.
Automatically detects whether path is local or a Hub repository. For Hub repos, downloads config and weights to cache.
- Parameters:
- Return type:
- Returns:
Loaded model instance
Example
>>> # From local path >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer") >>> >>> # From HuggingFace Hub >>> model = ContinuousTokenizer.from_pretrained("username/my-tokenizer") >>> >>> # Override config >>> model = ContinuousTokenizer.from_pretrained( ... "./my-tokenizer", ... dropout=0.1 # Override saved dropout value ... )
- classmethod from_pretrained_encoder_decoder(pretrained_path, strict=False, verbose=True, **kwargs)[source]ο
Create model and load encoder/decoder weights from pretrained checkpoint.
Factory method that creates a new model instance and loads encoder/decoder weights in one step. Useful for initializing new architectures from pretrained backbones.
- Parameters:
- Return type:
- Returns:
New model instance with loaded encoder/decoder weights
Example
>>> # Create FSQ model initialized from VAE weights >>> model = DiscreteTokenizer.from_pretrained_encoder_decoder( ... 'weights/maisi_converted.pt', ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... z_channels=256, ... channels=64, ... channels_mult=(1, 2, 4), ... )
- get_buffer(target)[source]ο
Return the buffer given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the buffer to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The buffer referenced by
target- Return type:
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not a buffer
- get_extra_state()[source]ο
Return any extra state to include in the moduleβs state_dict.
Implement this and a corresponding
set_extra_state()for your module if you need to store extra state. This function is called when building the moduleβs state_dict().Note that extra state should be picklable to ensure working serialization of the state_dict. We only provide backwards compatibility guarantees for serializing Tensors; other objects may break backwards compatibility if their serialized pickled form changes.
- Returns:
Any extra state to store in the moduleβs state_dict
- Return type:
- get_parameter(target)[source]ο
Return the parameter given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the Parameter to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The Parameter referenced by
target- Return type:
torch.nn.Parameter
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not an
nn.Parameter
- get_submodule(target)[source]ο
Return the submodule given by
targetif it exists, otherwise throw an error.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(16, 33, kernel_size=(3, 3), stride=(2, 2)) ) (linear): Linear(in_features=100, out_features=200, bias=True) ) )(The diagram shows an
nn.ModuleA.Awhich has a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To check whether or not we have the
linearsubmodule, we would callget_submodule("net_b.linear"). To check whether we have theconvsubmodule, we would callget_submodule("net_b.net_c.conv").The runtime of
get_submoduleis bounded by the degree of module nesting intarget. A query againstnamed_modulesachieves the same result, but it is O(N) in the number of transitive modules. So, for a simple check to see if some submodule exists,get_submoduleshould always be used.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)- Returns:
The submodule referenced by
target- Return type:
- Raises:
AttributeError β If at any point along the path resulting from the target string the (sub)path resolves to a non-existent attribute name or an object that is not an instance of
nn.Module.
- half()[source]ο
Casts all floating point parameters and buffers to
halfdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- inference_mode()[source]ο
Context manager for optimized inference.
Configures the model for maximum inference performance: - Sets model to eval mode - Disables gradient computation - Uses torch.inference_mode for additional optimizations
The model is restored to its previous state upon exit.
- Return type:
Example
>>> with model.inference_mode(): ... latents = model.tokenize(volume) ... recon = model.detokenize(latents)
- Yields:
self β The model instance for method chaining
- ipu(device=None)[source]ο
Move all model parameters and buffers to the IPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on IPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- load_encoder_decoder_weights(pretrained_path, strict=False, verbose=True)[source]ο
Load encoder/decoder weights from a pretrained checkpoint.
This method enables transfer learning by loading encoder and decoder weights from any compatible tokenizer (VAE, VQ-VAE, FSQ, etc.) while leaving quantizer-specific layers randomly initialized.
Use Casesο
Initialize discrete tokenizer from VAE: Load MAISI VAE encoder/decoder, then train only the quantizer layers (VQ, FSQ, etc.)
Fine-tune on new domain: Start from pretrained weights, fine-tune all
Encoder-only transfer: Use pretrained encoder for downstream tasks
How It Worksο
The method identifies encoder/decoder weights by key prefixes and loads only those that match. Quantizer-specific layers (quant_conv, post_quant_conv, quantizer) may or may not be loaded depending on architecture compatibility.
Weight Matching Strategy: - encoder.* keys: Always attempted - decoder.* keys: Always attempted - quant_conv.* keys: Loaded if shapes match - post_quant_conv.* keys: Loaded if shapes match - quantizer.* keys: Skipped (architecture-specific)
- type pretrained_path:
- param pretrained_path:
Path to pretrained weights file (.pt, .bin) or directory containing βpytorch_model.binβ
- type strict:
bool, default:False- param strict:
If True, raise error on shape mismatches. If False (default), skip mismatched weights and log warnings.
- type verbose:
bool, default:True- param verbose:
If True, print summary of loaded/skipped weights
- rtype:
- returns:
Tuple of (loaded_keys, skipped_keys) for inspection
Example
>>> # Initialize FSQ tokenizer from MAISI VAE weights >>> model = DiscreteTokenizer( ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... # ... other args matching MAISI architecture ... ) >>> loaded, skipped = model.load_encoder_decoder_weights( ... 'weights/maisi_converted.pt', ... verbose=True ... ) >>> print(f"Loaded {len(loaded)} keys, skipped {len(skipped)}") >>> >>> # Now train with frozen encoder if desired >>> for param in model.encoder.parameters(): ... param.requires_grad = False
Note
For best results, ensure the pretrained model has the same: - channels, channels_mult, num_res_blocks - spatial_compression
The following can differ: - z_channels, latent_channels, embedding_dim - Quantizer type and configuration
- load_state_dict(state_dict, strict=True, assign=False)[source]ο
Copy parameters and buffers from
state_dictinto this module and its descendants.If
strictisTrue, then the keys ofstate_dictmust exactly match the keys returned by this moduleβsstate_dict()function.Warning
If
assignisTruethe optimizer must be created after the call toload_state_dictunlessget_swap_module_params_on_conversion()isTrue.- Parameters:
state_dict (dict) β a dict containing parameters and persistent buffers.
strict (bool, optional) β whether to strictly enforce that the keys in
state_dictmatch the keys returned by this moduleβsstate_dict()function. Default:Trueassign (bool, optional) β When set to
False, the properties of the tensors in the current module are preserved whereas setting it toTruepreserves properties of the Tensors in the state dict. The only exception is therequires_gradfield ofParameterfor which the value from the module is preserved. Default:False
- Returns:
missing_keysis a list of str containing any keys that are expectedby this module but missing from the provided
state_dict.
unexpected_keysis a list of str containing the keys that are notexpected by this module but present in the provided
state_dict.
- Return type:
NamedTuplewithmissing_keysandunexpected_keysfields
Note
If a parameter or buffer is registered as
Noneand its corresponding key exists instate_dict,load_state_dict()will raise aRuntimeError.
- modules(remove_duplicate=True)[source]ο
Return an iterator over all modules in the network.
- Parameters:
remove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not.- Yields:
Module β a module in the network
- Return type:
Note
Duplicate modules are returned only once by default. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): ... print(idx, '->', m) 0 -> Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) ) 1 -> Linear(in_features=2, out_features=2, bias=True)
- mtia(device=None)[source]ο
Move all model parameters and buffers to the MTIA.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on MTIA while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- named_buffers(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
- Parameters:
prefix (str) β prefix to prepend to all buffer names.
recurse (bool, optional) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module. Defaults to True.
remove_duplicate (bool, optional) β whether to remove the duplicated buffers in the result. Defaults to True.
- Yields:
(str, torch.Tensor) β Tuple containing the name and buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, buf in self.named_buffers(): >>> if name in ['running_var']: >>> print(buf.size())
- named_children()[source]ο
Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
- Yields:
(str, Module) β Tuple containing a name and child module
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module)
- named_modules(memo=None, prefix='', remove_duplicate=True)[source]ο
Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
- Parameters:
memo (
set[Module] |None, default:None) β a memo to store the set of modules already added to the resultprefix (
str, default:'') β a prefix that will be added to the name of the moduleremove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not
- Yields:
(str, Module) β Tuple of name and module
Note
Duplicate modules are returned only once. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): ... print(idx, '->', m) 0 -> ('', Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )) 1 -> ('0', Linear(in_features=2, out_features=2, bias=True))
- named_parameters(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
- Parameters:
prefix (str) β prefix to prepend to all parameter names.
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
remove_duplicate (bool, optional) β whether to remove the duplicated parameters in the result. Defaults to True.
- Yields:
(str, Parameter) β Tuple containing the name and parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size())
- num_parameters()[source]ο
Get total number of learnable parameters.
- Return type:
- Returns:
Sum of numel() for all parameters
- parameters(recurse=True)[source]ο
Return an iterator over module parameters.
This is typically passed to an optimizer.
- Parameters:
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
- Yields:
Parameter β module parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for param in model.parameters(): >>> print(type(param), param.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- push_to_hub(repo_id, save_directory=None, commit_message='Upload model', private=False, **kwargs)[source]ο
Upload model to HuggingFace Hub.
Creates or updates a repository on the Hub with model weights and configuration.
- Parameters:
repo_id (
str) β Repository ID (e.g., βusername/model-nameβ)save_directory (
Optional[str], default:None) β Local directory to save before upload (auto-created if None)commit_message (
str, default:'Upload model') β Commit message for the uploadprivate (
bool, default:False) β Whether to create a private repository**kwargs β Additional arguments for HfApi.upload_folder
- Return type:
Example
>>> model.push_to_hub("username/my-vae-tokenizer") >>> # Creates https://huggingface.co/username/my-vae-tokenizer
- register_backward_hook(hook)[source]ο
Register a backward hook on the module.
This function is deprecated in favor of
register_full_backward_hook()and the behavior of this function will change in future versions.
- register_buffer(name, tensor, persistent=True)[source]ο
Add a buffer to the module.
This is typically used to register a buffer that should not be considered a model parameter. For example, BatchNormβs
running_meanis not a parameter, but is part of the moduleβs state. Buffers, by default, are persistent and will be saved alongside parameters. This behavior can be changed by settingpersistenttoFalse. The only difference between a persistent buffer and a non-persistent buffer is that the latter will not be a part of this moduleβsstate_dict.Buffers can be accessed as attributes using given names.
- Parameters:
name (str) β name of the buffer. The buffer can be accessed from this module using the given name
tensor (Tensor or None) β buffer to be registered. If
None, then operations that run on buffers, such ascuda, are ignored. IfNone, the buffer is not included in the moduleβsstate_dict.persistent (bool) β whether the buffer is part of this moduleβs
state_dict.
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> self.register_buffer('running_mean', torch.zeros(num_features))
- register_forward_hook(hook, *, prepend=False, with_kwargs=False, always_call=False)[source]ο
Register a forward hook on the module.
The hook will be called every time after
forward()has computed an output.If
with_kwargsisFalseor not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the output. It can modify the input inplace but it will not have effect on forward since this is called afterforward()is called. The hook should have the following signature:hook(module, args, output) -> None or modified output
If
with_kwargsisTrue, the forward hook will be passed thekwargsgiven to the forward function and be expected to return the output possibly modified. The hook should have the following signature:hook(module, args, kwargs, output) -> None or modified output
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If
True, the providedhookwill be fired before all existingforwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforwardhooks on thistorch.nn.Module. Note that globalforwardhooks registered withregister_module_forward_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If
True, thehookwill be passed the kwargs given to the forward function. Default:Falsealways_call (bool) β If
Truethehookwill be run regardless of whether an exception is raised while calling the Module. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_forward_pre_hook(hook, *, prepend=False, with_kwargs=False)[source]ο
Register a forward pre-hook on the module.
The hook will be called every time before
forward()is invoked.If
with_kwargsis false or not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the input. User can either return a tuple or a single modified value in the hook. We will wrap the value into a tuple if a single value is returned (unless that value is already a tuple). The hook should have the following signature:hook(module, args) -> None or modified input
If
with_kwargsis true, the forward pre-hook will be passed the kwargs given to the forward function. And if the hook modifies the input, both the args and kwargs should be returned. The hook should have the following signature:hook(module, args, kwargs) -> None or a tuple of modified input and kwargs
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingforward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforward_prehooks on thistorch.nn.Module. Note that globalforward_prehooks registered withregister_module_forward_pre_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If true, the
hookwill be passed the kwargs given to the forward function. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_hook(hook, prepend=False)[source]ο
Register a backward hook on the module.
The hook will be called every time the gradients with respect to a module are computed, and its firing rules are as follows:
Ordinarily, the hook fires when the gradients are computed with respect to the module inputs.
If none of the module inputs require gradients, the hook will fire when the gradients are computed with respect to module outputs.
If none of the module outputs require gradients, then the hooks will not fire.
The hook should have the following signature:
hook(module, grad_input, grad_output) -> tuple(Tensor) or None
The
grad_inputandgrad_outputare tuples that contain the gradients with respect to the inputs and outputs respectively. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the input that will be used in place ofgrad_inputin subsequent computations.grad_inputwill only correspond to the inputs given as positional arguments and all kwarg arguments are ignored. Entries ingrad_inputandgrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs or outputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackwardhooks on thistorch.nn.Module. Note that globalbackwardhooks registered withregister_module_full_backward_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_pre_hook(hook, prepend=False)[source]ο
Register a backward pre-hook on the module.
The hook will be called every time the gradients for the module are computed. The hook should have the following signature:
hook(module, grad_output) -> tuple[Tensor, ...], Tensor or None
The
grad_outputis a tuple. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the output that will be used in place ofgrad_outputin subsequent computations. Entries ingrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackward_prehooks on thistorch.nn.Module. Note that globalbackward_prehooks registered withregister_module_full_backward_pre_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_post_hook(hook)[source]ο
Register a post-hook to be run after moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, incompatible_keys) -> None
The
moduleargument is the current module that this hook is registered on, and theincompatible_keysargument is aNamedTupleconsisting of attributesmissing_keysandunexpected_keys.missing_keysis alistofstrcontaining the missing keys andunexpected_keysis alistofstrcontaining the unexpected keys.The given incompatible_keys can be modified inplace if needed.
Note that the checks performed when calling
load_state_dict()withstrict=Trueare affected by modifications the hook makes tomissing_keysorunexpected_keys, as expected. Additions to either set of keys will result in an error being thrown whenstrict=True, and clearing out both missing and unexpected keys will avoid an error.- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_pre_hook(hook)[source]ο
Register a pre-hook to be run before moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) -> None # noqa: B950
- Parameters:
hook (Callable) β Callable hook that will be invoked before loading the state dict.
- register_module(name, module)[source]ο
Alias for
add_module().
- register_parameter(name, param)[source]ο
Add a parameter to the module.
The parameter can be accessed as an attribute using given name.
- Parameters:
name (str) β name of the parameter. The parameter can be accessed from this module using the given name
param (Parameter or None) β parameter to be added to the module. If
None, then operations that run on parameters, such ascuda, are ignored. IfNone, the parameter is not included in the moduleβsstate_dict.
- Return type:
- register_state_dict_post_hook(hook)[source]ο
Register a post-hook for the
state_dict()method.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata) -> None
The registered hooks can modify the
state_dictinplace.
- register_state_dict_pre_hook(hook)[source]ο
Register a pre-hook for the
state_dict()method.- It should have the following signature::
hook(module, prefix, keep_vars) -> None
The registered hooks can be used to perform pre-processing before the
state_dictcall is made.
- requires_grad_(requires_grad=True)[source]ο
Change if autograd should record operations on parameters in this module.
This method sets the parametersβ
requires_gradattributes in-place.This method is helpful for freezing part of the module for finetuning or training parts of a model individually (e.g., GAN training).
See Locally disabling gradient computation for a comparison between .requires_grad_() and several similar mechanisms that may be confused with it.
- Parameters:
requires_grad (bool) β whether autograd should record operations on parameters in this module. Default:
True.- Returns:
self
- Return type:
Module
- save_pretrained(save_directory, push_to_hub=False, **kwargs)[source]ο
Save model weights and configuration to directory.
Creates a directory structure compatible with from_pretrained():
` save_directory/ βββ config.json # Model configuration βββ pytorch_model.bin # Model weights `- Parameters:
- Return type:
Example
>>> model.save_pretrained("./my-tokenizer") >>> # Later... >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer")
- set_extra_state(state)[source]ο
Set extra state contained in the loaded state_dict.
This function is called from
load_state_dict()to handle any extra state found within the state_dict. Implement this function and a correspondingget_extra_state()for your module if you need to store extra state within its state_dict.
- set_submodule(target, module, strict=False)[source]ο
Set the submodule given by
targetif it exists, otherwise throw an error.Note
If
strictis set toFalse(default), the method will replace an existing submodule or create a new submodule if the parent module exists. Ifstrictis set toTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule does not exist.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(3, 3, 3) ) (linear): Linear(3, 3) ) )(The diagram shows an
nn.ModuleA.Ahas a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To override the
Conv2dwith a new submoduleLinear, you could callset_submodule("net_b.net_c.conv", nn.Linear(1, 1))wherestrictcould beTrueorFalseTo add a new submodule
Conv2dto the existingnet_bmodule, you would callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1)).In the above if you set
strict=Trueand callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1), strict=True), an AttributeError will be raised becausenet_bdoes not have a submodule namedconv.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)module (
Module) β The module to set the submodule to.strict (
bool, default:False) β IfFalse, the method will replace an existing submodule or create a new submodule if the parent module exists. IfTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule doesnβt already exist.
- Raises:
ValueError β If the
targetstring is empty or ifmoduleis not an instance ofnn.Module.AttributeError β If at any point along the path resulting from the
targetstring the (sub)path resolves to a non-existent attribute name or an object that is not an instance ofnn.Module.
- Return type:
See
torch.Tensor.share_memory_().- Return type:
Self
- state_dict(*args, destination=None, prefix='', keep_vars=False)[source]ο
Return a dictionary containing references to the whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. Parameters and buffers set to
Noneare not included.Note
The returned object is a shallow copy. It contains references to the moduleβs parameters and buffers.
Warning
Currently
state_dict()also accepts positional arguments fordestination,prefixandkeep_varsin order. However, this is being deprecated and keyword arguments will be enforced in future releases.Warning
Please avoid the use of argument
destinationas it is not designed for end-users.- Parameters:
destination (dict, optional) β If provided, the state of module will be updated into the dict and the same object is returned. Otherwise, an
OrderedDictwill be created and returned. Default:None.prefix (str, optional) β a prefix added to parameter and buffer names to compose the keys in state_dict. Default:
''.keep_vars (bool, optional) β by default the
Tensors returned in the state dict are detached from autograd. If itβs set toTrue, detaching will not be performed. Default:False.
- Returns:
a dictionary containing a whole state of the module
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> module.state_dict().keys() ['bias', 'weight']
- to_empty(*, device, recurse=True)[source]ο
Move the parameters and buffers to the specified device without copying storage.
- Parameters:
device (
torch.device) β The desired device of the parameters and buffers in this module.recurse (bool) β Whether parameters and buffers of submodules should be recursively moved to the specified device.
- Returns:
self
- Return type:
Module
- train(mode=True)[source]ο
Set the module in training mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e., whether they are affected, e.g.
Dropout,BatchNorm, etc.- Parameters:
mode (bool) β whether to set training mode (
True) or evaluation mode (False). Default:True.- Returns:
self
- Return type:
Module
- type(dst_type)[source]ο
Casts all parameters and buffers to
dst_type.Note
This method modifies the module in-place.
- Parameters:
dst_type (type or string) β the desired type
- Returns:
self
- Return type:
Module
- xpu(device=None)[source]ο
Move all model parameters and buffers to the XPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on XPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- zero_grad(set_to_none=True)[source]ο
Reset gradients of all model parameters.
See similar function under
torch.optim.Optimizerfor more context.- Parameters:
set_to_none (bool) β instead of setting to zero, set the grads to None. See
torch.optim.Optimizer.zero_grad()for details.- Return type:
- class medtokenizers.MAISITokenizer(pretrained=None, **kwargs)[source][source]ο
Bases:
medtokenizers.networks.continuous.ContinuousTokenizerMAISI VAE tokenizer matching NVIDIA NV-Generate-MR architecture.
To use published NVIDIA MAISI weights, first convert them with
scripts/convert_maisi_to_hf.pyand then load the converted checkpoint viafrom_pretrained(). Note that NVIDIA MAISI model weights are distributed under the NVIDIA Source Code License (NSCLv1), separate from this repositoryβs Apache-2.0 code license.- DEFAULT_CONFIG = {'attn_resolutions': (), 'channels': 64, 'channels_mult': (1, 2, 4), 'decoder_blocks_per_stage': [2, 2, 0], 'dim': 3, 'dropout': 0.0, 'formulation': 'VAE', 'in_channels': 1, 'latent_channels': 4, 'name': 'MAISITokenizer', 'num_res_blocks': 2, 'out_channels': 1, 'resolution': 256, 'spatial_compression': 4, 'use_encoder_mid': False, 'z_channels': 4, 'z_factor': 2}ο
- classmethod from_pretrained(pretrained_model_name_or_path, **kwargs)[source][source]ο
Load from local checkpoint.
- Parameters:
pretrained_model_name_or_path (
str)- Return type:
- static get_training_config()[source][source]ο
Recommended training hyperparameters from MAISI paper.
- Return type:
- T_destination = ~T_destinationο
- add_module(name, module)[source]ο
Add a child module to the current module.
The module can be accessed as an attribute using the given name.
- apply(fn)[source]ο
Apply
fnrecursively to every submodule (as returned by.children()) as well as self.Typical use includes initializing the parameters of a model (see also torch.nn.init).
- Parameters:
fn (
Module-> None) β function to be applied to each submodule- Returns:
self
- Return type:
Module
Example:
>>> @torch.no_grad() >>> def init_weights(m): >>> print(m) >>> if type(m) is nn.Linear: >>> m.weight.fill_(1.0) >>> print(m.weight) >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )
- bfloat16()[source]ο
Casts all floating point parameters and buffers to
bfloat16datatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- buffers(recurse=True)[source]ο
Return an iterator over module buffers.
- Parameters:
recurse (bool) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module.
- Yields:
torch.Tensor β module buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for buf in model.buffers(): >>> print(type(buf), buf.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- compile(mode='reduce-overhead', fullgraph=False, **kwargs)[source]ο
Compile model with torch.compile() for faster inference.
Uses PyTorch 2.0+ compilation to optimize the model graph. The compiled model maintains the same interface but runs faster, especially for repeated inference calls.
- Parameters:
mode (
str, default:'reduce-overhead') β Compilation mode. Options: - βreduce-overheadβ: Best for small batches (default) - βmax-autotuneβ: Best throughput, longer warmup - βdefaultβ: Balanced compilationfullgraph (
bool, default:False) β If True, require full graph compilation (stricter)**kwargs β Additional arguments passed to torch.compile()
- Return type:
- Returns:
Compiled model (self, for method chaining)
Example
>>> model = ContinuousTokenizer.from_pretrained("path/to/model") >>> model = model.compile(mode="reduce-overhead") >>> # First call triggers compilation (slower) >>> latents = model.tokenize(batch) >>> # Subsequent calls are faster >>> latents = model.tokenize(batch2)
Note
Compilation happens on first forward pass
Different input shapes may trigger recompilation
Use dynamic=False (default) for fixed input sizes
- cpu()[source]ο
Move all model parameters and buffers to the CPU.
Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- cuda(device=None)[source]ο
Move all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on GPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- decode(z)[source]ο
Decode latent representation to output.
- Parameters:
z (
Float[Tensor, 'batch latent_channels *spatial_compressed']) β Latent tensor from encode() or external source. Shape:(B, latent_channels, *spatial_compressed)- Return type:
Float[Tensor, 'batch channels *spatial']- Returns:
Reconstructed output with original spatial dimensions
- Raises:
TypeError β If z is not a floating point tensor
ValueError β If z has wrong shape or contains NaN/Inf
- decode_batch(z, batch_size=8, show_progress=False)[source]ο
Decode large batch with automatic mini-batching.
- double()[source]ο
Casts all floating point parameters and buffers to
doubledatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- encode(x)[source]ο
Encode input to latent representation.
For VAE: - Encoder outputs (ΞΌ, log ΟΒ²) - Samples z using reparameterization: z = ΞΌ + Ο * Ξ΅ - Returns (z, (kl_loss, (mean, logvar)))
For AE: - Encoder outputs z directly - Returns (z, (zero_kl, zero_logvar))
- Parameters:
x (
Float[Tensor, 'batch channels *spatial']) β Input tensor of shape(B, C, *spatial)where: - B: batch size - C: number of channels (must match modelβs in_channels) - spatial: (H, W) for 2D or (H, W, D) for 3D- Returns:
latent: Sampled or deterministic latent tensor
distribution_output: KL loss and posterior parameters
- Return type:
Tuple of
- Raises:
TypeError β If x is not a floating point tensor
ValueError β If x has wrong shape or contains NaN/Inf
- encode_batch(x, batch_size=8, show_progress=False)[source]ο
Encode large batch with automatic mini-batching.
Processes input in chunks to avoid OOM for large datasets.
- Parameters:
- Return type:
- Returns:
Concatenated latents for all inputs
Example
>>> dataset = torch.randn(1000, 1, 64, 64, 64) >>> latents = model.encode_batch(dataset, batch_size=4)
- eval()[source]ο
Set the module in evaluation mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e. whether they are affected, e.g.
Dropout,BatchNorm, etc.This is equivalent with
self.train(False).See Locally disabling gradient computation for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns:
self
- Return type:
Module
- extra_repr()[source]ο
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- Return type:
- float()[source]ο
Casts all floating point parameters and buffers to
floatdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- forward(input)[source]ο
Full forward pass: encode -> decode.
During training, returns a dict with all outputs for loss computation. During evaluation, returns a NetworkEval namedtuple.
- Parameters:
input (
Float[Tensor, 'batch channels *spatial']) β Input tensor- Returns:
- βreconstructionsβ: Decoded output
βposteriorsβ: (mean, logvar) for VAE, or identity for AE
βlatentβ/βlatentsβ: Sampled latent tensor
βkl_lossβ: KL divergence (VAE only)
- Eval mode (NetworkEval):
reconstructions: Decoded output
posteriors: Distribution parameters
latent: Sampled latent
- Return type:
Training mode (dict)
- classmethod from_pretrained_encoder_decoder(pretrained_path, strict=False, verbose=True, **kwargs)[source]ο
Create model and load encoder/decoder weights from pretrained checkpoint.
Factory method that creates a new model instance and loads encoder/decoder weights in one step. Useful for initializing new architectures from pretrained backbones.
- Parameters:
- Return type:
- Returns:
New model instance with loaded encoder/decoder weights
Example
>>> # Create FSQ model initialized from VAE weights >>> model = DiscreteTokenizer.from_pretrained_encoder_decoder( ... 'weights/maisi_converted.pt', ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... z_channels=256, ... channels=64, ... channels_mult=(1, 2, 4), ... )
- get_buffer(target)[source]ο
Return the buffer given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the buffer to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The buffer referenced by
target- Return type:
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not a buffer
- get_extra_state()[source]ο
Return any extra state to include in the moduleβs state_dict.
Implement this and a corresponding
set_extra_state()for your module if you need to store extra state. This function is called when building the moduleβs state_dict().Note that extra state should be picklable to ensure working serialization of the state_dict. We only provide backwards compatibility guarantees for serializing Tensors; other objects may break backwards compatibility if their serialized pickled form changes.
- Returns:
Any extra state to store in the moduleβs state_dict
- Return type:
- get_latent_shape(input_shape)[source]ο
Calculate output latent shape for given input shape.
Useful for pre-allocating memory or understanding compression ratio.
- get_parameter(target)[source]ο
Return the parameter given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the Parameter to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The Parameter referenced by
target- Return type:
torch.nn.Parameter
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not an
nn.Parameter
- get_submodule(target)[source]ο
Return the submodule given by
targetif it exists, otherwise throw an error.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(16, 33, kernel_size=(3, 3), stride=(2, 2)) ) (linear): Linear(in_features=100, out_features=200, bias=True) ) )(The diagram shows an
nn.ModuleA.Awhich has a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To check whether or not we have the
linearsubmodule, we would callget_submodule("net_b.linear"). To check whether we have theconvsubmodule, we would callget_submodule("net_b.net_c.conv").The runtime of
get_submoduleis bounded by the degree of module nesting intarget. A query againstnamed_modulesachieves the same result, but it is O(N) in the number of transitive modules. So, for a simple check to see if some submodule exists,get_submoduleshould always be used.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)- Returns:
The submodule referenced by
target- Return type:
- Raises:
AttributeError β If at any point along the path resulting from the target string the (sub)path resolves to a non-existent attribute name or an object that is not an instance of
nn.Module.
- half()[source]ο
Casts all floating point parameters and buffers to
halfdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- inference_mode()[source]ο
Context manager for optimized inference.
Configures the model for maximum inference performance: - Sets model to eval mode - Disables gradient computation - Uses torch.inference_mode for additional optimizations
The model is restored to its previous state upon exit.
- Return type:
Example
>>> with model.inference_mode(): ... latents = model.tokenize(volume) ... recon = model.detokenize(latents)
- Yields:
self β The model instance for method chaining
- ipu(device=None)[source]ο
Move all model parameters and buffers to the IPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on IPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- load_encoder_decoder_weights(pretrained_path, strict=False, verbose=True)[source]ο
Load encoder/decoder weights from a pretrained checkpoint.
This method enables transfer learning by loading encoder and decoder weights from any compatible tokenizer (VAE, VQ-VAE, FSQ, etc.) while leaving quantizer-specific layers randomly initialized.
Use Casesο
Initialize discrete tokenizer from VAE: Load MAISI VAE encoder/decoder, then train only the quantizer layers (VQ, FSQ, etc.)
Fine-tune on new domain: Start from pretrained weights, fine-tune all
Encoder-only transfer: Use pretrained encoder for downstream tasks
How It Worksο
The method identifies encoder/decoder weights by key prefixes and loads only those that match. Quantizer-specific layers (quant_conv, post_quant_conv, quantizer) may or may not be loaded depending on architecture compatibility.
Weight Matching Strategy: - encoder.* keys: Always attempted - decoder.* keys: Always attempted - quant_conv.* keys: Loaded if shapes match - post_quant_conv.* keys: Loaded if shapes match - quantizer.* keys: Skipped (architecture-specific)
- type pretrained_path:
- param pretrained_path:
Path to pretrained weights file (.pt, .bin) or directory containing βpytorch_model.binβ
- type strict:
bool, default:False- param strict:
If True, raise error on shape mismatches. If False (default), skip mismatched weights and log warnings.
- type verbose:
bool, default:True- param verbose:
If True, print summary of loaded/skipped weights
- rtype:
- returns:
Tuple of (loaded_keys, skipped_keys) for inspection
Example
>>> # Initialize FSQ tokenizer from MAISI VAE weights >>> model = DiscreteTokenizer( ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... # ... other args matching MAISI architecture ... ) >>> loaded, skipped = model.load_encoder_decoder_weights( ... 'weights/maisi_converted.pt', ... verbose=True ... ) >>> print(f"Loaded {len(loaded)} keys, skipped {len(skipped)}") >>> >>> # Now train with frozen encoder if desired >>> for param in model.encoder.parameters(): ... param.requires_grad = False
Note
For best results, ensure the pretrained model has the same: - channels, channels_mult, num_res_blocks - spatial_compression
The following can differ: - z_channels, latent_channels, embedding_dim - Quantizer type and configuration
- load_state_dict(state_dict, strict=True, assign=False)[source]ο
Copy parameters and buffers from
state_dictinto this module and its descendants.If
strictisTrue, then the keys ofstate_dictmust exactly match the keys returned by this moduleβsstate_dict()function.Warning
If
assignisTruethe optimizer must be created after the call toload_state_dictunlessget_swap_module_params_on_conversion()isTrue.- Parameters:
state_dict (dict) β a dict containing parameters and persistent buffers.
strict (bool, optional) β whether to strictly enforce that the keys in
state_dictmatch the keys returned by this moduleβsstate_dict()function. Default:Trueassign (bool, optional) β When set to
False, the properties of the tensors in the current module are preserved whereas setting it toTruepreserves properties of the Tensors in the state dict. The only exception is therequires_gradfield ofParameterfor which the value from the module is preserved. Default:False
- Returns:
missing_keysis a list of str containing any keys that are expectedby this module but missing from the provided
state_dict.
unexpected_keysis a list of str containing the keys that are notexpected by this module but present in the provided
state_dict.
- Return type:
NamedTuplewithmissing_keysandunexpected_keysfields
Note
If a parameter or buffer is registered as
Noneand its corresponding key exists instate_dict,load_state_dict()will raise aRuntimeError.
- modules(remove_duplicate=True)[source]ο
Return an iterator over all modules in the network.
- Parameters:
remove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not.- Yields:
Module β a module in the network
- Return type:
Note
Duplicate modules are returned only once by default. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): ... print(idx, '->', m) 0 -> Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) ) 1 -> Linear(in_features=2, out_features=2, bias=True)
- mtia(device=None)[source]ο
Move all model parameters and buffers to the MTIA.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on MTIA while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- named_buffers(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
- Parameters:
prefix (str) β prefix to prepend to all buffer names.
recurse (bool, optional) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module. Defaults to True.
remove_duplicate (bool, optional) β whether to remove the duplicated buffers in the result. Defaults to True.
- Yields:
(str, torch.Tensor) β Tuple containing the name and buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, buf in self.named_buffers(): >>> if name in ['running_var']: >>> print(buf.size())
- named_children()[source]ο
Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
- Yields:
(str, Module) β Tuple containing a name and child module
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module)
- named_modules(memo=None, prefix='', remove_duplicate=True)[source]ο
Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
- Parameters:
memo (
set[Module] |None, default:None) β a memo to store the set of modules already added to the resultprefix (
str, default:'') β a prefix that will be added to the name of the moduleremove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not
- Yields:
(str, Module) β Tuple of name and module
Note
Duplicate modules are returned only once. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): ... print(idx, '->', m) 0 -> ('', Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )) 1 -> ('0', Linear(in_features=2, out_features=2, bias=True))
- named_parameters(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
- Parameters:
prefix (str) β prefix to prepend to all parameter names.
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
remove_duplicate (bool, optional) β whether to remove the duplicated parameters in the result. Defaults to True.
- Yields:
(str, Parameter) β Tuple containing the name and parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size())
- num_parameters()[source]ο
Get total number of learnable parameters.
- Return type:
- Returns:
Sum of numel() for all parameters
- parameters(recurse=True)[source]ο
Return an iterator over module parameters.
This is typically passed to an optimizer.
- Parameters:
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
- Yields:
Parameter β module parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for param in model.parameters(): >>> print(type(param), param.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- push_to_hub(repo_id, save_directory=None, commit_message='Upload model', private=False, **kwargs)[source]ο
Upload model to HuggingFace Hub.
Creates or updates a repository on the Hub with model weights and configuration.
- Parameters:
repo_id (
str) β Repository ID (e.g., βusername/model-nameβ)save_directory (
Optional[str], default:None) β Local directory to save before upload (auto-created if None)commit_message (
str, default:'Upload model') β Commit message for the uploadprivate (
bool, default:False) β Whether to create a private repository**kwargs β Additional arguments for HfApi.upload_folder
- Return type:
Example
>>> model.push_to_hub("username/my-vae-tokenizer") >>> # Creates https://huggingface.co/username/my-vae-tokenizer
- reconstruct(x, roi_size=None, overlap=0.5, sw_batch_size=1)[source]ο
Full encode-decode reconstruction with optional sliding window.
For large 3D volumes that exceed GPU memory, this method implements sliding window inference with Gaussian importance weighting to seamlessly blend overlapping patches.
The Algorithmο
Pad input to multiple of stride + window size
Extract overlapping windows with specified stride
Process windows in batches through tokenize -> detokenize
Weight each windowβs contribution by Gaussian importance
Normalize by accumulated importance and crop to original size
Gaussian Weightingο
Uses a Gaussian importance map (2D or 3D based on input) that gives higher weight to the center of each window, reducing boundary artifacts when blending.
- type x:
- param x:
Input tensor of shape (B, C, H, W, D) for 3D or (B, C, H, W) for 2D
- type roi_size:
- param roi_size:
Size of sliding window. If None, processes entire volume. Can be int (isotropic) or tuple (anisotropic).
- type overlap:
float, default:0.5- param overlap:
Fraction of overlap between windows (0.0 to 0.9). Higher overlap = smoother blending but more compute.
- type sw_batch_size:
int, default:1- param sw_batch_size:
Number of windows to process in parallel per batch. Higher values use more GPU memory but are faster. Default is 1 (sequential processing).
- rtype:
- returns:
Reconstructed tensor with same shape as input
Example
>>> volume = torch.randn(1, 1, 256, 256, 256) # 256Β³ volume >>> # Process in 128Β³ windows with 50% overlap, 4 windows at a time >>> recon = model.reconstruct(volume, roi_size=128, overlap=0.5, sw_batch_size=4)
Note
For volumes that fit in memory, omit roi_size for faster processing
Overlap of 0.5 is a good default; higher values reduce artifacts but increase computation proportionally
sw_batch_size > 1 can significantly speed up inference on GPUs with sufficient memory
- register_backward_hook(hook)[source]ο
Register a backward hook on the module.
This function is deprecated in favor of
register_full_backward_hook()and the behavior of this function will change in future versions.
- register_buffer(name, tensor, persistent=True)[source]ο
Add a buffer to the module.
This is typically used to register a buffer that should not be considered a model parameter. For example, BatchNormβs
running_meanis not a parameter, but is part of the moduleβs state. Buffers, by default, are persistent and will be saved alongside parameters. This behavior can be changed by settingpersistenttoFalse. The only difference between a persistent buffer and a non-persistent buffer is that the latter will not be a part of this moduleβsstate_dict.Buffers can be accessed as attributes using given names.
- Parameters:
name (str) β name of the buffer. The buffer can be accessed from this module using the given name
tensor (Tensor or None) β buffer to be registered. If
None, then operations that run on buffers, such ascuda, are ignored. IfNone, the buffer is not included in the moduleβsstate_dict.persistent (bool) β whether the buffer is part of this moduleβs
state_dict.
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> self.register_buffer('running_mean', torch.zeros(num_features))
- register_forward_hook(hook, *, prepend=False, with_kwargs=False, always_call=False)[source]ο
Register a forward hook on the module.
The hook will be called every time after
forward()has computed an output.If
with_kwargsisFalseor not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the output. It can modify the input inplace but it will not have effect on forward since this is called afterforward()is called. The hook should have the following signature:hook(module, args, output) -> None or modified output
If
with_kwargsisTrue, the forward hook will be passed thekwargsgiven to the forward function and be expected to return the output possibly modified. The hook should have the following signature:hook(module, args, kwargs, output) -> None or modified output
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If
True, the providedhookwill be fired before all existingforwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforwardhooks on thistorch.nn.Module. Note that globalforwardhooks registered withregister_module_forward_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If
True, thehookwill be passed the kwargs given to the forward function. Default:Falsealways_call (bool) β If
Truethehookwill be run regardless of whether an exception is raised while calling the Module. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_forward_pre_hook(hook, *, prepend=False, with_kwargs=False)[source]ο
Register a forward pre-hook on the module.
The hook will be called every time before
forward()is invoked.If
with_kwargsis false or not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the input. User can either return a tuple or a single modified value in the hook. We will wrap the value into a tuple if a single value is returned (unless that value is already a tuple). The hook should have the following signature:hook(module, args) -> None or modified input
If
with_kwargsis true, the forward pre-hook will be passed the kwargs given to the forward function. And if the hook modifies the input, both the args and kwargs should be returned. The hook should have the following signature:hook(module, args, kwargs) -> None or a tuple of modified input and kwargs
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingforward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforward_prehooks on thistorch.nn.Module. Note that globalforward_prehooks registered withregister_module_forward_pre_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If true, the
hookwill be passed the kwargs given to the forward function. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_hook(hook, prepend=False)[source]ο
Register a backward hook on the module.
The hook will be called every time the gradients with respect to a module are computed, and its firing rules are as follows:
Ordinarily, the hook fires when the gradients are computed with respect to the module inputs.
If none of the module inputs require gradients, the hook will fire when the gradients are computed with respect to module outputs.
If none of the module outputs require gradients, then the hooks will not fire.
The hook should have the following signature:
hook(module, grad_input, grad_output) -> tuple(Tensor) or None
The
grad_inputandgrad_outputare tuples that contain the gradients with respect to the inputs and outputs respectively. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the input that will be used in place ofgrad_inputin subsequent computations.grad_inputwill only correspond to the inputs given as positional arguments and all kwarg arguments are ignored. Entries ingrad_inputandgrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs or outputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackwardhooks on thistorch.nn.Module. Note that globalbackwardhooks registered withregister_module_full_backward_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_pre_hook(hook, prepend=False)[source]ο
Register a backward pre-hook on the module.
The hook will be called every time the gradients for the module are computed. The hook should have the following signature:
hook(module, grad_output) -> tuple[Tensor, ...], Tensor or None
The
grad_outputis a tuple. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the output that will be used in place ofgrad_outputin subsequent computations. Entries ingrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackward_prehooks on thistorch.nn.Module. Note that globalbackward_prehooks registered withregister_module_full_backward_pre_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_post_hook(hook)[source]ο
Register a post-hook to be run after moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, incompatible_keys) -> None
The
moduleargument is the current module that this hook is registered on, and theincompatible_keysargument is aNamedTupleconsisting of attributesmissing_keysandunexpected_keys.missing_keysis alistofstrcontaining the missing keys andunexpected_keysis alistofstrcontaining the unexpected keys.The given incompatible_keys can be modified inplace if needed.
Note that the checks performed when calling
load_state_dict()withstrict=Trueare affected by modifications the hook makes tomissing_keysorunexpected_keys, as expected. Additions to either set of keys will result in an error being thrown whenstrict=True, and clearing out both missing and unexpected keys will avoid an error.- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_pre_hook(hook)[source]ο
Register a pre-hook to be run before moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) -> None # noqa: B950
- Parameters:
hook (Callable) β Callable hook that will be invoked before loading the state dict.
- register_module(name, module)[source]ο
Alias for
add_module().
- register_parameter(name, param)[source]ο
Add a parameter to the module.
The parameter can be accessed as an attribute using given name.
- Parameters:
name (str) β name of the parameter. The parameter can be accessed from this module using the given name
param (Parameter or None) β parameter to be added to the module. If
None, then operations that run on parameters, such ascuda, are ignored. IfNone, the parameter is not included in the moduleβsstate_dict.
- Return type:
- register_state_dict_post_hook(hook)[source]ο
Register a post-hook for the
state_dict()method.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata) -> None
The registered hooks can modify the
state_dictinplace.
- register_state_dict_pre_hook(hook)[source]ο
Register a pre-hook for the
state_dict()method.- It should have the following signature::
hook(module, prefix, keep_vars) -> None
The registered hooks can be used to perform pre-processing before the
state_dictcall is made.
- requires_grad_(requires_grad=True)[source]ο
Change if autograd should record operations on parameters in this module.
This method sets the parametersβ
requires_gradattributes in-place.This method is helpful for freezing part of the module for finetuning or training parts of a model individually (e.g., GAN training).
See Locally disabling gradient computation for a comparison between .requires_grad_() and several similar mechanisms that may be confused with it.
- Parameters:
requires_grad (bool) β whether autograd should record operations on parameters in this module. Default:
True.- Returns:
self
- Return type:
Module
- save_pretrained(save_directory, push_to_hub=False, **kwargs)[source]ο
Save model weights and configuration to directory.
Creates a directory structure compatible with from_pretrained():
` save_directory/ βββ config.json # Model configuration βββ pytorch_model.bin # Model weights `- Parameters:
- Return type:
Example
>>> model.save_pretrained("./my-tokenizer") >>> # Later... >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer")
- set_extra_state(state)[source]ο
Set extra state contained in the loaded state_dict.
This function is called from
load_state_dict()to handle any extra state found within the state_dict. Implement this function and a correspondingget_extra_state()for your module if you need to store extra state within its state_dict.
- set_submodule(target, module, strict=False)[source]ο
Set the submodule given by
targetif it exists, otherwise throw an error.Note
If
strictis set toFalse(default), the method will replace an existing submodule or create a new submodule if the parent module exists. Ifstrictis set toTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule does not exist.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(3, 3, 3) ) (linear): Linear(3, 3) ) )(The diagram shows an
nn.ModuleA.Ahas a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To override the
Conv2dwith a new submoduleLinear, you could callset_submodule("net_b.net_c.conv", nn.Linear(1, 1))wherestrictcould beTrueorFalseTo add a new submodule
Conv2dto the existingnet_bmodule, you would callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1)).In the above if you set
strict=Trueand callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1), strict=True), an AttributeError will be raised becausenet_bdoes not have a submodule namedconv.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)module (
Module) β The module to set the submodule to.strict (
bool, default:False) β IfFalse, the method will replace an existing submodule or create a new submodule if the parent module exists. IfTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule doesnβt already exist.
- Raises:
ValueError β If the
targetstring is empty or ifmoduleis not an instance ofnn.Module.AttributeError β If at any point along the path resulting from the
targetstring the (sub)path resolves to a non-existent attribute name or an object that is not an instance ofnn.Module.
- Return type:
See
torch.Tensor.share_memory_().- Return type:
Self
- state_dict(*args, destination=None, prefix='', keep_vars=False)[source]ο
Return a dictionary containing references to the whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. Parameters and buffers set to
Noneare not included.Note
The returned object is a shallow copy. It contains references to the moduleβs parameters and buffers.
Warning
Currently
state_dict()also accepts positional arguments fordestination,prefixandkeep_varsin order. However, this is being deprecated and keyword arguments will be enforced in future releases.Warning
Please avoid the use of argument
destinationas it is not designed for end-users.- Parameters:
destination (dict, optional) β If provided, the state of module will be updated into the dict and the same object is returned. Otherwise, an
OrderedDictwill be created and returned. Default:None.prefix (str, optional) β a prefix added to parameter and buffer names to compose the keys in state_dict. Default:
''.keep_vars (bool, optional) β by default the
Tensors returned in the state dict are detached from autograd. If itβs set toTrue, detaching will not be performed. Default:False.
- Returns:
a dictionary containing a whole state of the module
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> module.state_dict().keys() ['bias', 'weight']
- to(*args, **kwargs)[source]ο
Move and/or cast the parameters and buffers.
This can be called as
- to(device=None, dtype=None, non_blocking=False)[source]
- to(dtype, non_blocking=False)[source]
- to(tensor, non_blocking=False)[source]
- to(memory_format=torch.channels_last)[source]
Its signature is similar to
torch.Tensor.to(), but only accepts floating point or complexdtypes. In addition, this method will only cast the floating point or complex parameters and buffers todtype(if given). The integral parameters and buffers will be moveddevice, if that is given, but with dtypes unchanged. Whennon_blockingis set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.See below for examples.
Note
This method modifies the module in-place.
- Parameters:
device (
torch.device) β the desired device of the parameters and buffers in this moduledtype (
torch.dtype) β the desired floating point or complex dtype of the parameters and buffers in this moduletensor (torch.Tensor) β Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module
memory_format (
torch.memory_format) β the desired memory format for 4D parameters and buffers in this module (keyword only argument)
- Returns:
self
- Return type:
Module
Examples:
>>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16) >>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble) >>> linear.weight Parameter containing: tensor([[ 0.3741+0.j, 0.2382+0.j], [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128) >>> linear(torch.ones(3, 2, dtype=torch.cdouble)) tensor([[0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
- to_empty(*, device, recurse=True)[source]ο
Move the parameters and buffers to the specified device without copying storage.
- Parameters:
device (
torch.device) β The desired device of the parameters and buffers in this module.recurse (bool) β Whether parameters and buffers of submodules should be recursively moved to the specified device.
- Returns:
self
- Return type:
Module
- tokenize(x)[source]ο
Encode input to latent representation (convenience method).
For inference, this is the primary encoding method. For VAE, returns the sampled latent (not the mean), enabling diverse reconstructions.
- Parameters:
x (
Float[Tensor, 'batch channels *spatial']) β Input tensor- Return type:
Float[Tensor, 'batch latent_channels *spatial_compressed']- Returns:
Latent tensor suitable for storage, manipulation, or decoding
- train(mode=True)[source]ο
Set the module in training mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e., whether they are affected, e.g.
Dropout,BatchNorm, etc.- Parameters:
mode (bool) β whether to set training mode (
True) or evaluation mode (False). Default:True.- Returns:
self
- Return type:
Module
- type(dst_type)[source]ο
Casts all parameters and buffers to
dst_type.Note
This method modifies the module in-place.
- Parameters:
dst_type (type or string) β the desired type
- Returns:
self
- Return type:
Module
- xpu(device=None)[source]ο
Move all model parameters and buffers to the XPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on XPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- zero_grad(set_to_none=True)[source]ο
Reset gradients of all model parameters.
See similar function under
torch.optim.Optimizerfor more context.- Parameters:
set_to_none (bool) β instead of setting to zero, set the grads to None. See
torch.optim.Optimizer.zero_grad()for details.- Return type:
- class medtokenizers.TiTokTokenizer(dim, in_channels=1, out_channels=None, num_tokens=32, num_embeddings=1024, embedding_dim=None, hidden_dim=256, num_heads=8, num_layers=4, patch_size=16, resolution=128, dropout=0.0, beta=0.25, use_norm=False, use_ema=False, ema_decay=0.99, reset_unused_codes=False, dead_code_threshold=100, name='TiTokTokenizer')[source][source]ο
Bases:
medtokenizers.modules.base.BaseTokenizerTransformer-based 1D image tokenizer (TiTok).
Implements the TiTok idea (Yu et al., 2024) for 2D and 3D medical images: instead of producing a spatial grid of codes, the image is compressed into a flat sequence of
num_tokenslearnable 1D latent tokens. The input is split into non-overlapping patches and embedded; a learnable set of latent tokens is appended and a Transformer encoder lets those latent tokens attend over all patches. The latent tokens are then vector-quantized into discrete indices. Decoding mirrors this: quantized latents plus a learnable mask token per patch are passed through a Transformer decoder, and the patch outputs are un-embedded and reassembled into an image.Because the latent is a fixed-length 1D sequence (independent of spatial resolution), TiTok is well suited to autoregressive / sequence models. Note that the input spatial size is fixed: it must equal
resolution(validated at encode time), andresolutionmust be divisible bypatch_size.- Patch ordering:
2D: patches are flattened in (height, width) row-major order, i.e. token index
i = row * grid_w + col. Each patch vector concatenates its pixels as(p_h, p_w, channels).3D: patches are flattened in (depth, height, width) order following the
(g_h, g_w, g_d)grid, with each patch vector laid out as(p_h, p_w, p_d, channels).
_patchify/_unpatchifyare exact inverses, so decode restores the original spatial layout.- Parameters:
dim (
int) β Spatial dimensionality,2or3.in_channels (
int, default:1) β Number of input image channels.out_channels (
int|None, default:None) β Number of reconstructed output channels. Defaults toin_channelswhenNone.num_tokens (
int, default:32) β Number of 1D latent tokens (the compressed sequence length).num_embeddings (
int, default:1024) β Codebook size of the vector quantizer (vocabulary).embedding_dim (
int|None, default:None) β Dimensionality of each quantized code. Defaults tohidden_dimwhenNone.hidden_dim (
int, default:256) β Transformer model width; must be divisible bynum_heads.num_heads (
int, default:8) β Number of attention heads in encoder and decoder.num_layers (
int, default:4) β Number of Transformer layers in encoder and decoder.patch_size (
Union[int,Iterable[int]], default:16) β Patch edge length. Either a single int (applied to every spatial axis) or a per-axis iterable of lengthdim.resolution (
Union[int,Iterable[int]], default:128) β Expected input spatial size. Either a single int or a per-axis iterable of lengthdim. Must be divisible bypatch_sizealong every axis.dropout (
float, default:0.0) β Dropout probability in[0, 1)applied to embeddings and within Transformer layers.beta (
float, default:0.25) β Commitment loss weight for the vector quantizer.use_norm (
bool, default:False) β Whether the quantizer L2-normalizes codes/inputs.use_ema (
bool, default:False) β Whether the quantizer updates its codebook via EMA.ema_decay (
float, default:0.99) β EMA decay used whenuse_emaisTrue.reset_unused_codes (
bool, default:False) β Whether to reset dead codebook entries.dead_code_threshold (
int, default:100) β Usage count below which a code is considered dead.name (
str, default:'TiTokTokenizer') β Human-readable tokenizer name.
- Shapes:
Input:
(B, in_channels, *resolution)where*resolutionis(H, W)for 2D or(H, W, D)for 3D.Discrete indices:
(B, num_tokens)(integer dtype).Quantized latents:
(B, num_tokens, embedding_dim).Reconstruction:
(B, out_channels, *resolution).
- __init__(dim, in_channels=1, out_channels=None, num_tokens=32, num_embeddings=1024, embedding_dim=None, hidden_dim=256, num_heads=8, num_layers=4, patch_size=16, resolution=128, dropout=0.0, beta=0.25, use_norm=False, use_ema=False, ema_decay=0.99, reset_unused_codes=False, dead_code_threshold=100, name='TiTokTokenizer')[source][source]ο
- Parameters:
dim (
int)in_channels (
int, default:1)num_tokens (
int, default:32)num_embeddings (
int, default:1024)hidden_dim (
int, default:256)num_heads (
int, default:8)num_layers (
int, default:4)dropout (
float, default:0.0)beta (
float, default:0.25)use_norm (
bool, default:False)use_ema (
bool, default:False)ema_decay (
float, default:0.99)reset_unused_codes (
bool, default:False)dead_code_threshold (
int, default:100)name (
str, default:'TiTokTokenizer')
- encode(x)[source][source]ο
Encode an image into discrete 1D latent tokens.
- Parameters:
x (
Float[Tensor, 'batch channels *spatial']) β Input image of shape(B, in_channels, *resolution).- Return type:
tuple[Int[Tensor, 'batch num_tokens'],Float[Tensor, 'batch num_tokens embedding_dim'],Tensor]- Returns:
Tuple of
(indices, quantized, quant_loss)whereindiceshas shape(B, num_tokens)(integer dtype),quantizedhas shape(B, num_tokens, embedding_dim), andquant_lossis the quantizer commitment/codebook loss.
- indices_to_codes(indices)[source][source]ο
Look up quantized code vectors for a batch of token indices.
- Parameters:
indices (
Int[Tensor, 'batch num_tokens']) β Integer indices of shape(B, num_tokens).- Return type:
Float[Tensor, 'batch num_tokens embedding_dim']- Returns:
Quantized code vectors of shape
(B, num_tokens, embedding_dim).
- decode(quantized)[source][source]ο
Decode quantized 1D latent tokens back into an image.
- Parameters:
quantized (
Float[Tensor, 'batch num_tokens embedding_dim']) β Quantized code vectors of shape(B, num_tokens, embedding_dim).- Return type:
Float[Tensor, 'batch channels *spatial']- Returns:
Reconstructed image of shape
(B, out_channels, *resolution).
- forward(x)[source][source]ο
Encode then decode
x, returning training or eval outputs.- Parameters:
x (
Float[Tensor, 'batch channels *spatial']) β Input image of shape(B, in_channels, *resolution).- Return type:
- Returns:
In training mode, a dict with
reconstructions,quant_loss,quant_info(indices) andlatents(quantized). In eval mode, aNetworkEvalnamed tuple.
- tokenize(x)[source][source]ο
Encode
xto discrete token indices of shape(B, num_tokens).- Parameters:
x (
Float[Tensor, 'batch channels *spatial'])- Return type:
Int[Tensor, 'batch num_tokens']
- detokenize(indices)[source][source]ο
Decode discrete token indices back into an image.
Inverse of
tokenize(). This is the canonical index-to-reconstruction decoding path.- Parameters:
indices (
Int[Tensor, 'batch num_tokens']) β Integer indices of shape(B, num_tokens).- Return type:
Float[Tensor, 'batch channels *spatial']- Returns:
Reconstructed image of shape
(B, out_channels, *resolution).
- T_destination = ~T_destinationο
- add_module(name, module)[source]ο
Add a child module to the current module.
The module can be accessed as an attribute using the given name.
- apply(fn)[source]ο
Apply
fnrecursively to every submodule (as returned by.children()) as well as self.Typical use includes initializing the parameters of a model (see also torch.nn.init).
- Parameters:
fn (
Module-> None) β function to be applied to each submodule- Returns:
self
- Return type:
Module
Example:
>>> @torch.no_grad() >>> def init_weights(m): >>> print(m) >>> if type(m) is nn.Linear: >>> m.weight.fill_(1.0) >>> print(m.weight) >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )
- bfloat16()[source]ο
Casts all floating point parameters and buffers to
bfloat16datatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- buffers(recurse=True)[source]ο
Return an iterator over module buffers.
- Parameters:
recurse (bool) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module.
- Yields:
torch.Tensor β module buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for buf in model.buffers(): >>> print(type(buf), buf.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- compile(mode='reduce-overhead', fullgraph=False, **kwargs)[source]ο
Compile model with torch.compile() for faster inference.
Uses PyTorch 2.0+ compilation to optimize the model graph. The compiled model maintains the same interface but runs faster, especially for repeated inference calls.
- Parameters:
mode (
str, default:'reduce-overhead') β Compilation mode. Options: - βreduce-overheadβ: Best for small batches (default) - βmax-autotuneβ: Best throughput, longer warmup - βdefaultβ: Balanced compilationfullgraph (
bool, default:False) β If True, require full graph compilation (stricter)**kwargs β Additional arguments passed to torch.compile()
- Return type:
- Returns:
Compiled model (self, for method chaining)
Example
>>> model = ContinuousTokenizer.from_pretrained("path/to/model") >>> model = model.compile(mode="reduce-overhead") >>> # First call triggers compilation (slower) >>> latents = model.tokenize(batch) >>> # Subsequent calls are faster >>> latents = model.tokenize(batch2)
Note
Compilation happens on first forward pass
Different input shapes may trigger recompilation
Use dynamic=False (default) for fixed input sizes
- cpu()[source]ο
Move all model parameters and buffers to the CPU.
Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- cuda(device=None)[source]ο
Move all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on GPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- decode_batch(z, batch_size=8, show_progress=False)[source]ο
Decode large batch with automatic mini-batching.
- double()[source]ο
Casts all floating point parameters and buffers to
doubledatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- encode_batch(x, batch_size=8, show_progress=False)[source]ο
Encode large batch with automatic mini-batching.
Processes input in chunks to avoid OOM for large datasets.
- Parameters:
- Return type:
- Returns:
Concatenated latents for all inputs
Example
>>> dataset = torch.randn(1000, 1, 64, 64, 64) >>> latents = model.encode_batch(dataset, batch_size=4)
- eval()[source]ο
Set the module in evaluation mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e. whether they are affected, e.g.
Dropout,BatchNorm, etc.This is equivalent with
self.train(False).See Locally disabling gradient computation for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns:
self
- Return type:
Module
- extra_repr()[source]ο
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- Return type:
- float()[source]ο
Casts all floating point parameters and buffers to
floatdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- classmethod from_pretrained(model_name_or_path, map_location=None, **kwargs)[source]ο
Load model from local directory or HuggingFace Hub.
Automatically detects whether path is local or a Hub repository. For Hub repos, downloads config and weights to cache.
- Parameters:
- Return type:
- Returns:
Loaded model instance
Example
>>> # From local path >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer") >>> >>> # From HuggingFace Hub >>> model = ContinuousTokenizer.from_pretrained("username/my-tokenizer") >>> >>> # Override config >>> model = ContinuousTokenizer.from_pretrained( ... "./my-tokenizer", ... dropout=0.1 # Override saved dropout value ... )
- classmethod from_pretrained_encoder_decoder(pretrained_path, strict=False, verbose=True, **kwargs)[source]ο
Create model and load encoder/decoder weights from pretrained checkpoint.
Factory method that creates a new model instance and loads encoder/decoder weights in one step. Useful for initializing new architectures from pretrained backbones.
- Parameters:
- Return type:
- Returns:
New model instance with loaded encoder/decoder weights
Example
>>> # Create FSQ model initialized from VAE weights >>> model = DiscreteTokenizer.from_pretrained_encoder_decoder( ... 'weights/maisi_converted.pt', ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... z_channels=256, ... channels=64, ... channels_mult=(1, 2, 4), ... )
- get_buffer(target)[source]ο
Return the buffer given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the buffer to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The buffer referenced by
target- Return type:
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not a buffer
- get_extra_state()[source]ο
Return any extra state to include in the moduleβs state_dict.
Implement this and a corresponding
set_extra_state()for your module if you need to store extra state. This function is called when building the moduleβs state_dict().Note that extra state should be picklable to ensure working serialization of the state_dict. We only provide backwards compatibility guarantees for serializing Tensors; other objects may break backwards compatibility if their serialized pickled form changes.
- Returns:
Any extra state to store in the moduleβs state_dict
- Return type:
- get_parameter(target)[source]ο
Return the parameter given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the Parameter to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The Parameter referenced by
target- Return type:
torch.nn.Parameter
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not an
nn.Parameter
- get_submodule(target)[source]ο
Return the submodule given by
targetif it exists, otherwise throw an error.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(16, 33, kernel_size=(3, 3), stride=(2, 2)) ) (linear): Linear(in_features=100, out_features=200, bias=True) ) )(The diagram shows an
nn.ModuleA.Awhich has a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To check whether or not we have the
linearsubmodule, we would callget_submodule("net_b.linear"). To check whether we have theconvsubmodule, we would callget_submodule("net_b.net_c.conv").The runtime of
get_submoduleis bounded by the degree of module nesting intarget. A query againstnamed_modulesachieves the same result, but it is O(N) in the number of transitive modules. So, for a simple check to see if some submodule exists,get_submoduleshould always be used.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)- Returns:
The submodule referenced by
target- Return type:
- Raises:
AttributeError β If at any point along the path resulting from the target string the (sub)path resolves to a non-existent attribute name or an object that is not an instance of
nn.Module.
- half()[source]ο
Casts all floating point parameters and buffers to
halfdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- inference_mode()[source]ο
Context manager for optimized inference.
Configures the model for maximum inference performance: - Sets model to eval mode - Disables gradient computation - Uses torch.inference_mode for additional optimizations
The model is restored to its previous state upon exit.
- Return type:
Example
>>> with model.inference_mode(): ... latents = model.tokenize(volume) ... recon = model.detokenize(latents)
- Yields:
self β The model instance for method chaining
- ipu(device=None)[source]ο
Move all model parameters and buffers to the IPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on IPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- load_encoder_decoder_weights(pretrained_path, strict=False, verbose=True)[source]ο
Load encoder/decoder weights from a pretrained checkpoint.
This method enables transfer learning by loading encoder and decoder weights from any compatible tokenizer (VAE, VQ-VAE, FSQ, etc.) while leaving quantizer-specific layers randomly initialized.
Use Casesο
Initialize discrete tokenizer from VAE: Load MAISI VAE encoder/decoder, then train only the quantizer layers (VQ, FSQ, etc.)
Fine-tune on new domain: Start from pretrained weights, fine-tune all
Encoder-only transfer: Use pretrained encoder for downstream tasks
How It Worksο
The method identifies encoder/decoder weights by key prefixes and loads only those that match. Quantizer-specific layers (quant_conv, post_quant_conv, quantizer) may or may not be loaded depending on architecture compatibility.
Weight Matching Strategy: - encoder.* keys: Always attempted - decoder.* keys: Always attempted - quant_conv.* keys: Loaded if shapes match - post_quant_conv.* keys: Loaded if shapes match - quantizer.* keys: Skipped (architecture-specific)
- type pretrained_path:
- param pretrained_path:
Path to pretrained weights file (.pt, .bin) or directory containing βpytorch_model.binβ
- type strict:
bool, default:False- param strict:
If True, raise error on shape mismatches. If False (default), skip mismatched weights and log warnings.
- type verbose:
bool, default:True- param verbose:
If True, print summary of loaded/skipped weights
- rtype:
- returns:
Tuple of (loaded_keys, skipped_keys) for inspection
Example
>>> # Initialize FSQ tokenizer from MAISI VAE weights >>> model = DiscreteTokenizer( ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... # ... other args matching MAISI architecture ... ) >>> loaded, skipped = model.load_encoder_decoder_weights( ... 'weights/maisi_converted.pt', ... verbose=True ... ) >>> print(f"Loaded {len(loaded)} keys, skipped {len(skipped)}") >>> >>> # Now train with frozen encoder if desired >>> for param in model.encoder.parameters(): ... param.requires_grad = False
Note
For best results, ensure the pretrained model has the same: - channels, channels_mult, num_res_blocks - spatial_compression
The following can differ: - z_channels, latent_channels, embedding_dim - Quantizer type and configuration
- load_state_dict(state_dict, strict=True, assign=False)[source]ο
Copy parameters and buffers from
state_dictinto this module and its descendants.If
strictisTrue, then the keys ofstate_dictmust exactly match the keys returned by this moduleβsstate_dict()function.Warning
If
assignisTruethe optimizer must be created after the call toload_state_dictunlessget_swap_module_params_on_conversion()isTrue.- Parameters:
state_dict (dict) β a dict containing parameters and persistent buffers.
strict (bool, optional) β whether to strictly enforce that the keys in
state_dictmatch the keys returned by this moduleβsstate_dict()function. Default:Trueassign (bool, optional) β When set to
False, the properties of the tensors in the current module are preserved whereas setting it toTruepreserves properties of the Tensors in the state dict. The only exception is therequires_gradfield ofParameterfor which the value from the module is preserved. Default:False
- Returns:
missing_keysis a list of str containing any keys that are expectedby this module but missing from the provided
state_dict.
unexpected_keysis a list of str containing the keys that are notexpected by this module but present in the provided
state_dict.
- Return type:
NamedTuplewithmissing_keysandunexpected_keysfields
Note
If a parameter or buffer is registered as
Noneand its corresponding key exists instate_dict,load_state_dict()will raise aRuntimeError.
- modules(remove_duplicate=True)[source]ο
Return an iterator over all modules in the network.
- Parameters:
remove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not.- Yields:
Module β a module in the network
- Return type:
Note
Duplicate modules are returned only once by default. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): ... print(idx, '->', m) 0 -> Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) ) 1 -> Linear(in_features=2, out_features=2, bias=True)
- mtia(device=None)[source]ο
Move all model parameters and buffers to the MTIA.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on MTIA while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- named_buffers(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
- Parameters:
prefix (str) β prefix to prepend to all buffer names.
recurse (bool, optional) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module. Defaults to True.
remove_duplicate (bool, optional) β whether to remove the duplicated buffers in the result. Defaults to True.
- Yields:
(str, torch.Tensor) β Tuple containing the name and buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, buf in self.named_buffers(): >>> if name in ['running_var']: >>> print(buf.size())
- named_children()[source]ο
Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
- Yields:
(str, Module) β Tuple containing a name and child module
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module)
- named_modules(memo=None, prefix='', remove_duplicate=True)[source]ο
Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
- Parameters:
memo (
set[Module] |None, default:None) β a memo to store the set of modules already added to the resultprefix (
str, default:'') β a prefix that will be added to the name of the moduleremove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not
- Yields:
(str, Module) β Tuple of name and module
Note
Duplicate modules are returned only once. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): ... print(idx, '->', m) 0 -> ('', Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )) 1 -> ('0', Linear(in_features=2, out_features=2, bias=True))
- named_parameters(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
- Parameters:
prefix (str) β prefix to prepend to all parameter names.
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
remove_duplicate (bool, optional) β whether to remove the duplicated parameters in the result. Defaults to True.
- Yields:
(str, Parameter) β Tuple containing the name and parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size())
- num_parameters()[source]ο
Get total number of learnable parameters.
- Return type:
- Returns:
Sum of numel() for all parameters
- parameters(recurse=True)[source]ο
Return an iterator over module parameters.
This is typically passed to an optimizer.
- Parameters:
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
- Yields:
Parameter β module parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for param in model.parameters(): >>> print(type(param), param.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- push_to_hub(repo_id, save_directory=None, commit_message='Upload model', private=False, **kwargs)[source]ο
Upload model to HuggingFace Hub.
Creates or updates a repository on the Hub with model weights and configuration.
- Parameters:
repo_id (
str) β Repository ID (e.g., βusername/model-nameβ)save_directory (
Optional[str], default:None) β Local directory to save before upload (auto-created if None)commit_message (
str, default:'Upload model') β Commit message for the uploadprivate (
bool, default:False) β Whether to create a private repository**kwargs β Additional arguments for HfApi.upload_folder
- Return type:
Example
>>> model.push_to_hub("username/my-vae-tokenizer") >>> # Creates https://huggingface.co/username/my-vae-tokenizer
- reconstruct(x, roi_size=None, overlap=0.5, sw_batch_size=1)[source]ο
Full encode-decode reconstruction with optional sliding window.
For large 3D volumes that exceed GPU memory, this method implements sliding window inference with Gaussian importance weighting to seamlessly blend overlapping patches.
The Algorithmο
Pad input to multiple of stride + window size
Extract overlapping windows with specified stride
Process windows in batches through tokenize -> detokenize
Weight each windowβs contribution by Gaussian importance
Normalize by accumulated importance and crop to original size
Gaussian Weightingο
Uses a Gaussian importance map (2D or 3D based on input) that gives higher weight to the center of each window, reducing boundary artifacts when blending.
- type x:
- param x:
Input tensor of shape (B, C, H, W, D) for 3D or (B, C, H, W) for 2D
- type roi_size:
- param roi_size:
Size of sliding window. If None, processes entire volume. Can be int (isotropic) or tuple (anisotropic).
- type overlap:
float, default:0.5- param overlap:
Fraction of overlap between windows (0.0 to 0.9). Higher overlap = smoother blending but more compute.
- type sw_batch_size:
int, default:1- param sw_batch_size:
Number of windows to process in parallel per batch. Higher values use more GPU memory but are faster. Default is 1 (sequential processing).
- rtype:
- returns:
Reconstructed tensor with same shape as input
Example
>>> volume = torch.randn(1, 1, 256, 256, 256) # 256Β³ volume >>> # Process in 128Β³ windows with 50% overlap, 4 windows at a time >>> recon = model.reconstruct(volume, roi_size=128, overlap=0.5, sw_batch_size=4)
Note
For volumes that fit in memory, omit roi_size for faster processing
Overlap of 0.5 is a good default; higher values reduce artifacts but increase computation proportionally
sw_batch_size > 1 can significantly speed up inference on GPUs with sufficient memory
- register_backward_hook(hook)[source]ο
Register a backward hook on the module.
This function is deprecated in favor of
register_full_backward_hook()and the behavior of this function will change in future versions.
- register_buffer(name, tensor, persistent=True)[source]ο
Add a buffer to the module.
This is typically used to register a buffer that should not be considered a model parameter. For example, BatchNormβs
running_meanis not a parameter, but is part of the moduleβs state. Buffers, by default, are persistent and will be saved alongside parameters. This behavior can be changed by settingpersistenttoFalse. The only difference between a persistent buffer and a non-persistent buffer is that the latter will not be a part of this moduleβsstate_dict.Buffers can be accessed as attributes using given names.
- Parameters:
name (str) β name of the buffer. The buffer can be accessed from this module using the given name
tensor (Tensor or None) β buffer to be registered. If
None, then operations that run on buffers, such ascuda, are ignored. IfNone, the buffer is not included in the moduleβsstate_dict.persistent (bool) β whether the buffer is part of this moduleβs
state_dict.
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> self.register_buffer('running_mean', torch.zeros(num_features))
- register_forward_hook(hook, *, prepend=False, with_kwargs=False, always_call=False)[source]ο
Register a forward hook on the module.
The hook will be called every time after
forward()has computed an output.If
with_kwargsisFalseor not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the output. It can modify the input inplace but it will not have effect on forward since this is called afterforward()is called. The hook should have the following signature:hook(module, args, output) -> None or modified output
If
with_kwargsisTrue, the forward hook will be passed thekwargsgiven to the forward function and be expected to return the output possibly modified. The hook should have the following signature:hook(module, args, kwargs, output) -> None or modified output
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If
True, the providedhookwill be fired before all existingforwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforwardhooks on thistorch.nn.Module. Note that globalforwardhooks registered withregister_module_forward_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If
True, thehookwill be passed the kwargs given to the forward function. Default:Falsealways_call (bool) β If
Truethehookwill be run regardless of whether an exception is raised while calling the Module. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_forward_pre_hook(hook, *, prepend=False, with_kwargs=False)[source]ο
Register a forward pre-hook on the module.
The hook will be called every time before
forward()is invoked.If
with_kwargsis false or not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the input. User can either return a tuple or a single modified value in the hook. We will wrap the value into a tuple if a single value is returned (unless that value is already a tuple). The hook should have the following signature:hook(module, args) -> None or modified input
If
with_kwargsis true, the forward pre-hook will be passed the kwargs given to the forward function. And if the hook modifies the input, both the args and kwargs should be returned. The hook should have the following signature:hook(module, args, kwargs) -> None or a tuple of modified input and kwargs
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingforward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforward_prehooks on thistorch.nn.Module. Note that globalforward_prehooks registered withregister_module_forward_pre_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If true, the
hookwill be passed the kwargs given to the forward function. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_hook(hook, prepend=False)[source]ο
Register a backward hook on the module.
The hook will be called every time the gradients with respect to a module are computed, and its firing rules are as follows:
Ordinarily, the hook fires when the gradients are computed with respect to the module inputs.
If none of the module inputs require gradients, the hook will fire when the gradients are computed with respect to module outputs.
If none of the module outputs require gradients, then the hooks will not fire.
The hook should have the following signature:
hook(module, grad_input, grad_output) -> tuple(Tensor) or None
The
grad_inputandgrad_outputare tuples that contain the gradients with respect to the inputs and outputs respectively. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the input that will be used in place ofgrad_inputin subsequent computations.grad_inputwill only correspond to the inputs given as positional arguments and all kwarg arguments are ignored. Entries ingrad_inputandgrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs or outputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackwardhooks on thistorch.nn.Module. Note that globalbackwardhooks registered withregister_module_full_backward_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_pre_hook(hook, prepend=False)[source]ο
Register a backward pre-hook on the module.
The hook will be called every time the gradients for the module are computed. The hook should have the following signature:
hook(module, grad_output) -> tuple[Tensor, ...], Tensor or None
The
grad_outputis a tuple. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the output that will be used in place ofgrad_outputin subsequent computations. Entries ingrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackward_prehooks on thistorch.nn.Module. Note that globalbackward_prehooks registered withregister_module_full_backward_pre_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_post_hook(hook)[source]ο
Register a post-hook to be run after moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, incompatible_keys) -> None
The
moduleargument is the current module that this hook is registered on, and theincompatible_keysargument is aNamedTupleconsisting of attributesmissing_keysandunexpected_keys.missing_keysis alistofstrcontaining the missing keys andunexpected_keysis alistofstrcontaining the unexpected keys.The given incompatible_keys can be modified inplace if needed.
Note that the checks performed when calling
load_state_dict()withstrict=Trueare affected by modifications the hook makes tomissing_keysorunexpected_keys, as expected. Additions to either set of keys will result in an error being thrown whenstrict=True, and clearing out both missing and unexpected keys will avoid an error.- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_pre_hook(hook)[source]ο
Register a pre-hook to be run before moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) -> None # noqa: B950
- Parameters:
hook (Callable) β Callable hook that will be invoked before loading the state dict.
- register_module(name, module)[source]ο
Alias for
add_module().
- register_parameter(name, param)[source]ο
Add a parameter to the module.
The parameter can be accessed as an attribute using given name.
- Parameters:
name (str) β name of the parameter. The parameter can be accessed from this module using the given name
param (Parameter or None) β parameter to be added to the module. If
None, then operations that run on parameters, such ascuda, are ignored. IfNone, the parameter is not included in the moduleβsstate_dict.
- Return type:
- register_state_dict_post_hook(hook)[source]ο
Register a post-hook for the
state_dict()method.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata) -> None
The registered hooks can modify the
state_dictinplace.
- register_state_dict_pre_hook(hook)[source]ο
Register a pre-hook for the
state_dict()method.- It should have the following signature::
hook(module, prefix, keep_vars) -> None
The registered hooks can be used to perform pre-processing before the
state_dictcall is made.
- requires_grad_(requires_grad=True)[source]ο
Change if autograd should record operations on parameters in this module.
This method sets the parametersβ
requires_gradattributes in-place.This method is helpful for freezing part of the module for finetuning or training parts of a model individually (e.g., GAN training).
See Locally disabling gradient computation for a comparison between .requires_grad_() and several similar mechanisms that may be confused with it.
- Parameters:
requires_grad (bool) β whether autograd should record operations on parameters in this module. Default:
True.- Returns:
self
- Return type:
Module
- save_pretrained(save_directory, push_to_hub=False, **kwargs)[source]ο
Save model weights and configuration to directory.
Creates a directory structure compatible with from_pretrained():
` save_directory/ βββ config.json # Model configuration βββ pytorch_model.bin # Model weights `- Parameters:
- Return type:
Example
>>> model.save_pretrained("./my-tokenizer") >>> # Later... >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer")
- set_extra_state(state)[source]ο
Set extra state contained in the loaded state_dict.
This function is called from
load_state_dict()to handle any extra state found within the state_dict. Implement this function and a correspondingget_extra_state()for your module if you need to store extra state within its state_dict.
- set_submodule(target, module, strict=False)[source]ο
Set the submodule given by
targetif it exists, otherwise throw an error.Note
If
strictis set toFalse(default), the method will replace an existing submodule or create a new submodule if the parent module exists. Ifstrictis set toTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule does not exist.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(3, 3, 3) ) (linear): Linear(3, 3) ) )(The diagram shows an
nn.ModuleA.Ahas a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To override the
Conv2dwith a new submoduleLinear, you could callset_submodule("net_b.net_c.conv", nn.Linear(1, 1))wherestrictcould beTrueorFalseTo add a new submodule
Conv2dto the existingnet_bmodule, you would callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1)).In the above if you set
strict=Trueand callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1), strict=True), an AttributeError will be raised becausenet_bdoes not have a submodule namedconv.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)module (
Module) β The module to set the submodule to.strict (
bool, default:False) β IfFalse, the method will replace an existing submodule or create a new submodule if the parent module exists. IfTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule doesnβt already exist.
- Raises:
ValueError β If the
targetstring is empty or ifmoduleis not an instance ofnn.Module.AttributeError β If at any point along the path resulting from the
targetstring the (sub)path resolves to a non-existent attribute name or an object that is not an instance ofnn.Module.
- Return type:
See
torch.Tensor.share_memory_().- Return type:
Self
- state_dict(*args, destination=None, prefix='', keep_vars=False)[source]ο
Return a dictionary containing references to the whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. Parameters and buffers set to
Noneare not included.Note
The returned object is a shallow copy. It contains references to the moduleβs parameters and buffers.
Warning
Currently
state_dict()also accepts positional arguments fordestination,prefixandkeep_varsin order. However, this is being deprecated and keyword arguments will be enforced in future releases.Warning
Please avoid the use of argument
destinationas it is not designed for end-users.- Parameters:
destination (dict, optional) β If provided, the state of module will be updated into the dict and the same object is returned. Otherwise, an
OrderedDictwill be created and returned. Default:None.prefix (str, optional) β a prefix added to parameter and buffer names to compose the keys in state_dict. Default:
''.keep_vars (bool, optional) β by default the
Tensors returned in the state dict are detached from autograd. If itβs set toTrue, detaching will not be performed. Default:False.
- Returns:
a dictionary containing a whole state of the module
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> module.state_dict().keys() ['bias', 'weight']
- to(*args, **kwargs)[source]ο
Move and/or cast the parameters and buffers.
This can be called as
- to(device=None, dtype=None, non_blocking=False)[source]
- to(dtype, non_blocking=False)[source]
- to(tensor, non_blocking=False)[source]
- to(memory_format=torch.channels_last)[source]
Its signature is similar to
torch.Tensor.to(), but only accepts floating point or complexdtypes. In addition, this method will only cast the floating point or complex parameters and buffers todtype(if given). The integral parameters and buffers will be moveddevice, if that is given, but with dtypes unchanged. Whennon_blockingis set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.See below for examples.
Note
This method modifies the module in-place.
- Parameters:
device (
torch.device) β the desired device of the parameters and buffers in this moduledtype (
torch.dtype) β the desired floating point or complex dtype of the parameters and buffers in this moduletensor (torch.Tensor) β Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module
memory_format (
torch.memory_format) β the desired memory format for 4D parameters and buffers in this module (keyword only argument)
- Returns:
self
- Return type:
Module
Examples:
>>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16) >>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble) >>> linear.weight Parameter containing: tensor([[ 0.3741+0.j, 0.2382+0.j], [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128) >>> linear(torch.ones(3, 2, dtype=torch.cdouble)) tensor([[0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
- to_empty(*, device, recurse=True)[source]ο
Move the parameters and buffers to the specified device without copying storage.
- Parameters:
device (
torch.device) β The desired device of the parameters and buffers in this module.recurse (bool) β Whether parameters and buffers of submodules should be recursively moved to the specified device.
- Returns:
self
- Return type:
Module
- train(mode=True)[source]ο
Set the module in training mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e., whether they are affected, e.g.
Dropout,BatchNorm, etc.- Parameters:
mode (bool) β whether to set training mode (
True) or evaluation mode (False). Default:True.- Returns:
self
- Return type:
Module
- type(dst_type)[source]ο
Casts all parameters and buffers to
dst_type.Note
This method modifies the module in-place.
- Parameters:
dst_type (type or string) β the desired type
- Returns:
self
- Return type:
Module
- xpu(device=None)[source]ο
Move all model parameters and buffers to the XPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on XPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- zero_grad(set_to_none=True)[source]ο
Reset gradients of all model parameters.
See similar function under
torch.optim.Optimizerfor more context.- Parameters:
set_to_none (bool) β instead of setting to zero, set the grads to None. See
torch.optim.Optimizer.zero_grad()for details.- Return type:
- class medtokenizers.RAETokenizer(dim, encoder_type, encoder_name_or_path, out_channels=1, latent_dim=None, patch_size=None, encoder_image_size=None, encoder_drop_cls_token=True, encoder_kwargs=None, decoder_hidden_dim=1024, decoder_num_layers=2, decoder_dropout=0.0, noise_tau=0.0, latent_stats_path=None, latent_eps=1e-05, name='RAETokenizer')[source][source]ο
Bases:
medtokenizers.modules.base.BaseTokenizerRepresentation Autoencoder (RAE) tokenizer with a frozen encoder.
- Parameters:
dim (
int)encoder_type (
Literal['vit','medsiglip','neurovfm'])encoder_name_or_path (
str)out_channels (
int, default:1)encoder_drop_cls_token (
bool, default:True)decoder_hidden_dim (
int, default:1024)decoder_num_layers (
int, default:2)decoder_dropout (
float, default:0.0)noise_tau (
float, default:0.0)latent_eps (
float, default:1e-05)name (
str, default:'RAETokenizer')
- __init__(dim, encoder_type, encoder_name_or_path, out_channels=1, latent_dim=None, patch_size=None, encoder_image_size=None, encoder_drop_cls_token=True, encoder_kwargs=None, decoder_hidden_dim=1024, decoder_num_layers=2, decoder_dropout=0.0, noise_tau=0.0, latent_stats_path=None, latent_eps=1e-05, name='RAETokenizer')[source][source]ο
- Parameters:
dim (
int)encoder_type (
Literal['vit','medsiglip','neurovfm'])encoder_name_or_path (
str)out_channels (
int, default:1)encoder_drop_cls_token (
bool, default:True)decoder_hidden_dim (
int, default:1024)decoder_num_layers (
int, default:2)decoder_dropout (
float, default:0.0)noise_tau (
float, default:0.0)latent_eps (
float, default:1e-05)name (
str, default:'RAETokenizer')
- T_destination = ~T_destinationο
- add_module(name, module)[source]ο
Add a child module to the current module.
The module can be accessed as an attribute using the given name.
- apply(fn)[source]ο
Apply
fnrecursively to every submodule (as returned by.children()) as well as self.Typical use includes initializing the parameters of a model (see also torch.nn.init).
- Parameters:
fn (
Module-> None) β function to be applied to each submodule- Returns:
self
- Return type:
Module
Example:
>>> @torch.no_grad() >>> def init_weights(m): >>> print(m) >>> if type(m) is nn.Linear: >>> m.weight.fill_(1.0) >>> print(m.weight) >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Linear(in_features=2, out_features=2, bias=True) Parameter containing: tensor([[1., 1.], [1., 1.]], requires_grad=True) Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )
- bfloat16()[source]ο
Casts all floating point parameters and buffers to
bfloat16datatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- buffers(recurse=True)[source]ο
Return an iterator over module buffers.
- Parameters:
recurse (bool) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module.
- Yields:
torch.Tensor β module buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for buf in model.buffers(): >>> print(type(buf), buf.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- compile(mode='reduce-overhead', fullgraph=False, **kwargs)[source]ο
Compile model with torch.compile() for faster inference.
Uses PyTorch 2.0+ compilation to optimize the model graph. The compiled model maintains the same interface but runs faster, especially for repeated inference calls.
- Parameters:
mode (
str, default:'reduce-overhead') β Compilation mode. Options: - βreduce-overheadβ: Best for small batches (default) - βmax-autotuneβ: Best throughput, longer warmup - βdefaultβ: Balanced compilationfullgraph (
bool, default:False) β If True, require full graph compilation (stricter)**kwargs β Additional arguments passed to torch.compile()
- Return type:
- Returns:
Compiled model (self, for method chaining)
Example
>>> model = ContinuousTokenizer.from_pretrained("path/to/model") >>> model = model.compile(mode="reduce-overhead") >>> # First call triggers compilation (slower) >>> latents = model.tokenize(batch) >>> # Subsequent calls are faster >>> latents = model.tokenize(batch2)
Note
Compilation happens on first forward pass
Different input shapes may trigger recompilation
Use dynamic=False (default) for fixed input sizes
- cpu()[source]ο
Move all model parameters and buffers to the CPU.
Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- cuda(device=None)[source]ο
Move all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on GPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- decode_batch(z, batch_size=8, show_progress=False)[source]ο
Decode large batch with automatic mini-batching.
- double()[source]ο
Casts all floating point parameters and buffers to
doubledatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- encode_batch(x, batch_size=8, show_progress=False)[source]ο
Encode large batch with automatic mini-batching.
Processes input in chunks to avoid OOM for large datasets.
- Parameters:
- Return type:
- Returns:
Concatenated latents for all inputs
Example
>>> dataset = torch.randn(1000, 1, 64, 64, 64) >>> latents = model.encode_batch(dataset, batch_size=4)
- eval()[source]ο
Set the module in evaluation mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e. whether they are affected, e.g.
Dropout,BatchNorm, etc.This is equivalent with
self.train(False).See Locally disabling gradient computation for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns:
self
- Return type:
Module
- extra_repr()[source]ο
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- Return type:
- float()[source]ο
Casts all floating point parameters and buffers to
floatdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- classmethod from_pretrained(model_name_or_path, map_location=None, **kwargs)[source]ο
Load model from local directory or HuggingFace Hub.
Automatically detects whether path is local or a Hub repository. For Hub repos, downloads config and weights to cache.
- Parameters:
- Return type:
- Returns:
Loaded model instance
Example
>>> # From local path >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer") >>> >>> # From HuggingFace Hub >>> model = ContinuousTokenizer.from_pretrained("username/my-tokenizer") >>> >>> # Override config >>> model = ContinuousTokenizer.from_pretrained( ... "./my-tokenizer", ... dropout=0.1 # Override saved dropout value ... )
- classmethod from_pretrained_encoder_decoder(pretrained_path, strict=False, verbose=True, **kwargs)[source]ο
Create model and load encoder/decoder weights from pretrained checkpoint.
Factory method that creates a new model instance and loads encoder/decoder weights in one step. Useful for initializing new architectures from pretrained backbones.
- Parameters:
- Return type:
- Returns:
New model instance with loaded encoder/decoder weights
Example
>>> # Create FSQ model initialized from VAE weights >>> model = DiscreteTokenizer.from_pretrained_encoder_decoder( ... 'weights/maisi_converted.pt', ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... z_channels=256, ... channels=64, ... channels_mult=(1, 2, 4), ... )
- get_buffer(target)[source]ο
Return the buffer given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the buffer to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The buffer referenced by
target- Return type:
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not a buffer
- get_extra_state()[source]ο
Return any extra state to include in the moduleβs state_dict.
Implement this and a corresponding
set_extra_state()for your module if you need to store extra state. This function is called when building the moduleβs state_dict().Note that extra state should be picklable to ensure working serialization of the state_dict. We only provide backwards compatibility guarantees for serializing Tensors; other objects may break backwards compatibility if their serialized pickled form changes.
- Returns:
Any extra state to store in the moduleβs state_dict
- Return type:
- get_parameter(target)[source]ο
Return the parameter given by
targetif it exists, otherwise throw an error.See the docstring for
get_submodulefor a more detailed explanation of this methodβs functionality as well as how to correctly specifytarget.- Parameters:
target (
str) β The fully-qualified string name of the Parameter to look for. (Seeget_submodulefor how to specify a fully-qualified string.)- Returns:
The Parameter referenced by
target- Return type:
torch.nn.Parameter
- Raises:
AttributeError β If the target string references an invalid path or resolves to something that is not an
nn.Parameter
- get_submodule(target)[source]ο
Return the submodule given by
targetif it exists, otherwise throw an error.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(16, 33, kernel_size=(3, 3), stride=(2, 2)) ) (linear): Linear(in_features=100, out_features=200, bias=True) ) )(The diagram shows an
nn.ModuleA.Awhich has a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To check whether or not we have the
linearsubmodule, we would callget_submodule("net_b.linear"). To check whether we have theconvsubmodule, we would callget_submodule("net_b.net_c.conv").The runtime of
get_submoduleis bounded by the degree of module nesting intarget. A query againstnamed_modulesachieves the same result, but it is O(N) in the number of transitive modules. So, for a simple check to see if some submodule exists,get_submoduleshould always be used.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)- Returns:
The submodule referenced by
target- Return type:
- Raises:
AttributeError β If at any point along the path resulting from the target string the (sub)path resolves to a non-existent attribute name or an object that is not an instance of
nn.Module.
- half()[source]ο
Casts all floating point parameters and buffers to
halfdatatype.Note
This method modifies the module in-place.
- Returns:
self
- Return type:
Module
- inference_mode()[source]ο
Context manager for optimized inference.
Configures the model for maximum inference performance: - Sets model to eval mode - Disables gradient computation - Uses torch.inference_mode for additional optimizations
The model is restored to its previous state upon exit.
- Return type:
Example
>>> with model.inference_mode(): ... latents = model.tokenize(volume) ... recon = model.detokenize(latents)
- Yields:
self β The model instance for method chaining
- ipu(device=None)[source]ο
Move all model parameters and buffers to the IPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on IPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- load_encoder_decoder_weights(pretrained_path, strict=False, verbose=True)[source]ο
Load encoder/decoder weights from a pretrained checkpoint.
This method enables transfer learning by loading encoder and decoder weights from any compatible tokenizer (VAE, VQ-VAE, FSQ, etc.) while leaving quantizer-specific layers randomly initialized.
Use Casesο
Initialize discrete tokenizer from VAE: Load MAISI VAE encoder/decoder, then train only the quantizer layers (VQ, FSQ, etc.)
Fine-tune on new domain: Start from pretrained weights, fine-tune all
Encoder-only transfer: Use pretrained encoder for downstream tasks
How It Worksο
The method identifies encoder/decoder weights by key prefixes and loads only those that match. Quantizer-specific layers (quant_conv, post_quant_conv, quantizer) may or may not be loaded depending on architecture compatibility.
Weight Matching Strategy: - encoder.* keys: Always attempted - decoder.* keys: Always attempted - quant_conv.* keys: Loaded if shapes match - post_quant_conv.* keys: Loaded if shapes match - quantizer.* keys: Skipped (architecture-specific)
- type pretrained_path:
- param pretrained_path:
Path to pretrained weights file (.pt, .bin) or directory containing βpytorch_model.binβ
- type strict:
bool, default:False- param strict:
If True, raise error on shape mismatches. If False (default), skip mismatched weights and log warnings.
- type verbose:
bool, default:True- param verbose:
If True, print summary of loaded/skipped weights
- rtype:
- returns:
Tuple of (loaded_keys, skipped_keys) for inspection
Example
>>> # Initialize FSQ tokenizer from MAISI VAE weights >>> model = DiscreteTokenizer( ... dim=3, ... quantizer='FSQ', ... levels=[8, 5, 5, 5], ... # ... other args matching MAISI architecture ... ) >>> loaded, skipped = model.load_encoder_decoder_weights( ... 'weights/maisi_converted.pt', ... verbose=True ... ) >>> print(f"Loaded {len(loaded)} keys, skipped {len(skipped)}") >>> >>> # Now train with frozen encoder if desired >>> for param in model.encoder.parameters(): ... param.requires_grad = False
Note
For best results, ensure the pretrained model has the same: - channels, channels_mult, num_res_blocks - spatial_compression
The following can differ: - z_channels, latent_channels, embedding_dim - Quantizer type and configuration
- load_state_dict(state_dict, strict=True, assign=False)[source]ο
Copy parameters and buffers from
state_dictinto this module and its descendants.If
strictisTrue, then the keys ofstate_dictmust exactly match the keys returned by this moduleβsstate_dict()function.Warning
If
assignisTruethe optimizer must be created after the call toload_state_dictunlessget_swap_module_params_on_conversion()isTrue.- Parameters:
state_dict (dict) β a dict containing parameters and persistent buffers.
strict (bool, optional) β whether to strictly enforce that the keys in
state_dictmatch the keys returned by this moduleβsstate_dict()function. Default:Trueassign (bool, optional) β When set to
False, the properties of the tensors in the current module are preserved whereas setting it toTruepreserves properties of the Tensors in the state dict. The only exception is therequires_gradfield ofParameterfor which the value from the module is preserved. Default:False
- Returns:
missing_keysis a list of str containing any keys that are expectedby this module but missing from the provided
state_dict.
unexpected_keysis a list of str containing the keys that are notexpected by this module but present in the provided
state_dict.
- Return type:
NamedTuplewithmissing_keysandunexpected_keysfields
Note
If a parameter or buffer is registered as
Noneand its corresponding key exists instate_dict,load_state_dict()will raise aRuntimeError.
- modules(remove_duplicate=True)[source]ο
Return an iterator over all modules in the network.
- Parameters:
remove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not.- Yields:
Module β a module in the network
- Return type:
Note
Duplicate modules are returned only once by default. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): ... print(idx, '->', m) 0 -> Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) ) 1 -> Linear(in_features=2, out_features=2, bias=True)
- mtia(device=None)[source]ο
Move all model parameters and buffers to the MTIA.
This also makes associated parameters and buffers different objects. So it should be called before constructing the optimizer if the module will live on MTIA while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- named_buffers(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
- Parameters:
prefix (str) β prefix to prepend to all buffer names.
recurse (bool, optional) β if True, then yields buffers of this module and all submodules. Otherwise, yields only buffers that are direct members of this module. Defaults to True.
remove_duplicate (bool, optional) β whether to remove the duplicated buffers in the result. Defaults to True.
- Yields:
(str, torch.Tensor) β Tuple containing the name and buffer
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, buf in self.named_buffers(): >>> if name in ['running_var']: >>> print(buf.size())
- named_children()[source]ο
Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
- Yields:
(str, Module) β Tuple containing a name and child module
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module)
- named_modules(memo=None, prefix='', remove_duplicate=True)[source]ο
Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
- Parameters:
memo (
set[Module] |None, default:None) β a memo to store the set of modules already added to the resultprefix (
str, default:'') β a prefix that will be added to the name of the moduleremove_duplicate (
bool, default:True) β whether to remove the duplicated module instances in the result or not
- Yields:
(str, Module) β Tuple of name and module
Note
Duplicate modules are returned only once. In the following example,
lwill be returned only once.Example:
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): ... print(idx, '->', m) 0 -> ('', Sequential( (0): Linear(in_features=2, out_features=2, bias=True) (1): Linear(in_features=2, out_features=2, bias=True) )) 1 -> ('0', Linear(in_features=2, out_features=2, bias=True))
- named_parameters(prefix='', recurse=True, remove_duplicate=True)[source]ο
Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
- Parameters:
prefix (str) β prefix to prepend to all parameter names.
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
remove_duplicate (bool, optional) β whether to remove the duplicated parameters in the result. Defaults to True.
- Yields:
(str, Parameter) β Tuple containing the name and parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size())
- num_parameters()[source]ο
Get total number of learnable parameters.
- Return type:
- Returns:
Sum of numel() for all parameters
- parameters(recurse=True)[source]ο
Return an iterator over module parameters.
This is typically passed to an optimizer.
- Parameters:
recurse (bool) β if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
- Yields:
Parameter β module parameter
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for param in model.parameters(): >>> print(type(param), param.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- push_to_hub(repo_id, save_directory=None, commit_message='Upload model', private=False, **kwargs)[source]ο
Upload model to HuggingFace Hub.
Creates or updates a repository on the Hub with model weights and configuration.
- Parameters:
repo_id (
str) β Repository ID (e.g., βusername/model-nameβ)save_directory (
Optional[str], default:None) β Local directory to save before upload (auto-created if None)commit_message (
str, default:'Upload model') β Commit message for the uploadprivate (
bool, default:False) β Whether to create a private repository**kwargs β Additional arguments for HfApi.upload_folder
- Return type:
Example
>>> model.push_to_hub("username/my-vae-tokenizer") >>> # Creates https://huggingface.co/username/my-vae-tokenizer
- reconstruct(x, roi_size=None, overlap=0.5, sw_batch_size=1)[source]ο
Full encode-decode reconstruction with optional sliding window.
For large 3D volumes that exceed GPU memory, this method implements sliding window inference with Gaussian importance weighting to seamlessly blend overlapping patches.
The Algorithmο
Pad input to multiple of stride + window size
Extract overlapping windows with specified stride
Process windows in batches through tokenize -> detokenize
Weight each windowβs contribution by Gaussian importance
Normalize by accumulated importance and crop to original size
Gaussian Weightingο
Uses a Gaussian importance map (2D or 3D based on input) that gives higher weight to the center of each window, reducing boundary artifacts when blending.
- type x:
- param x:
Input tensor of shape (B, C, H, W, D) for 3D or (B, C, H, W) for 2D
- type roi_size:
- param roi_size:
Size of sliding window. If None, processes entire volume. Can be int (isotropic) or tuple (anisotropic).
- type overlap:
float, default:0.5- param overlap:
Fraction of overlap between windows (0.0 to 0.9). Higher overlap = smoother blending but more compute.
- type sw_batch_size:
int, default:1- param sw_batch_size:
Number of windows to process in parallel per batch. Higher values use more GPU memory but are faster. Default is 1 (sequential processing).
- rtype:
- returns:
Reconstructed tensor with same shape as input
Example
>>> volume = torch.randn(1, 1, 256, 256, 256) # 256Β³ volume >>> # Process in 128Β³ windows with 50% overlap, 4 windows at a time >>> recon = model.reconstruct(volume, roi_size=128, overlap=0.5, sw_batch_size=4)
Note
For volumes that fit in memory, omit roi_size for faster processing
Overlap of 0.5 is a good default; higher values reduce artifacts but increase computation proportionally
sw_batch_size > 1 can significantly speed up inference on GPUs with sufficient memory
- register_backward_hook(hook)[source]ο
Register a backward hook on the module.
This function is deprecated in favor of
register_full_backward_hook()and the behavior of this function will change in future versions.
- register_buffer(name, tensor, persistent=True)[source]ο
Add a buffer to the module.
This is typically used to register a buffer that should not be considered a model parameter. For example, BatchNormβs
running_meanis not a parameter, but is part of the moduleβs state. Buffers, by default, are persistent and will be saved alongside parameters. This behavior can be changed by settingpersistenttoFalse. The only difference between a persistent buffer and a non-persistent buffer is that the latter will not be a part of this moduleβsstate_dict.Buffers can be accessed as attributes using given names.
- Parameters:
name (str) β name of the buffer. The buffer can be accessed from this module using the given name
tensor (Tensor or None) β buffer to be registered. If
None, then operations that run on buffers, such ascuda, are ignored. IfNone, the buffer is not included in the moduleβsstate_dict.persistent (bool) β whether the buffer is part of this moduleβs
state_dict.
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> self.register_buffer('running_mean', torch.zeros(num_features))
- register_forward_hook(hook, *, prepend=False, with_kwargs=False, always_call=False)[source]ο
Register a forward hook on the module.
The hook will be called every time after
forward()has computed an output.If
with_kwargsisFalseor not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the output. It can modify the input inplace but it will not have effect on forward since this is called afterforward()is called. The hook should have the following signature:hook(module, args, output) -> None or modified output
If
with_kwargsisTrue, the forward hook will be passed thekwargsgiven to the forward function and be expected to return the output possibly modified. The hook should have the following signature:hook(module, args, kwargs, output) -> None or modified output
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If
True, the providedhookwill be fired before all existingforwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforwardhooks on thistorch.nn.Module. Note that globalforwardhooks registered withregister_module_forward_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If
True, thehookwill be passed the kwargs given to the forward function. Default:Falsealways_call (bool) β If
Truethehookwill be run regardless of whether an exception is raised while calling the Module. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_forward_pre_hook(hook, *, prepend=False, with_kwargs=False)[source]ο
Register a forward pre-hook on the module.
The hook will be called every time before
forward()is invoked.If
with_kwargsis false or not specified, the input contains only the positional arguments given to the module. Keyword arguments wonβt be passed to the hooks and only to theforward. The hook can modify the input. User can either return a tuple or a single modified value in the hook. We will wrap the value into a tuple if a single value is returned (unless that value is already a tuple). The hook should have the following signature:hook(module, args) -> None or modified input
If
with_kwargsis true, the forward pre-hook will be passed the kwargs given to the forward function. And if the hook modifies the input, both the args and kwargs should be returned. The hook should have the following signature:hook(module, args, kwargs) -> None or a tuple of modified input and kwargs
- Parameters:
hook (Callable) β The user defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingforward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingforward_prehooks on thistorch.nn.Module. Note that globalforward_prehooks registered withregister_module_forward_pre_hook()will fire before all hooks registered by this method. Default:Falsewith_kwargs (bool) β If true, the
hookwill be passed the kwargs given to the forward function. Default:False
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_hook(hook, prepend=False)[source]ο
Register a backward hook on the module.
The hook will be called every time the gradients with respect to a module are computed, and its firing rules are as follows:
Ordinarily, the hook fires when the gradients are computed with respect to the module inputs.
If none of the module inputs require gradients, the hook will fire when the gradients are computed with respect to module outputs.
If none of the module outputs require gradients, then the hooks will not fire.
The hook should have the following signature:
hook(module, grad_input, grad_output) -> tuple(Tensor) or None
The
grad_inputandgrad_outputare tuples that contain the gradients with respect to the inputs and outputs respectively. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the input that will be used in place ofgrad_inputin subsequent computations.grad_inputwill only correspond to the inputs given as positional arguments and all kwarg arguments are ignored. Entries ingrad_inputandgrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs or outputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackwardhooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackwardhooks on thistorch.nn.Module. Note that globalbackwardhooks registered withregister_module_full_backward_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_full_backward_pre_hook(hook, prepend=False)[source]ο
Register a backward pre-hook on the module.
The hook will be called every time the gradients for the module are computed. The hook should have the following signature:
hook(module, grad_output) -> tuple[Tensor, ...], Tensor or None
The
grad_outputis a tuple. The hook should not modify its arguments, but it can optionally return a new gradient with respect to the output that will be used in place ofgrad_outputin subsequent computations. Entries ingrad_outputwill beNonefor all non-Tensor arguments.For technical reasons, when this hook is applied to a Module, its forward function will receive a view of each Tensor passed to the Module. Similarly the caller will receive a view of each Tensor returned by the Moduleβs forward function.
Warning
Modifying inputs inplace is not allowed when using backward hooks and will raise an error.
- Parameters:
hook (Callable) β The user-defined hook to be registered.
prepend (bool) β If true, the provided
hookwill be fired before all existingbackward_prehooks on thistorch.nn.Module. Otherwise, the providedhookwill be fired after all existingbackward_prehooks on thistorch.nn.Module. Note that globalbackward_prehooks registered withregister_module_full_backward_pre_hook()will fire before all hooks registered by this method.
- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_post_hook(hook)[source]ο
Register a post-hook to be run after moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, incompatible_keys) -> None
The
moduleargument is the current module that this hook is registered on, and theincompatible_keysargument is aNamedTupleconsisting of attributesmissing_keysandunexpected_keys.missing_keysis alistofstrcontaining the missing keys andunexpected_keysis alistofstrcontaining the unexpected keys.The given incompatible_keys can be modified inplace if needed.
Note that the checks performed when calling
load_state_dict()withstrict=Trueare affected by modifications the hook makes tomissing_keysorunexpected_keys, as expected. Additions to either set of keys will result in an error being thrown whenstrict=True, and clearing out both missing and unexpected keys will avoid an error.- Returns:
a handle that can be used to remove the added hook by calling
handle.remove()- Return type:
torch.utils.hooks.RemovableHandle
- register_load_state_dict_pre_hook(hook)[source]ο
Register a pre-hook to be run before moduleβs
load_state_dict()is called.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) -> None # noqa: B950
- Parameters:
hook (Callable) β Callable hook that will be invoked before loading the state dict.
- register_module(name, module)[source]ο
Alias for
add_module().
- register_parameter(name, param)[source]ο
Add a parameter to the module.
The parameter can be accessed as an attribute using given name.
- Parameters:
name (str) β name of the parameter. The parameter can be accessed from this module using the given name
param (Parameter or None) β parameter to be added to the module. If
None, then operations that run on parameters, such ascuda, are ignored. IfNone, the parameter is not included in the moduleβsstate_dict.
- Return type:
- register_state_dict_post_hook(hook)[source]ο
Register a post-hook for the
state_dict()method.- It should have the following signature::
hook(module, state_dict, prefix, local_metadata) -> None
The registered hooks can modify the
state_dictinplace.
- register_state_dict_pre_hook(hook)[source]ο
Register a pre-hook for the
state_dict()method.- It should have the following signature::
hook(module, prefix, keep_vars) -> None
The registered hooks can be used to perform pre-processing before the
state_dictcall is made.
- requires_grad_(requires_grad=True)[source]ο
Change if autograd should record operations on parameters in this module.
This method sets the parametersβ
requires_gradattributes in-place.This method is helpful for freezing part of the module for finetuning or training parts of a model individually (e.g., GAN training).
See Locally disabling gradient computation for a comparison between .requires_grad_() and several similar mechanisms that may be confused with it.
- Parameters:
requires_grad (bool) β whether autograd should record operations on parameters in this module. Default:
True.- Returns:
self
- Return type:
Module
- save_pretrained(save_directory, push_to_hub=False, **kwargs)[source]ο
Save model weights and configuration to directory.
Creates a directory structure compatible with from_pretrained():
` save_directory/ βββ config.json # Model configuration βββ pytorch_model.bin # Model weights `- Parameters:
- Return type:
Example
>>> model.save_pretrained("./my-tokenizer") >>> # Later... >>> model = ContinuousTokenizer.from_pretrained("./my-tokenizer")
- set_extra_state(state)[source]ο
Set extra state contained in the loaded state_dict.
This function is called from
load_state_dict()to handle any extra state found within the state_dict. Implement this function and a correspondingget_extra_state()for your module if you need to store extra state within its state_dict.
- set_submodule(target, module, strict=False)[source]ο
Set the submodule given by
targetif it exists, otherwise throw an error.Note
If
strictis set toFalse(default), the method will replace an existing submodule or create a new submodule if the parent module exists. Ifstrictis set toTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule does not exist.For example, letβs say you have an
nn.ModuleAthat looks like this:A( (net_b): Module( (net_c): Module( (conv): Conv2d(3, 3, 3) ) (linear): Linear(3, 3) ) )(The diagram shows an
nn.ModuleA.Ahas a nested submodulenet_b, which itself has two submodulesnet_candlinear.net_cthen has a submoduleconv.)To override the
Conv2dwith a new submoduleLinear, you could callset_submodule("net_b.net_c.conv", nn.Linear(1, 1))wherestrictcould beTrueorFalseTo add a new submodule
Conv2dto the existingnet_bmodule, you would callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1)).In the above if you set
strict=Trueand callset_submodule("net_b.conv", nn.Conv2d(1, 1, 1), strict=True), an AttributeError will be raised becausenet_bdoes not have a submodule namedconv.- Parameters:
target (
str) β The fully-qualified string name of the submodule to look for. (See above example for how to specify a fully-qualified string.)module (
Module) β The module to set the submodule to.strict (
bool, default:False) β IfFalse, the method will replace an existing submodule or create a new submodule if the parent module exists. IfTrue, the method will only attempt to replace an existing submodule and throw an error if the submodule doesnβt already exist.
- Raises:
ValueError β If the
targetstring is empty or ifmoduleis not an instance ofnn.Module.AttributeError β If at any point along the path resulting from the
targetstring the (sub)path resolves to a non-existent attribute name or an object that is not an instance ofnn.Module.
- Return type:
See
torch.Tensor.share_memory_().- Return type:
Self
- state_dict(*args, destination=None, prefix='', keep_vars=False)[source]ο
Return a dictionary containing references to the whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. Parameters and buffers set to
Noneare not included.Note
The returned object is a shallow copy. It contains references to the moduleβs parameters and buffers.
Warning
Currently
state_dict()also accepts positional arguments fordestination,prefixandkeep_varsin order. However, this is being deprecated and keyword arguments will be enforced in future releases.Warning
Please avoid the use of argument
destinationas it is not designed for end-users.- Parameters:
destination (dict, optional) β If provided, the state of module will be updated into the dict and the same object is returned. Otherwise, an
OrderedDictwill be created and returned. Default:None.prefix (str, optional) β a prefix added to parameter and buffer names to compose the keys in state_dict. Default:
''.keep_vars (bool, optional) β by default the
Tensors returned in the state dict are detached from autograd. If itβs set toTrue, detaching will not be performed. Default:False.
- Returns:
a dictionary containing a whole state of the module
- Return type:
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> module.state_dict().keys() ['bias', 'weight']
- to(*args, **kwargs)[source]ο
Move and/or cast the parameters and buffers.
This can be called as
- to(device=None, dtype=None, non_blocking=False)[source]
- to(dtype, non_blocking=False)[source]
- to(tensor, non_blocking=False)[source]
- to(memory_format=torch.channels_last)[source]
Its signature is similar to
torch.Tensor.to(), but only accepts floating point or complexdtypes. In addition, this method will only cast the floating point or complex parameters and buffers todtype(if given). The integral parameters and buffers will be moveddevice, if that is given, but with dtypes unchanged. Whennon_blockingis set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.See below for examples.
Note
This method modifies the module in-place.
- Parameters:
device (
torch.device) β the desired device of the parameters and buffers in this moduledtype (
torch.dtype) β the desired floating point or complex dtype of the parameters and buffers in this moduletensor (torch.Tensor) β Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module
memory_format (
torch.memory_format) β the desired memory format for 4D parameters and buffers in this module (keyword only argument)
- Returns:
self
- Return type:
Module
Examples:
>>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16) >>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble) >>> linear.weight Parameter containing: tensor([[ 0.3741+0.j, 0.2382+0.j], [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128) >>> linear(torch.ones(3, 2, dtype=torch.cdouble)) tensor([[0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
- to_empty(*, device, recurse=True)[source]ο
Move the parameters and buffers to the specified device without copying storage.
- Parameters:
device (
torch.device) β The desired device of the parameters and buffers in this module.recurse (bool) β Whether parameters and buffers of submodules should be recursively moved to the specified device.
- Returns:
self
- Return type:
Module
- train(mode=True)[source]ο
Set the module in training mode.
This has an effect only on certain modules. See the documentation of particular modules for details of their behaviors in training/evaluation mode, i.e., whether they are affected, e.g.
Dropout,BatchNorm, etc.- Parameters:
mode (bool) β whether to set training mode (
True) or evaluation mode (False). Default:True.- Returns:
self
- Return type:
Module
- type(dst_type)[source]ο
Casts all parameters and buffers to
dst_type.Note
This method modifies the module in-place.
- Parameters:
dst_type (type or string) β the desired type
- Returns:
self
- Return type:
Module
- xpu(device=None)[source]ο
Move all model parameters and buffers to the XPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on XPU while being optimized.
Note
This method modifies the module in-place.
- Parameters:
device (int, optional) β if specified, all parameters will be copied to that device
- Returns:
self
- Return type:
Module
- zero_grad(set_to_none=True)[source]ο
Reset gradients of all model parameters.
See similar function under
torch.optim.Optimizerfor more context.- Parameters:
set_to_none (bool) β instead of setting to zero, set the grads to None. See
torch.optim.Optimizer.zero_grad()for details.- Return type:
- forward(input)[source][source]ο
Full forward pass: encode -> decode.
Follows the same return contract as the sibling tokenizers (
ContinuousTokenizer/DiscreteTokenizer): a dict during training (for loss computation) and aNetworkEvalnamedtuple during evaluation.- Parameters:
input (
Tensor|dict[str,Tensor]) β Input tensor (or encoder batch dict for NeuroVFM).- Returns:
reconstructions: Decoded output.latent/latents: Latent grid tensor.
- Eval mode (NetworkEval):
reconstructions: Decoded output.posteriors: AlwaysNone(RAE has no posterior).latent: Latent grid tensor.
- Return type:
Training mode (dict)