Skip to main content

PyTorch extension for handling deeply nested sequences of variable length

Project description



Tests PyPI Codecov License

FoldedTensor: PyTorch extension for handling deeply nested sequences of variable length

foldedtensor is a PyTorch extension that provides efficient handling of tensors containing deeply nested sequences variable sizes. It enables the flattening/unflattening (or unfolding/folding) of data dimensions based on a inner structure of sequence lengths. This library is particularly useful when working with data that can be split in different ways and enables you to avoid choosing a fixed representation.

Installation

The library can be installed with pip:

pip install foldedtensor

Features

  • Support for arbitrary numbers of nested dimensions
  • No computational overhead when dealing with already padded tensors
  • Dynamic re-padding (or refolding) of data based on stored inner lengths
  • Automatic mask generation and updating whenever the tensor is refolded
  • C++ optimized code for fast data loading from Python lists and refolding
  • Flexibility in data representation, making it easy to switch between different layouts when needed

Example

import torch
from foldedtensor import as_folded_tensor

# Creating a folded tensor from a nested list
# There are 2 samples, the first with 5 lines, the second with 1 line.
# Each line contain between 1 and 2 words.
ft = as_folded_tensor(
    [
        [[1], [], [], [], [2, 3]],
        [[4, 3]],
    ],
    data_dims=("samples", "words"),
    full_names=("samples", "lines", "words"),
    dtype=torch.long,
)
print(ft)
# FoldedTensor([[1, 2, 3],
#               [4, 3, 0]])

# Refold on the lines and words dims (flatten the samples dim)
print(ft.refold(("lines", "words")))
# FoldedTensor([[1, 0],
#               [0, 0],
#               [0, 0],
#               [0, 0],
#               [2, 3],
#               [4, 3]])

# Refold on the words dim only: flatten everything
print(ft.refold(("words",)))
# FoldedTensor([1, 2, 3, 4, 3])

# Working with PyTorch operations
embedder = torch.nn.Embedding(10, 16)
embedding = embedder(ft.refold(("words",)))
print(embedding.shape)
# torch.Size([5, 16]) # 5 words total, 16 dims

refolded_embedding = embedding.refold(("samples", "words"))
print(refolded_embedding.shape)
# torch.Size([2, 5, 16]) # 2 samples, 5 words max, 16 dims

Comparison with alternatives

Unlike other ragged or nested tensor implementations, a FoldedTensor does not enforce a specific structure on the nested data, and does not require padding all dimensions. This provides the user with greater flexibility when working with data that can be arranged in multiple ways depending on the data transformation. Moreover, the C++ optimization ensures high performance, making it ideal for handling deeply nested tensors efficiently.

Here is a comparison with other common implementations for handling nested sequences of variable length:

Feature NestedTensor MaskedTensor FoldedTensor
Inner data structure Flat Padded Arbitrary
Max nesting level 1 1
From nested python lists No No Yes
Layout conversion To padded No Any
Reduction ops w/o padding Yes No No

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

foldedtensor-0.3.2.tar.gz (14.8 kB view details)

Uploaded Source

Built Distributions

foldedtensor-0.3.2-cp311-cp311-win_amd64.whl (76.9 kB view details)

Uploaded CPython 3.11 Windows x86-64

