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)
out, aux_loss = net(tokens) # (1, 1024, 512), (1,)
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},
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file h_net_dynamic_chunking-0.3.2.tar.gz.
File metadata
- Download URL: h_net_dynamic_chunking-0.3.2.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28e39fad93d96afd8a26881454c89b74bad3522ae186384acec3eb56a8d7f81b
|
|
| MD5 |
0262b775312bb6089029e004239b06cc
|
|
| BLAKE2b-256 |
316d6879ab46acb1c6055f1a938e6677d32c318073284e7832566d5bd91cb255
|
File details
Details for the file h_net_dynamic_chunking-0.3.2-py3-none-any.whl.
File metadata
- Download URL: h_net_dynamic_chunking-0.3.2-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11b394a8fb81dd2156c7cca66d9c97301600c48d7db7c857912686444be9677c
|
|
| MD5 |
eda98282fc28b60a35c03db09122c531
|
|
| BLAKE2b-256 |
194fbfe23e6ab7bb8f70355ca7ce2e58e60a26967a32ab2f7d9834b0f44ad8da
|