langvae.decoders package

Submodules

langvae.decoders.sentence module

class langvae.decoders.sentence.SentenceDecoder(model_path: str, latent_size: int, max_len: int, conditional: bool = False, device: device = 'cpu', device_map: str = None, args=None)[source]

Bases: BaseDecoder

Decoder class for generating sentences from latent representations.

This decoder uses a pre-trained causal language model to generate text from latent representations. It outputs token probability distribution tensors (B x S x V), where \(B\) is the batch size, \(S\) is the maximum sentence length and \(V\) is the decoder vocabulary size.

model_path

Path/locator to the pre-trained language model.

Type:

str

latent_size

Size of the latent space.

Type:

int

max_len

Maximum length (in tokens) of the generated sentences.

Type:

int

device

Device on which the model and data are allocated (e.g., ‘cpu’, ‘cuda’).

Type:

torch.device

device_map

Device map configuration for model parallelism.

Type:

str

args

Additional configuration arguments.

Type:

ModelConfig, optional

property decoder: Module
forward(z: Tensor, max_len: int = 0, x: Tensor = None) ModelOutput[source]

Processes the input latent tensor through the decoder to generate sentences.

Parameters:
  • z (Tensor) – Input tensor containing latent representations.

  • max_len (int) – Maximum length (tokens) of output sentences.

  • x (Tensor) – Input tensor containing original tokens, for teach-forcing training.

Returns:

The generated sentences as a ModelOutput object: token probability distribution tensors (B x S x V), where \(B\) is the batch size, \(S is the maximum sentence length and :math:`V\) is the decoder vocabulary size.

Return type:

ModelOutput

generate(z: Tensor, max_len: int = 0) ModelOutput[source]
init_pretrained_model()[source]
to(device, include_pretrained: bool = True)[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 complex dtypes. In addition, this method will only cast the floating point or complex parameters and buffers to dtype (if given). The integral parameters and buffers will be moved device, if that is given, but with dtypes unchanged. When non_blocking is 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 module

  • dtype (torch.dtype) – the desired floating point or complex dtype of the parameters and buffers in this module

  • tensor (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)
property tokenizer: PreTrainedTokenizer

Module contents