組み込みコーデック

コーデックは、テンソルのチャンクを一つずつバイト列に変換し、元に戻す。コーデックの ID は <name>/<version> の形で、アーカイブには、テンソルごとにコーデックの ID と全パラメータが記録される。下の表と各節は、コーデックのレジストリから生成している。dtype の列は、そのコーデックが符号化できる dtype を示し、それ以外の dtype では UnsupportedDtypeError が発生する。

  • Assigner を指定しないと、Jabara は DefaultAssigner を使う。float16、bfloat16、float32、float64 には bytegroup-zstd/1 を、それ以外のすべての dtype には zstd/1 を選ぶ。

  • 可逆なコーデックは、すべてのチャンクを、符号化したバイト列と同一のバイト列に復号する。

  • アーカイブで非可逆なコーデックを使えるのは、呼び出し側が allow_lossy=True を渡したときだけである。渡さなければ、write_archive で LossyNotAllowedError が発生する。アーカイブには、非可逆なテンソルごとの絶対誤差の最大値が max_abs_err として記録される。

  • ベースを必要とするコーデックは、テンソルを、以前のアーカイブ(ベース)にある、名前、dtype、形状が同じテンソルとの差分として符号化する。ベースにそのようなテンソルがなければ、書き出し側は CodecChoice の fallback を使い、fallback がなければ MissingBaseError を発生させる。ベースなしで差分のテンソルを読んでも、MissingBaseError が発生する。

  • get_codec(codec_id, params) は、params をコーデックの Params モデルに照らして検査し、未知または不正なパラメータには pydantic.ValidationError を発生させる。差分コーデックは内側のコーデックも構築するので、未知の inner には UnknownCodecError が、非可逆である、またはベースを必要とする内側のコーデックには ValueError が発生する。

各コーデックの節には、そのコーデックが基にした文献やライブラリと、それとの関係を示す。"wraps" は、コーデックがそのライブラリを呼び出すことを表す。"describes" は、コーデックが呼び出すライブラリの手法や形式を、その文献が記述していることを表す。"related" は、文献が同じ考え方をとっていることを表す。この場合、コーデックはこのプロジェクト独自のもので、文献の手法を再現したものではない。

jabara codecs は、コーデックの ID を出力する。jabara codecs --long は、このページがコーデックごとに示す内容と、必要な extra がインストール済みかどうかを加えて出力する。jabara codecs ID は一つのコーデックを示し、--json は同じ内容を JSON で出力する。

$ jabara codecs raw/1
raw/1: Stores the bytes unchanged.

type: lossless; needs no base; dtypes: every dtype; parameters: none

Codec

Type

Needs a base

Dtypes

Parameters and defaults

bytegroup-zstd/1

lossless

no

all

level = 3

quant8-linear/1

lossy

no

float16, bfloat16, float32, float64

level = 3

quant8-log/1

lossy

no

float16, bfloat16, float32, float64

level = 3

raw/1

lossless

no

all

none

sub-delta/1

lossless

yes

all except complex128

inner = "bytegroup-zstd/1", inner_params = {}

xor-delta/1

lossless

yes

all

inner = "bytegroup-zstd/1", inner_params = {}

zipnn/1

lossless

no

float16, bfloat16, float32

none

zstd/1

lossless

no

all

level = 3

Dtype names: bool, uint8, int8, uint16, int16, float16, bfloat16, uint32, int32, float32, uint64, int64, float64, complex64, complex128.

bytegroup-zstd/1

Class: jabara.codecs.builtin.ByteGroupZstdCodec.

Splits the chunk into one byte plane per item byte and compresses each plane with Zstandard. For one-byte items it is the same as zstd/1.

Parameters: level is the Zstandard compression level of every plane.

References:

quant8-linear/1

Class: jabara.codecs.quant.Quant8LinearCodec.

8-bit codes for 255 equal steps from the chunk minimum to its maximum.

Code c stands for lo + c * scale.

Per chunk, scale = (max - min) / 255 (the exact quotient). With tiny the smallest positive subnormal of the dtype, abs(x - x_hat) <= m * scale / 2 + 2**-48 * max(abs(x)) + tiny, where m is 1 for float64 and 2 for float16, bfloat16, and float32. No decoded value leaves [min, max]. A chunk whose items all have the same bits decodes exactly, -0.0 included.

Zeros keep no sign of their own. Entry 0 is lo, the chunk minimum, and no other entry is -0.0. When the minimum is zero, every zero gets code 0 and decodes to -0.0 if the chunk holds a -0.0, else to +0.0, so a chunk that mixes 0.0 and -0.0 decodes all its zeros to -0.0. When the minimum is negative, a zero decodes to +0.0 or to a nonzero value within the bound, so no zero decodes to -0.0.

Payload: the table size in bytes (uint16), the table (entry c is lo + c * scale kept inside [min, max] and rounded to the dtype, for c up to the largest code used), then the Zstandard-compressed codes.

Parameters: level is the Zstandard compression level of the 8-bit codes.

References:

quant8-log/1

Class: jabara.codecs.quant.Quant8LogCodec.

Logarithmic steps on the magnitude; the sign of every item is a separate packed bit plane.

Code 0 is the zero code: the item decodes to a signed zero. Codes 1 to 255 are 255 levels of ln(abs(x)) from the chunk's smallest to its largest nonzero magnitude, with step = (ln(max) - ln(min)) / 254; code c stands for exp(ln_min + (c - 1) * step). With step the exact quotient and m and tiny as for the linear codec, abs(x - x_hat) <= m * abs(x) * (exp(step / 2) - 1) + 2**-40 * abs(x) + tiny. No decoded magnitude leaves [min, max] of the nonzero magnitudes. Exact zeros, including -0.0, and every sign are kept exactly. Payload: the table size in bytes (uint16), the table of magnitudes in the dtype (entry 0 is zero), the packed sign plane (ceil(n / 8) bytes), then the Zstandard-compressed codes.

Parameters: level is the Zstandard compression level of the 8-bit codes.

References:

raw/1

Class: jabara.codecs.builtin.RawCodec.

Stores the bytes unchanged.

sub-delta/1

Class: jabara.codecs.delta.SubDeltaCodec.

Codes the wrap-around difference of the chunk and the base chunk, read as unsigned integers of the item size, with the inner codec. complex128 raises UnsupportedDtypeError.

Parameters: inner is the id of the lossless codec that codes the difference, and inner_params are its parameters. The inner codec must not need a base.

References:

xor-delta/1

Class: jabara.codecs.delta.XorDeltaCodec.

Codes the bitwise XOR of the chunk and the same chunk of the base, with the inner codec. Accepts every dtype.

Parameters: inner is the id of the lossless codec that codes the difference, and inner_params are its parameters. The inner codec must not need a base.

References:

zipnn/1

Class: jabara.codecs.zipnn.ZipNNCodec.

The zipnn package, with Zstandard as its entropy coder.

Lossless. Handles float16, bfloat16, and float32. Any other dtype raises UnsupportedDtypeError.

The third-party zipnn package is imported on first use. It imports torch, so the first call loads torch into the process. This import hides the FutureWarning about torch.jit.script that torch issues.

zipnn holds the GIL for a whole call and runs the call on threads of its own, so the container's thread pool cannot run two chunks at once. with_threads hands it the caller's thread budget instead; the bytes it writes are the same for every thread count. An instance from get_codec uses one thread.

It needs the zipnn extra.

References:

zstd/1

Class: jabara.codecs.builtin.ZstdCodec.

Compresses the whole chunk with Zstandard.

Parameters: level is the Zstandard compression level.

References: