Skip to main content

Mathematical Morphology-based self-attention module for PyTorch (CUDA) using Flash-style kernel fusion.

Project description

Morphottention

Mathematical Morphology-based self-attention module for PyTorch using Flash-style kernel fusion.

Install

Prebuilt wheels are published for CPython 3.14 on Linux (x86_64, aarch64) and Windows (x86_64). A working CUDA-enabled PyTorch (torch >= 2.12) must already be installed in the environment.

pip install morphottention

Usage

The package exposes an nn.Module (MorphoAttention), a functional entry point (morpho_attention), and the raw autograd bridge (MorphoAttentionFunction). All inputs must be CUDA tensors; the module defaults to float16.

As an nn.Module

import torch
from morphottention import MorphoAttention

attn = MorphoAttention(
    dim=256,       # model dimension D
    num_heads=8,   # number of attention heads H
    cube_m=16,     # hypercube width per head
    scale=1.0,     # softmax temperature
    causal=False,  # casual masking flag
    device="cuda",
)

x = torch.randn(2, 128, 256, dtype=torch.float16, device="cuda")  # (B, N, D)
out = attn(x)          # (B, N, D)
out.sum().backward()

Functional form

from morphottention import morpho_attention

out = morpho_attention(
    x, 
    W_phi, 
    gate_q, 
    gate_k, 
    W_V,
    num_heads=8, cube_m=16, scale=1.0, 
    causal=False,
)

Raw autograd bridge

import torch
from morphottention import MorphoAttentionFunction

B, N, D, H, cube_m = 2, 128, 256, 8, 16

x = torch.randn(B, N, D, dtype=torch.float16, device="cuda", requires_grad=True)
W_phi = torch.randn(D, H * cube_m, dtype=torch.float16, device="cuda", requires_grad=True)
gate_q = torch.ones(H, cube_m, dtype=torch.float16, device="cuda", requires_grad=True)
gate_k = torch.ones(H, cube_m, dtype=torch.float16, device="cuda", requires_grad=True)
W_V = torch.randn(D, D, dtype=torch.float16, device="cuda", requires_grad=True)

out = MorphoAttentionFunction.apply(
    x, W_phi, gate_q, gate_k, W_V,
    H, cube_m, 1.0, False,  # num_heads, cube_m, scale, causal
)  # (B, N, D)
out.sum().backward()

W_phi has shape (D, H * cube_m), W_V has shape (D, D), and gate_q / gate_k each have shape (H, cube_m).

Building from source

Requires the CUDA 13.X toolkit (nvcc) and a matching torch build:

uv sync --package morphottention --no-dev --group build
uv build --package morphottention --wheel --no-build-isolation

License

MIT

Copyright © 2026 Vedran Hrabar.

Download files

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

Source Distribution

morphottention-0.2.1.tar.gz (19.8 kB view details)

Uploaded Source

Built Distributions

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

morphottention-0.2.1-cp314-cp314-win_amd64.whl (193.7 kB view details)

Uploaded CPython 3.14Windows x86-64