foldedtensor-0.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (113.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.2-cp311-cp311-macosx_11_0_arm64.whl (77.8 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

foldedtensor-0.3.2-cp311-cp311-macosx_10_9_x86_64.whl (81.3 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

foldedtensor-0.3.2-cp310-cp310-win_amd64.whl (75.4 kB view details)

Uploaded CPython 3.10 Windows x86-64

foldedtensor-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (112.4 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.2-cp310-cp310-macosx_11_0_arm64.whl (76.5 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

foldedtensor-0.3.2-cp310-cp310-macosx_10_9_x86_64.whl (79.9 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

foldedtensor-0.3.2-cp39-cp39-win_amd64.whl (75.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

foldedtensor-0.3.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (112.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.2-cp39-cp39-macosx_11_0_arm64.whl (76.6 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

foldedtensor-0.3.2-cp39-cp39-macosx_10_9_x86_64.whl (80.0 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

foldedtensor-0.3.2-cp38-cp38-win_amd64.whl (75.4 kB view details)

Uploaded CPython 3.8 Windows x86-64

foldedtensor-0.3.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (112.3 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.2-cp38-cp38-macosx_11_0_arm64.whl (76.5 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

foldedtensor-0.3.2-cp38-cp38-macosx_10_9_x86_64.whl (79.8 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

foldedtensor-0.3.2-cp37-cp37m-win_amd64.whl (76.4 kB view details)

Uploaded CPython 3.7m Windows x86-64

foldedtensor-0.3.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (113.7 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.2-cp37-cp37m-macosx_10_9_x86_64.whl (79.8 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

Details for the file foldedtensor-0.3.2.tar.gz.

File metadata

  • Download URL: foldedtensor-0.3.2.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for foldedtensor-0.3.2.tar.gz
Algorithm Hash digest
SHA256 484ea243f8c48dc800c13266c107f221b296611ad9cebd3eb4322fba4f4f8bd4
MD5 42c0482df3fa43f9d0844c70481b7275
BLAKE2b-256 132b18bdcb5d25c901fbd1f2619a15406a8cd63837fddecfeb444c29bce79c28

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 cf29dd1b87a1f9a0562ebf66217172f4afd515f5480dca48a3476ed9342687f8
MD5 fdda976c87ba3b4eab29706de3a7ba74
BLAKE2b-256 ad379f390a7a2da5a8940a13dee42db0b9dc9ceb73a1ab0401e8078ce099f070

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7a0773d19456e5363ba0edba72d9a011b45e740a3e45a2ac502c8195a48f9ff1
MD5 172a5154300642e63b6cc4a91a1ce544
BLAKE2b-256 2b1dfdc6e2c21f92c0cbdf40e90448cf8ff7844e9e82e46aa0d9d341029ce212

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9f6f853b27bcccc0211cd2fc447af43b8b6d5a0583c31a708ce214f86507cddf
MD5 6ffcddbbc787ec3d6ae5f4e4c86bad09
BLAKE2b-256 3a978a00135f51573d69e112a72ff2aec47be78fbcaf19b19284532e5a12d86d

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f3a82e3cd9926cb7543a18b0fede8a305cf4b7cb3604ad7811cc610f679f0893
MD5 11ab4774ad095d452aaf13a1786617ee
BLAKE2b-256 97367af1d56267b91784284794c33f9ac4c1a3619dbcc27f3db69d3e57d76108

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 840dd5530e8b6e110712d3cbf2fca7b795541d14a398cfcc075d58bdf69d9580
MD5 8d08f4df61e2c2321ab18dd953cb15f0
BLAKE2b-256 c629499803ede9d87d82ae0c809b11518cfef3ad804e737a5f05cec1f06f1cab

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fd7537674116c943958d41dfcf727b0617ba671a9ebb50232baef43bdc8d1b15
MD5 e1714a77cd0dcb6a8addfec34a908be2
BLAKE2b-256 58df7db78a9ff830df849706995978a4f57940345d1f3b83964c11a201e917d4

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 39d943eaf2dceba2e65846e2b078d977f6f099ff8d89ec8955a28d0630069055
MD5 993413a3c543440e7239bd289a0a077a
BLAKE2b-256 45a8b4a49a9a9856ec1e10a77f22d99bddb264e7bd9969297ae9293ac85b7b69

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e09e7d34c95eb020bbe8a4bd9032355e4db291ae7d1d2d6c25307edf6940b620
MD5 b9378c89331f18a7134f0b1c86408cdb
BLAKE2b-256 6333abdd49c013ae9758fdf14e7c5458971f1131f5edd1ab9f8cd97ee66982a0

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 d0eef47addea65327f2a02ef6379cefcaae8be59b1760289ad57799cd63719f9
MD5 b1dd1b8c8a6be4501b1c2911109d6cd6
BLAKE2b-256 a744acb6d1b10c0b96e7b2f9543dd5f244e4c593371e0f66e7a4781d026fda70

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1b79b96ee74b9f65a1ccab2e9978f5bd53f6ffbcdc8d5ef5353447afdc7b7782
MD5 d4034078c6b344ff00e1ff7a357d4e39
BLAKE2b-256 1627aafac66d3169b71dbe528a58c654a7e06051b7214a04a2721e2fee868943

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6dd5f144cc9674facf35eda0d7a0affbd6214b085dfe2fb27ebc2af6628ed1d9
MD5 cb0209e035f7c52d3858244183fb3c53
BLAKE2b-256 9639b2846765619902680bc8146d5687bc303cf02840d3b72ab7e952dc2f33d4

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 143cad1d4db88617c08aaabfd86eda3d65614f20db6cbbe4961de3ac18533add
MD5 3c1e08361ae2c6231c0e85b6545c1793
BLAKE2b-256 4dec91689c283e452ed794afaf729ea81802f43cf0d20b7a724150a261cb561c

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 707904bd2a129a8980a2ad511dc3537c6210aff5d1d1aed81f987abd9dcd8fc1
MD5 90a2194333c8f8c44016507ef76ca5ca
BLAKE2b-256 d172b2cf34c05349e8e2423fe84b19c83a3d3960fc57d47a1375c6a1d0d1143c

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 088047f3c1788ee0f11b09d8d65f5be93301cbfd1586b203253bb59bb303d251
MD5 718b3e45f00fea79ebecc9e08ecb85c9
BLAKE2b-256 def481b8106d58578de016c4f28c7baca64d59cbf0325c5b7ecc624f42372556

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 feca605e871b7ac7ffb4cd86617ce051405d3643fbabb412f9357e7e69e033f6
MD5 91fde04ca0a974c569253b16e98f63fa
BLAKE2b-256 62b98712fcf872efb951e532b7c3693e48de4e4391bd66ffae23dc7004365c9b

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 afef9c9c14356dd84716034f1686ad9bc41535cd8ff76de64fc5bf0dcdb6ea2f
MD5 c977d68907e1fc5db2a0b851f82c3a17
BLAKE2b-256 d0c152812d70b056ba8598fa13b165077a1b50f18da7aac35f00bd1b0768187f

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 6b3024d4c4ca65e4674f049ef0ddfd6a68e03d02f57e3c3317b5532442a6b037
MD5 cd5f2dc3ca09b97e1fd0fd4d79dcae9f
BLAKE2b-256 094ce4206f6bfb7fe1f36e8a916679f74dd7f744a9920a4e12d6bc8256b9c1f4

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 986f6a66289e5d16de4b2b6930c4aa20b5667516f1a31c1ec08107488c85021d
MD5 58e8955b9f33be0bc2636abd0beb0613
BLAKE2b-256 687436c719d5907f0dca835e4fb8759b60655bb9bde53ef1d5a27b28ad4b6c3a

See more details on using hashes here.

Provenance

File details

Details for the file foldedtensor-0.3.2-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for foldedtensor-0.3.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e127e75ed6bc9bf59c22d0b40e0cb2cdeb7727f24e0c2fd74ddd502410562296
MD5 b693208bb3ae431872b7bcac1d0dae97
BLAKE2b-256 baf6ecd7e93b08b4d75ddb3712719053142ee04a4436054b3784cf34ae70646d

See more details on using hashes here.

Provenance

Supported by

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