|
| 1 | +package io.github.dfa1.vortex.encoding; |
| 2 | + |
| 3 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 4 | +import dev.vortex.proto.DTypeProtos; |
| 5 | +import dev.vortex.proto.EncodingProtos; |
| 6 | +import io.github.dfa1.vortex.core.DType; |
| 7 | +import io.github.dfa1.vortex.core.PType; |
| 8 | + |
| 9 | +import java.lang.foreign.MemorySegment; |
| 10 | +import java.lang.foreign.ValueLayout; |
| 11 | +import java.nio.ByteBuffer; |
| 12 | +import java.nio.ByteOrder; |
| 13 | + |
| 14 | +/// Decoder for {@code vortex.alp} — Adaptive Lossless floating-Point compression. |
| 15 | +/// |
| 16 | +/// <p>Metadata: protobuf {@code ALPMetadata} — {@code exp_e u32} (tag 1), |
| 17 | +/// {@code exp_f u32} (tag 2), optional {@code patches PatchesMetadata} (tag 3). |
| 18 | +/// |
| 19 | +/// <p>Child slot 0: encoded integers (I32 for F32, I64 for F64 parent), rowCount = array len. |
| 20 | +/// Child slot 1: patch indices (if patches), rowCount = patches.len. |
| 21 | +/// Child slot 2: patch values (if patches, same dtype as parent), rowCount = patches.len. |
| 22 | +/// Child slot 3: chunk offsets (optional, ignored — patches applied by absolute index). |
| 23 | +/// |
| 24 | +/// <p>Decode: {@code decoded[i] = (float/double) encoded[i] * F10[exp_f] * IF10[exp_e]}, |
| 25 | +/// then overwrite {@code decoded[indices[j] - offset] = values[j]} for each patch. |
| 26 | +public final class AlpCodec implements Decoder { |
| 27 | + |
| 28 | + // Powers of 10 for F64 (index 0..18 used by the encoder). |
| 29 | + private static final double[] F10_F64 = { |
| 30 | + 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, |
| 31 | + 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
| 32 | + 1e20, 1e21, 1e22, 1e23 |
| 33 | + }; |
| 34 | + |
| 35 | + private static final double[] IF10_F64 = { |
| 36 | + 1e-0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9, |
| 37 | + 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15, 1e-16, 1e-17, 1e-18, 1e-19, |
| 38 | + 1e-20, 1e-21, 1e-22, 1e-23 |
| 39 | + }; |
| 40 | + |
| 41 | + // Powers of 10 for F32. |
| 42 | + private static final float[] F10_F32 = { |
| 43 | + 1e0f, 1e1f, 1e2f, 1e3f, 1e4f, 1e5f, 1e6f, 1e7f, 1e8f, 1e9f, 1e10f |
| 44 | + }; |
| 45 | + |
| 46 | + private static final float[] IF10_F32 = { |
| 47 | + 1e-0f, 1e-1f, 1e-2f, 1e-3f, 1e-4f, 1e-5f, 1e-6f, 1e-7f, 1e-8f, 1e-9f, 1e-10f |
| 48 | + }; |
| 49 | + |
| 50 | + @Override |
| 51 | + public String encodingId() { |
| 52 | + return "vortex.alp"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Array decode(DecodeContext ctx) { |
| 57 | + ByteBuffer rawMeta = ctx.metadata(); |
| 58 | + if (rawMeta == null) { |
| 59 | + throw new IllegalStateException("vortex.alp: missing metadata"); |
| 60 | + } |
| 61 | + byte[] metaBytes = new byte[rawMeta.remaining()]; |
| 62 | + rawMeta.duplicate().get(metaBytes); |
| 63 | + |
| 64 | + EncodingProtos.ALPMetadata meta; |
| 65 | + try { |
| 66 | + meta = EncodingProtos.ALPMetadata.parseFrom(metaBytes); |
| 67 | + } catch (InvalidProtocolBufferException e) { |
| 68 | + throw new IllegalStateException("vortex.alp: invalid metadata", e); |
| 69 | + } |
| 70 | + |
| 71 | + if (!(ctx.dtype() instanceof DType.Primitive p)) { |
| 72 | + throw new IllegalStateException("vortex.alp: expected primitive dtype, got " + ctx.dtype()); |
| 73 | + } |
| 74 | + |
| 75 | + int expE = meta.getExpE(); |
| 76 | + int expF = meta.getExpF(); |
| 77 | + PType ptype = p.ptype(); |
| 78 | + long n = ctx.rowCount(); |
| 79 | + |
| 80 | + return switch (ptype) { |
| 81 | + case F64 -> decodeF64(ctx, meta, expE, expF, n); |
| 82 | + case F32 -> decodeF32(ctx, meta, expE, expF, n); |
| 83 | + default -> throw new IllegalStateException("vortex.alp: unsupported dtype " + ptype); |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + // ── F64 ─────────────────────────────────────────────────────────────────── |
| 88 | + |
| 89 | + private Array decodeF64(DecodeContext ctx, EncodingProtos.ALPMetadata meta, int expE, int expF, long n) { |
| 90 | + DType encodedDtype = new DType.Primitive(PType.I64, false); |
| 91 | + Array encoded = decodeChildAs(ctx, 0, encodedDtype, n); |
| 92 | + |
| 93 | + double f10 = F10_F64[expF]; |
| 94 | + double if10 = IF10_F64[expE]; |
| 95 | + |
| 96 | + byte[] outBytes = new byte[(int) (n * 8)]; |
| 97 | + ByteBuffer out = ByteBuffer.wrap(outBytes).order(ByteOrder.LITTLE_ENDIAN); |
| 98 | + var srcLayout = ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 99 | + MemorySegment src = encoded.buffer(0); |
| 100 | + for (long i = 0; i < n; i++) { |
| 101 | + long encVal = src.get(srcLayout, i * 8); |
| 102 | + double dec = (double) encVal * f10 * if10; |
| 103 | + out.putDouble(dec); |
| 104 | + } |
| 105 | + |
| 106 | + if (meta.hasPatches()) { |
| 107 | + applyPatchesF64(ctx, meta.getPatches(), outBytes, n); |
| 108 | + } |
| 109 | + |
| 110 | + return new Array(ctx.dtype(), n, |
| 111 | + new MemorySegment[]{MemorySegment.ofArray(outBytes)}, new Array[0], ArrayStats.empty()); |
| 112 | + } |
| 113 | + |
| 114 | + private void applyPatchesF64(DecodeContext ctx, EncodingProtos.PatchesMetadata pm, |
| 115 | + byte[] outBytes, long n) { |
| 116 | + long numPatches = pm.getLen(); |
| 117 | + long offset = pm.getOffset(); |
| 118 | + PType idxPtype = ptypeFromProto(pm.getIndicesPtype()); |
| 119 | + |
| 120 | + DType idxDtype = new DType.Primitive(idxPtype, false); |
| 121 | + DType valDtype = ctx.dtype(); |
| 122 | + |
| 123 | + Array idxArr = decodeChildAs(ctx, 1, idxDtype, numPatches); |
| 124 | + Array valArr = decodeChildAs(ctx, 2, valDtype, numPatches); |
| 125 | + |
| 126 | + var idxLayout = unsignedLayout(idxPtype); |
| 127 | + var valLayout = ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 128 | + |
| 129 | + ByteBuffer out = ByteBuffer.wrap(outBytes).order(ByteOrder.LITTLE_ENDIAN); |
| 130 | + MemorySegment idxSeg = idxArr.buffer(0); |
| 131 | + MemorySegment valSeg = valArr.buffer(0); |
| 132 | + |
| 133 | + for (long i = 0; i < numPatches; i++) { |
| 134 | + long absIdx = readUnsigned(idxSeg, i, idxPtype) - offset; |
| 135 | + long rawBits = valSeg.get(valLayout, i * 8); |
| 136 | + out.putLong((int) (absIdx * 8), rawBits); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // ── F32 ─────────────────────────────────────────────────────────────────── |
| 141 | + |
| 142 | + private Array decodeF32(DecodeContext ctx, EncodingProtos.ALPMetadata meta, int expE, int expF, long n) { |
| 143 | + DType encodedDtype = new DType.Primitive(PType.I32, false); |
| 144 | + Array encoded = decodeChildAs(ctx, 0, encodedDtype, n); |
| 145 | + |
| 146 | + float f10 = F10_F32[expF]; |
| 147 | + float if10 = IF10_F32[expE]; |
| 148 | + |
| 149 | + byte[] outBytes = new byte[(int) (n * 4)]; |
| 150 | + ByteBuffer out = ByteBuffer.wrap(outBytes).order(ByteOrder.LITTLE_ENDIAN); |
| 151 | + var srcLayout = ValueLayout.JAVA_INT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 152 | + MemorySegment src = encoded.buffer(0); |
| 153 | + for (long i = 0; i < n; i++) { |
| 154 | + int encVal = src.get(srcLayout, i * 4); |
| 155 | + float dec = (float) encVal * f10 * if10; |
| 156 | + out.putFloat(dec); |
| 157 | + } |
| 158 | + |
| 159 | + if (meta.hasPatches()) { |
| 160 | + applyPatchesF32(ctx, meta.getPatches(), outBytes, n); |
| 161 | + } |
| 162 | + |
| 163 | + return new Array(ctx.dtype(), n, |
| 164 | + new MemorySegment[]{MemorySegment.ofArray(outBytes)}, new Array[0], ArrayStats.empty()); |
| 165 | + } |
| 166 | + |
| 167 | + private void applyPatchesF32(DecodeContext ctx, EncodingProtos.PatchesMetadata pm, |
| 168 | + byte[] outBytes, long n) { |
| 169 | + long numPatches = pm.getLen(); |
| 170 | + long offset = pm.getOffset(); |
| 171 | + PType idxPtype = ptypeFromProto(pm.getIndicesPtype()); |
| 172 | + |
| 173 | + DType idxDtype = new DType.Primitive(idxPtype, false); |
| 174 | + DType valDtype = ctx.dtype(); |
| 175 | + |
| 176 | + Array idxArr = decodeChildAs(ctx, 1, idxDtype, numPatches); |
| 177 | + Array valArr = decodeChildAs(ctx, 2, valDtype, numPatches); |
| 178 | + |
| 179 | + var valLayout = ValueLayout.JAVA_INT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 180 | + |
| 181 | + ByteBuffer out = ByteBuffer.wrap(outBytes).order(ByteOrder.LITTLE_ENDIAN); |
| 182 | + MemorySegment idxSeg = idxArr.buffer(0); |
| 183 | + MemorySegment valSeg = valArr.buffer(0); |
| 184 | + |
| 185 | + for (long i = 0; i < numPatches; i++) { |
| 186 | + long absIdx = readUnsigned(idxSeg, i, idxPtype) - offset; |
| 187 | + int rawBits = valSeg.get(valLayout, i * 4); |
| 188 | + out.putInt((int) (absIdx * 4), rawBits); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // ── Helpers ─────────────────────────────────────────────────────────────── |
| 193 | + |
| 194 | + private static Array decodeChildAs(DecodeContext parent, int childIdx, DType dtype, long rowCount) { |
| 195 | + ArrayNode childNode = parent.node().children()[childIdx]; |
| 196 | + DecodeContext childCtx = new DecodeContext( |
| 197 | + childNode, dtype, rowCount, parent.segmentBuffers(), parent.registry()); |
| 198 | + return parent.registry().decode(childCtx); |
| 199 | + } |
| 200 | + |
| 201 | + private static long readUnsigned(MemorySegment seg, long i, PType ptype) { |
| 202 | + return switch (ptype) { |
| 203 | + case U8 -> Byte.toUnsignedLong(seg.get(ValueLayout.JAVA_BYTE, i)); |
| 204 | + case U16 -> Short.toUnsignedLong( |
| 205 | + seg.get(ValueLayout.JAVA_SHORT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN), i * 2)); |
| 206 | + case U32 -> Integer.toUnsignedLong( |
| 207 | + seg.get(ValueLayout.JAVA_INT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN), i * 4)); |
| 208 | + case U64 -> seg.get( |
| 209 | + ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN), i * 8); |
| 210 | + default -> throw new IllegalStateException("vortex.alp: non-unsigned patch index ptype " + ptype); |
| 211 | + }; |
| 212 | + } |
| 213 | + |
| 214 | + private static ValueLayout.OfLong unsignedLayout(PType ptype) { |
| 215 | + return ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 216 | + } |
| 217 | + |
| 218 | + private static PType ptypeFromProto(DTypeProtos.PType proto) { |
| 219 | + return PType.values()[proto.getNumber()]; |
| 220 | + } |
| 221 | +} |
0 commit comments