Skip to main content

Efficient implementation of stacked linear modules

Project description

Parallel Stacked Linear Modules for PyTorch

Tests Documentation Codecov

Efficient implementation of stacked linear modules in PyTorch, with support for output and stack subsetting.

Features

  • LinearLayer: A linear layer with support for efficient output subsetting.
  • StackedLinearLayer: A parallelized linear layer that applies multiple independent transformations across different input stacks simultaneously. This is significantly more efficient than for loop over multiple nn.Linear layers. This is useful for specialized neural architectures like Additive Decoders.
  • Subsetting Support: Both layers allow for subsetting output features during the forward pass, and StackedLinearLayer additionally supports subsetting stacks.

Installation

pip install stacked-linear

Or install from source:

pip install git+https://github.com/moinfar/stacked-linear.git

Quick Start

Linear Layer with Output Subsetting

import torch
from stacked_linear import LinearLayer

# Initialize a layer (10 inputs, 5 outputs)
layer = LinearLayer(10, 5)
x = torch.randn(2, 10)

# Forward pass on a subset of output features (indices 0, 2, and 4)
subset = torch.tensor([0, 2, 4])
output = layer(x, output_subset=subset)  # Shape: (2, 3)

Stacked Linear Layer

import torch
from stacked_linear import StackedLinearLayer

# 3 parallel stacks, each mapping 10 inputs to 5 outputs
layer = StackedLinearLayer(n_stacks=3, in_features=10, out_features=5)
x = torch.randn(2, 3, 10)  # (batch, stacks, features)

# Efficient parallel forward pass
output = layer(x)  # Shape: (2, 3, 5)

# Forward pass on a subset of output features across all stacks
subset = torch.tensor([1, 3])
output_subset = layer(x, output_subset=subset)  # Shape: (2, 3, 2)

# Forward pass on a subset of stacks
stack_subset = torch.tensor([[0, 2], [1, 2]]) # Indices for each batch item
x_subset = torch.randn(2, 2, 10)
output_stack_subset = layer(x_subset, stack_subset=stack_subset) # Shape: (2, 2, 5)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stacked_linear-0.1.3.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stacked_linear-0.1.3-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file stacked_linear-0.1.3.tar.gz.

File metadata

  • Download URL: stacked_linear-0.1.3.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for stacked_linear-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8090ae23cf90c06c62bbe57ed9151eaeba9d0a46b999ecb05849629c688e0f5a
MD5 97a7de6ed5688a6bcd3362714872fc35
BLAKE2b-256 4535353e4c9ce8e097e5d6aef0a8adf0cd66185c3d0446e944bc29df19df3a36

See more details on using hashes here.

Provenance

The following attestation bundles were made for stacked_linear-0.1.3.tar.gz:

Publisher: release.yaml on moinfar/stacked-linear

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stacked_linear-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: stacked_linear-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for stacked_linear-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 de6f44e8eaa16aa2fe0648ecbdbcc755b140ec6d7f841c7bff6866cd78a519c7
MD5 d8e747b17067339c27d9804e43600b96
BLAKE2b-256 45e997ff062dfe502343bc32be1da48d00d7c8b0f7d8da1a58aea9cf239fe763

See more details on using hashes here.

Provenance

The following attestation bundles were made for stacked_linear-0.1.3-py3-none-any.whl:

Publisher: release.yaml on moinfar/stacked-linear

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page