Glimpses

Documentation for the glimpses kernel library

This is a module for the manipulation of tensors by means of lightweight memory views and minimal padding. It extends the native torch functions in ways that I find useful.

All items within this module are functions. They all accept a tensor and parameters, then do something with it. They also tend to return views to allow efficient memory utilization.

view

class supertransformerlib.Glimpses.view(tensor, input_shape: Union[Tensor, List[int], int], output_shape: Union[Tensor, List[int], int])

This will, when passed an input shape and compatible output shape, assume that said shapes refer to the later dimensions in a tensor, as in broadcasting, and will perform a reshape from input shape to output shape while keeping all other dimensions exactly the same.

—- parameters —

Parameters:
  • tensor – The tensor to be modified.

  • input_shape – The expected input shape. This can be a list/tuple of ints, or an int. It should represent the shape at the end of the input tensor’s .shape which will be matched in the tensor input

  • output_shape – The expected output shape. This can be a list/tuple of ints, or an int. It should represent the final shape one wishes the tensor to take. It also must be the case that the total size of the input and output shape must be the same.

—- Examples —-

For tensors of shape:

a = (5,2), b=(3, 4, 5,2), c=(30, 5,2),

For input_shape = (5,2), output_shape=10, one has

f(a, input_shape, output_shape) = shape(10) f(b, input_shape, output_shape) = shape(3, 4, 10) f(c, input_shape, output_shape) = shape(30, 10)

reshape

class supertransformerlib.Glimpses.reshape(tensor, input_shape: Union[Tensor, List[int], int], output_shape: Union[Tensor, List[int], int])

This will, when passed an input shape and compatible output shape, assume that said shapes refer to the later dimensions in a tensor, as in broadcasting, and will perform a reshape from input shape to output shape while keeping all other dimensions exactly the same.

—- parameters —

Parameters:
  • tensor – The tensor to be modified.

  • input_shape – The expected input shape. This can be a list/tuple of ints, or an int. It should represent the shape at the end of the input tensor’s .shape which will be matched in the tensor input

  • output_shape – The expected output shape. This can be a list/tuple of ints, or an int. It should represent the final shape one wishes the tensor to take. It also must be the case that the total size of the input and output shape must be the same.

—- Examples —-

For tensors of shape:

a = (5,2), b=(3, 4, 5,2), c=(30, 5,2),

For input_shape = (5,2), output_shape=10, one has

f(a, input_shape, output_shape) = shape(10) f(b, input_shape, output_shape) = shape(3, 4, 10) f(c, input_shape, output_shape) = shape(30, 10)

local

supertransformerlib.Glimpses.local()

Description:

This is a function designed to extract a series of kernels generated by standard convolutional keyword conventions which could, by broadcasted application of weights, be used to actually perform a convolution. The name “local” is due to the fact that the kernels generated are inherently a somewhat local phenomenon.

When calling this function, a series of kernels with shape determined by dilation_rate and kernel_width, and with number determined by stride_rate, will be generated along the last dimension of the input tensor. The output will be a tensor with an additional dimension on the end, with width equal to the size of the kernel, and the second-to-last dimension then indices these kernels.

Note that the different between initial and final indexing dimensions is:

compensation = (kernel_width - 1) * dilation_rate

See ‘dilocal’ for a version of this which is fast when dealing with many dilations in parallel, as in banding. Padding by this much is guaranteed to prevent information loss.

Parameters:
  • tensor – The tensor to take a local kernel out of

  • kernel_width – How wide to make the kernel

  • stride_rate – How fast to make the stride rate

  • dilation_rate – How large the dilation rate should be

  • start_offset – How long to wait before sampling from the tensor

  • end_offset – Where to stop sampling from the tensor

dilocal

supertransformerlib.Glimpses.dilocal()

Performs the local operation in parallel with a variety of different dilations. Entries are padded with zero if there would not be a reference in the original tensor, such as edge dilations.

Parameters:
  • tensor – The input to localize

  • kernel_width – The kernel to use

  • stride_rate – The stride rate to use

  • dilations – A list of the dilations. Each one will end up on a head dimension

  • pad_to_input_size – Whether or not to ensure the output is as wide as the input.

  • pad_value – What to pad with.

  • pad_justification – May be “forward”, “center”, or “backward”.

Returns:

tensor. Has shape (dilations, items, kernel_width).