Skip to main content

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), ()

Tokenization

import torch
from torch import nn
from h_net_dynamic_chunking import HNet

dim = 512

net = HNet(
    encoder = nn.Identity(),
    network = HNet(
        encoder = nn.Identity(),
        network = nn.Identity(),
        decoder = nn.Identity(),
        dim = dim
    ),
    decoder = nn.Identity(),
    dim = dim
).eval()

tokens = torch.randn(2, 128, dim)
lens = torch.tensor([128, 96])

with torch.no_grad():
    hierarchy = net.tokenize(tokens, lens = lens)

hierarchy.depth                 # 3 levels, fine -> coarse
hierarchy.finest.embeds         # (2, 128, 512)
hierarchy.coarsest.embeds       # compressed root
hierarchy.coarsest.run_lens     # run lengths back to the level below

hierarchy.repeat_interleave()                  # coarsest -> finest
hierarchy.repeat_interleave(level = 1)         # level 1 -> finest
hierarchy.repeat_interleave_all()              # every level at base resolution
hierarchy.to_rle()                             # coarse -> fine (tokens, run_lens)

With vector quantization

net = HNet(
    encoder = nn.Identity(),
    network = HNet(
        encoder = nn.Identity(),
        network = nn.Identity(),
        decoder = nn.Identity(),
        dim = dim,
        vq = dict(dim = dim, codebook_size = 2048)
    ),
    decoder = nn.Identity(),
    dim = dim,
    vq = dict(dim = dim, codebook_size = 2048)
).eval()

with torch.no_grad():
    hierarchy = net.tokenize(tokens, lens = lens)

hierarchy.indices                               # tuple of (batch, level length), -1 for padding
codes = hierarchy.repeat_interleave(use_indices = True)

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},
}
@misc{kim2026hierarchicallatentactionmodel,
    title   = {Hierarchical Latent Action Model},
    author  = {Hanjung Kim and Lerrel Pinto and Seon Joo Kim},
    year    = {2026},
    eprint  = {2603.05815},
    archivePrefix = {arXiv},
    primaryClass = {cs.RO},
    url     = {https://arxiv.org/abs/2603.05815},
}
@misc{kumbong2026nextconceptmodeling,
    title   = {Next-Concept Modeling: Fast, Parallel Coarse-to-Fine Text Generation},
    author  = {Hermann Kumbong and Ishan S. Khare and Iddah Mlauzi and Andrew Woen and Charles Ding and E. Kelly Buchanan and Simran Arora and Azalia Mirhoseini and Christopher Ré},
    year    = {2026},
    eprint  = {2610.14162},
    archivePrefix = {arXiv},
    primaryClass = {cs.CL},
    url     = {https://arxiv.org/abs/2610.14162},
}

Metadata

Release files for h-net-dynamic-chunking 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for h-net-dynamic-chunking 0.6.1
File Size Uploaded Provenance Actions
h_net_dynamic_chunking-0.6.1.tar.gz 23.3 kB Without provenance Details for h_net_dynamic_chunking-0.6.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for h-net-dynamic-chunking 0.6.1
File Interpreter ABI Platform Actions
h_net_dynamic_chunking-0.6.1-py3-none-any.whl Python 3 none any Details for h_net_dynamic_chunking-0.6.1-py3-none-any.whl

Total release size: 45.9 kB

Release files / h_net_dynamic_chunking-0.6.1.tar.gz

Download URL h_net_dynamic_chunking-0.6.1.tar.gz
Size 23.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8d34a0570b0cf8bba43fc9a2007ebbe9d1a6c645a0041e380088820a38cf9ad7
BLAKE2b-256 checksum
How to use checksums
f4d48ca56082f5c26a1952d6c826a16b691ed30d56790a009f7f84f7605946ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release files / h_net_dynamic_chunking-0.6.1-py3-none-any.whl

Download URL h_net_dynamic_chunking-0.6.1-py3-none-any.whl
Size 22.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc34da86a7a4ed27570a060f163e07e1e4e30dce6a8112c4bfffb54375a6c922
BLAKE2b-256 checksum
How to use checksums
66787e1963f578fb6cdace3735bb2eed4fc53403e2d16aad99312d230772f878
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Security for h-net-dynamic-chunking 0.6.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page