morphottention-0.2.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (46.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

morphottention-0.2.1-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (43.9 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

morphottention-0.2.1-cp313-cp313-win_amd64.whl (191.7 kB view details)

Uploaded CPython 3.13Windows x86-64

morphottention-0.2.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (46.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

morphottention-0.2.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (43.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

morphottention-0.2.1-cp312-cp312-win_amd64.whl (191.7 kB view details)

Uploaded CPython 3.12Windows x86-64

morphottention-0.2.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (46.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

morphottention-0.2.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (43.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

File details

Details for the file morphottention-0.2.1.tar.gz.

File metadata

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

File hashes

Hashes for morphottention-0.2.1.tar.gz
Algorithm Hash digest
SHA256 580343ed47707e22224a77599ee1652380afd65d67a367ff4d3467f2d65769f0
MD5 4cb617aca2ee38eafe434353893e2335
BLAKE2b-256 7f9fa6a1bfdb85e8aaae3af0e72e61b9b10644732d7582cddaee6fe44a579fea

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1.tar.gz:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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

File details

Details for the file morphottention-0.2.1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for morphottention-0.2.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 3ba555dbc16bcbb23652f1eccd9159d2adfca8550eb65be5b7a99badd36c637b
MD5 4a5c74c0a9bb034046665334d0ade575
BLAKE2b-256 8c3320454c7eaf6b2a6cb68b769a7da810a6ecb3cb4334e9cccba1dc81c4200d

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1-cp314-cp314-win_amd64.whl:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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

File details

Details for the file morphottention-0.2.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for morphottention-0.2.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cc185a28cfb731a9c17d1ffe7be6b6cfb7dba2bfba6ec9f3c13b7293493494ce
MD5 bb92d95bf3885752ea1b282a1e962152
BLAKE2b-256 344eda168aad6c39041682e3e6c0d5e4b2bb9b025eab17dfece6423762664332

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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

File details

Details for the file morphottention-0.2.1-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for morphottention-0.2.1-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2776eeab791dbb5c31b04d4bc2ea8f8a4d9a7a09cc09f244f5128ec02661a456
MD5 5cee4e150c2d6e8326de76110a4677d7
BLAKE2b-256 584ad0105b19ec2f6b878ce56a1930d3133e912afd424ff58a73ee7a11503d39

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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

File details

Details for the file morphottention-0.2.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for morphottention-0.2.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a3c85c8ee5818767119e324e2bffda19df33810c8f3f754decc16359644f4656
MD5 a2f6d5b0957f5e9ac7e98c0405da859c
BLAKE2b-256 819832f7c0e0a922b211e7f2ee6053fc0e969edf1271fea0f184e5a9b2954f0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1-cp313-cp313-win_amd64.whl:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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

File details

Details for the file morphottention-0.2.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for morphottention-0.2.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 637bbd0beb7e1ddef48945e1b5d4d0b3b964a71d1996c11def2a72a6b79abb7b
MD5 f5bbd1db24ae606f2f260af695cfd803
BLAKE2b-256 4467c956d90471b8c64c952ab1c0a245a522f4542ac3f174dae300671126cfca

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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

File details

Details for the file morphottention-0.2.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for morphottention-0.2.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a30ff72706d8805651c46a06762e531bcb98202062aac98c7548d5ff4d307f96
MD5 9ebd4a316d98a4a883572a1dac478e99
BLAKE2b-256 ba340366c8f75b6e23363be0d2a61ec2f8eeb8093f9550c49614b6af18f95e79

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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

File details

Details for the file morphottention-0.2.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for morphottention-0.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d76be371b5f72ac9866105bcbe282a8477841b4a3da4bd8cda6d3c37f090a53e
MD5 98068e5e78e06e5272b6eb48c337440d
BLAKE2b-256 d0e16c152ee0828975832cda57fb404cc643246de7187d70d34da636cbdcb43b

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1-cp312-cp312-win_amd64.whl:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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

File details

Details for the file morphottention-0.2.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for morphottention-0.2.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1ec02b1f7e7b66e1990b8619ced8ce6da803862473ec8f8a571fa0599a9d7bb8
MD5 c2abc26f094969cb068f1ed9dfcc8016
BLAKE2b-256 ceeacbfe8cd7b42073801e6c66f1e0266e73be88389b45f944063a95fd46e3e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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

File details

Details for the file morphottention-0.2.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for morphottention-0.2.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ec591974a276f7200ad77cd7abd88269460860f66774d31d4d26b8125713d40f
MD5 6da8af6df4832a3c05e1729bfc84ab62
BLAKE2b-256 20813ce1721f2357106a3e550ee8b9165bd93039bc8f24bd2b8008bd09f82481

See more details on using hashes here.

Provenance

The following attestation bundles were made for morphottention-0.2.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish-pypi.yml on vhrabar/Morphottention

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