Skip to main content

H-Net Dynamic Chunking Modules

Project description

H-Net Dynamic Chunking

Implementation of the dynamic chunking mechanism in H-net by Hwang et al. of Carnegie Mellon

Install

$ pip install h-net-dynamic-chunking

Usage

import torch
from h_net_dynamic_chunking import DynamicSequenceChunker

downsampler = DynamicSequenceChunker(512)

tokens = torch.randn(3, 1024, 512).requires_grad_()

downsampled, upsample_fn, *_ = downsampler(tokens)

assert upsample_fn(downsampled).shape == tokens.shape

3 layers hierarchy

import torch
from h_net_dynamic_chunking import DynamicSequenceChunker

downsampler1 = DynamicSequenceChunker(512)
downsampler2 = DynamicSequenceChunker(512)
downsampler3 = DynamicSequenceChunker(512)

tokens = torch.randn(3, 1024, 512).requires_grad_()

downsampled1, upsample_fn1, aux_loss1 = downsampler1(tokens)

# hierarchical network 1 ...

downsampled2, upsample_fn2, aux_loss2 = downsampler2(downsampled1)

# hierarchical network 2 ...

downsampled3, upsample_fn3, aux_loss3 = downsampler3(downsampled2)

# inner most network

# reconstituting

assert upsample_fn1(upsample_fn2(upsample_fn3(downsampled3))).shape == tokens.shape

HNet wrapper

import torch
from torch import nn
from h_net_dynamic_chunking.h_net import HNet

# 3 hierarchies, from 512 -> 1024 -> 2048 inner

net = HNet(
    nn.Identity(),
    HNet(
        nn.Identity(),
        HNet(
            nn.Identity(),
            nn.Identity(),
            nn.Identity(),
            dim = 2048
        ),
        nn.Identity(),
        dim = 1024,
        dim_inner = 2048
    ),
    nn.Identity(),
    dim = 512,
    dim_inner = 1024,
)

tokens = torch.randn(1, 1024, 512)

ret = net(tokens)
out, aux_loss = ret.output, ret.loss # (1, 1024, 512), ()

Example

Enwik8 with 2 hierarchies

$ pip install '.[examples]'

Then

$ python train.py

Citations

@misc{hwang2025dynamicchunkingendtoendhierarchical,
    title   = {Dynamic Chunking for End-to-End Hierarchical Sequence Modeling},
    author  = {Sukjun Hwang and Brandon Wang and Albert Gu},
    year    = {2025},
    eprint  = {2507.07955},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2507.07955},
}
@misc{balestriero2025lejepa,
    title   = {LeJEPA: Provable and Scalable Self-Supervised Learning Without the Heuristics},
    author  = {Randall Balestriero and Yann LeCun},
    year    = {2025},
    eprint  = {2511.08544},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2511.08544},
}

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

h_net_dynamic_chunking-0.5.17.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

h_net_dynamic_chunking-0.5.17-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file h_net_dynamic_chunking-0.5.17.tar.gz.

File metadata

File hashes

Hashes for h_net_dynamic_chunking-0.5.17.tar.gz
Algorithm Hash digest
SHA256 218785150a9c23b2b2b16f27b6b3604c1c309ab6406db812e5d1add59083089a
MD5 eb37baeadc4386d7982576b13bc91abd
BLAKE2b-256 8aff0013e4724c682bfcbef4cde4649160a62403be0eaccef27b142398488ebe

See more details on using hashes here.

File details

Details for the file h_net_dynamic_chunking-0.5.17-py3-none-any.whl.

File metadata

File hashes

Hashes for h_net_dynamic_chunking-0.5.17-py3-none-any.whl
Algorithm Hash digest
SHA256 38e5a04881434437518be4b1b315bcc5ce33a942213304e011dd165fe4f26132
MD5 fbbc1c3c0e5b5fea7816d70950a4b96b
BLAKE2b-256 320e16b6ef7ce250a7c0173e13601604d729a9da36c3401697709f8cbbbbe217

See more details on using hashes here.

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