|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { bg4_regoup_bytes, XetBlob } from "./XetBlob"; |
| 3 | + |
| 4 | +describe("XetBlob", () => { |
| 5 | + it("should lazy load the first 22 bytes", async () => { |
| 6 | + const blob = new XetBlob({ |
| 7 | + repo: { |
| 8 | + type: "model", |
| 9 | + name: "celinah/xet-experiments", |
| 10 | + }, |
| 11 | + hash: "7b3b6d07673a88cf467e67c1f7edef1a8c268cbf66e9dd9b0366322d4ab56d9b", |
| 12 | + size: 5_234_139_343, |
| 13 | + }); |
| 14 | + |
| 15 | + expect(await blob.slice(10, 22).text()).toBe("__metadata__"); |
| 16 | + }, 30_000); |
| 17 | + |
| 18 | + it("should load the first chunk correctly", async () => { |
| 19 | + let xorbCount = 0; |
| 20 | + const blob = new XetBlob({ |
| 21 | + repo: { |
| 22 | + type: "model", |
| 23 | + name: "celinah/xet-experiments", |
| 24 | + }, |
| 25 | + hash: "7b3b6d07673a88cf467e67c1f7edef1a8c268cbf66e9dd9b0366322d4ab56d9b", |
| 26 | + size: 5_234_139_343, |
| 27 | + fetch: async (url, opts) => { |
| 28 | + if (typeof url === "string" && url.includes("/xorbs/")) { |
| 29 | + xorbCount++; |
| 30 | + } |
| 31 | + return fetch(url, opts); |
| 32 | + }, |
| 33 | + }); |
| 34 | + |
| 35 | + const xetDownload = await blob.slice(0, 29928).arrayBuffer(); |
| 36 | + const bridgeDownload = await fetch( |
| 37 | + "https://huggingface.co/celinah/xet-experiments/resolve/main/model5GB.safetensors", |
| 38 | + { |
| 39 | + headers: { |
| 40 | + Range: "bytes=0-29927", |
| 41 | + }, |
| 42 | + } |
| 43 | + ).then((res) => res.arrayBuffer()); |
| 44 | + |
| 45 | + expect(new Uint8Array(xetDownload)).toEqual(new Uint8Array(bridgeDownload)); |
| 46 | + expect(xorbCount).toBe(1); |
| 47 | + }, 30_000); |
| 48 | + |
| 49 | + it("should load just past the first chunk correctly", async () => { |
| 50 | + let xorbCount = 0; |
| 51 | + const blob = new XetBlob({ |
| 52 | + repo: { |
| 53 | + type: "model", |
| 54 | + name: "celinah/xet-experiments", |
| 55 | + }, |
| 56 | + hash: "7b3b6d07673a88cf467e67c1f7edef1a8c268cbf66e9dd9b0366322d4ab56d9b", |
| 57 | + size: 5_234_139_343, |
| 58 | + fetch: async (url, opts) => { |
| 59 | + if (typeof url === "string" && url.includes("/xorbs/")) { |
| 60 | + xorbCount++; |
| 61 | + } |
| 62 | + return fetch(url, opts); |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + const xetDownload = await blob.slice(0, 29929).arrayBuffer(); |
| 67 | + const bridgeDownload = await fetch( |
| 68 | + "https://huggingface.co/celinah/xet-experiments/resolve/main/model5GB.safetensors", |
| 69 | + { |
| 70 | + headers: { |
| 71 | + Range: "bytes=0-29928", |
| 72 | + }, |
| 73 | + } |
| 74 | + ).then((res) => res.arrayBuffer()); |
| 75 | + |
| 76 | + expect(new Uint8Array(xetDownload)).toEqual(new Uint8Array(bridgeDownload)); |
| 77 | + expect(xetDownload.byteLength).toBe(29929); |
| 78 | + expect(xorbCount).toBe(2); |
| 79 | + }); |
| 80 | + |
| 81 | + // In github actions, this test doesn't work inside the browser, but it works locally |
| 82 | + // inside both chrome and chromium browsers |
| 83 | + // TODO: figure out why |
| 84 | + if (typeof window === "undefined") { |
| 85 | + it("should load correctly when loading far into a chunk range", async () => { |
| 86 | + const blob = new XetBlob({ |
| 87 | + repo: { |
| 88 | + type: "model", |
| 89 | + name: "celinah/xet-experiments", |
| 90 | + }, |
| 91 | + hash: "7b3b6d07673a88cf467e67c1f7edef1a8c268cbf66e9dd9b0366322d4ab56d9b", |
| 92 | + size: 5_234_139_343, |
| 93 | + }); |
| 94 | + |
| 95 | + const xetDownload = await blob.slice(10_000_000, 10_100_000).arrayBuffer(); |
| 96 | + const bridgeDownload = await fetch( |
| 97 | + "https://huggingface.co/celinah/xet-experiments/resolve/main/model5GB.safetensors", |
| 98 | + { |
| 99 | + headers: { |
| 100 | + Range: "bytes=10000000-10099999", |
| 101 | + }, |
| 102 | + } |
| 103 | + ).then((res) => res.arrayBuffer()); |
| 104 | + |
| 105 | + console.log("xet", xetDownload.byteLength, "bridge", bridgeDownload.byteLength); |
| 106 | + expect(new Uint8Array(xetDownload)).toEqual(new Uint8Array(bridgeDownload)); |
| 107 | + }, 30_000); |
| 108 | + } |
| 109 | + |
| 110 | + it("should load text correctly when offset_into_range starts in a chunk further than the first", async () => { |
| 111 | + const blob = new XetBlob({ |
| 112 | + repo: { |
| 113 | + type: "model", |
| 114 | + name: "celinah/xet-experiments", |
| 115 | + }, |
| 116 | + hash: "794efea76d8cb372bbe1385d9e51c3384555f3281e629903ecb6abeff7d54eec", |
| 117 | + size: 62_914_580, |
| 118 | + }); |
| 119 | + |
| 120 | + // Reconstruction info |
| 121 | + // { |
| 122 | + // "offset_into_first_range": 600000, |
| 123 | + // "terms": |
| 124 | + // [ |
| 125 | + // { |
| 126 | + // "hash": "be748f77930d5929cabd510a15f2c30f2f460b639804ef79dea46affa04fd8b2", |
| 127 | + // "unpacked_length": 655360, |
| 128 | + // "range": { "start": 0, "end": 5 }, |
| 129 | + // }, |
| 130 | + // { |
| 131 | + // "hash": "be748f77930d5929cabd510a15f2c30f2f460b639804ef79dea46affa04fd8b2", |
| 132 | + // "unpacked_length": 655360, |
| 133 | + // "range": { "start": 0, "end": 5 }, |
| 134 | + // }, |
| 135 | + // ], |
| 136 | + // "fetch_info": |
| 137 | + // { |
| 138 | + // "be748f77930d5929cabd510a15f2c30f2f460b639804ef79dea46affa04fd8b2": |
| 139 | + // [ |
| 140 | + // { |
| 141 | + // "range": { "start": 0, "end": 5 }, |
| 142 | + // "url": "...", |
| 143 | + // "url_range": { "start": 0, "end": 2839 }, |
| 144 | + // }, |
| 145 | + // ], |
| 146 | + // }, |
| 147 | + // } |
| 148 | + |
| 149 | + const text = await blob.slice(600_000, 700_000).text(); |
| 150 | + const bridgeDownload = await fetch("https://huggingface.co/celinah/xet-experiments/resolve/main/large_text.txt", { |
| 151 | + headers: { |
| 152 | + Range: "bytes=600000-699999", |
| 153 | + }, |
| 154 | + }).then((res) => res.text()); |
| 155 | + |
| 156 | + console.log("xet", text.length, "bridge", bridgeDownload.length); |
| 157 | + expect(text.length).toBe(bridgeDownload.length); |
| 158 | + }, 30_000); |
| 159 | + |
| 160 | + describe("bg4_regoup_bytes", () => { |
| 161 | + it("should regroup bytes when the array is %4 length", () => { |
| 162 | + expect(bg4_regoup_bytes(new Uint8Array([1, 5, 2, 6, 3, 7, 4, 8]))).toEqual( |
| 163 | + new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]) |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + it("should regroup bytes when the array is %4 + 1 length", () => { |
| 168 | + expect(bg4_regoup_bytes(new Uint8Array([1, 5, 9, 2, 6, 3, 7, 4, 8]))).toEqual( |
| 169 | + new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]) |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it("should regroup bytes when the array is %4 + 2 length", () => { |
| 174 | + expect(bg4_regoup_bytes(new Uint8Array([1, 5, 9, 2, 6, 10, 3, 7, 4, 8]))).toEqual( |
| 175 | + new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 176 | + ); |
| 177 | + }); |
| 178 | + |
| 179 | + it("should regroup bytes when the array is %4 + 3 length", () => { |
| 180 | + expect(bg4_regoup_bytes(new Uint8Array([1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8]))).toEqual( |
| 181 | + new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) |
| 182 | + ); |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments