File size: 4,668 Bytes
b8ecebf
 
 
 
e1d89d9
b8ecebf
 
 
 
 
 
 
 
 
 
 
 
da1f1b2
b8ecebf
 
 
 
 
 
 
 
b7cdc58
b8ecebf
 
 
 
 
 
 
 
 
da1f1b2
b8ecebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da1f1b2
b8ecebf
 
 
 
 
 
 
 
 
 
 
da1f1b2
b8ecebf
b7cdc58
b8ecebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da1f1b2
b8ecebf
 
 
 
 
 
 
 
b7cdc58
b8ecebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c363ca
 
 
9e6e93c
 
b7cdc58
 
 
 
5c363ca
 
 
9e6e93c
 
b7cdc58
 
 
 
5c363ca
 
 
9e6e93c
 
b7cdc58
 
 
 
5c363ca
 
 
9e6e93c
 
5c363ca
 
b7cdc58
 
 
 
5c363ca
 
9e6e93c
 
 
b7cdc58
 
 
9e6e93c
 
 
 
 
b7cdc58
 
 
 
9e6e93c
 
5c363ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {
  __internal_XetBlob as XetBlob,
  __internal_sha256,
} from "@huggingface/hub";

async function checkRange(n) {
  // Get selected option
  const select = document.getElementById("file");
  const option = select.options[select.selectedIndex];

  // hash is in data-xet-hash attribute
  const xetHash = option.getAttribute("data-xet-hash");
  const sha = option.getAttribute("data-sha");
  const size = +option.getAttribute("data-size");
  const path = `https://huggingface.co./celinah/xet-experiments/resolve/main/${option.value}`;

  const output = document.getElementById("output");
  output.textContent = "...";

  const blob = new XetBlob({
    repo: {
      type: "model",
      name: "celinah/xet-experiments",
    },
    hash: xetHash,
    size,
    internalLogging: true,
  });

  const [own, bridge] = await Promise.all([
    blob.slice(0, n).arrayBuffer(),
    fetch(path, { headers: { Range: `bytes=0-${n - 1}` } }).then((res) =>
      res.arrayBuffer()
    ),
  ]);

  output.textContent += `Own: ${own.byteLength.toLocaleString()}\nBridge: ${bridge.byteLength.toLocaleString()}\n`;
  // Check if the first n bytes are the same
  const array1 = new Uint8Array(own);
  const array2 = new Uint8Array(bridge);

  let different = false;
  for (let i = 0; i < n; i++) {
    if (array1[i] !== array2[i]) {
      output.textContent += `Difference at ${i}\n`;
      different = true;
      break;
    }
  }

  if (!different) {
    output.textContent += "No differences\n";
  }
}

async function download() {
  const select = document.getElementById("file");
  const option = select.options[select.selectedIndex];
  const xetHash = option.getAttribute("data-xet-hash");
  const size = +option.getAttribute("data-size");

  const output = document.getElementById("output");
  output.textContent = "...";

  const blob = new XetBlob({
    repo: {
      type: "model",
      name: "celinah/xet-experiments",
    },
    hash: xetHash,
    size,
    listener: (progress) => {
      output.textContent += JSON.stringify(progress) + "\n";
      output.textContent =
        output.textContent.split(/\n+/).slice(-1000).join("\n") + "\n";
    },
    internalLogging: true,
  });

  const arrayBuffer = await blob.arrayBuffer();
  const blob2 = new Blob([arrayBuffer]);
  const url = URL.createObjectURL(blob2);

  const a = document.createElement("a");
  a.href = url;
  a.download = option.value;
  a.click();
}

async function checkSha() {
  const select = document.getElementById("file");
  const option = select.options[select.selectedIndex];
  const xetHash = option.getAttribute("data-xet-hash");
  const sha = option.getAttribute("data-sha");
  const size = +option.getAttribute("data-size");

  const output = document.getElementById("output");
  output.textContent = "...";

  const blob = new XetBlob({
    repo: {
      type: "model",
      name: "celinah/xet-experiments",
    },
    hash: xetHash,
    size,
    internalLogging: true,
  });

  const iterator = __internal_sha256(blob);

  while (1) {
    const result = await iterator.next();

    if (result.done) {
      const hash = result.value;
      output.textContent += `SHA: ${hash}\n`;
      output.textContent += `Expected: ${sha}\n`;
      output.textContent += `Match: ${hash === sha}\n`;
      break;
    }

    output.textContent = `Progress: ${result.value * 100}%\n`;
  }
}

// On loaded, add event listeners
window.document.addEventListener("DOMContentLoaded", () => {
  console.log("Loaded");
  document.getElementById("10kB").addEventListener("click", () => {
    console.clear();

    checkRange(10_000).catch((err) => {
      console.error(err);
      alert(err);
    });
  });

  document.getElementById("100kB").addEventListener("click", () => {
    console.clear();

    checkRange(100_000).catch((err) => {
      console.error(err);
      alert(err);
    });
  });

  document.getElementById("1MB").addEventListener("click", () => {
    console.clear();

    checkRange(1_000_000).catch((err) => {
      console.error(err);
      alert(err);
    });
  });

  document.getElementById("sequential").addEventListener("click", () => {
    console.clear();

    checkRange(100_000)
      .then(() => checkRange(1_000_000))
      .catch((err) => {
        console.error(err);
        alert(err);
      });
  });

  document.getElementById("download").addEventListener("click", () => {
    console.clear();

    download().catch((err) => {
      console.error(err);
      alert(err);
    });
  });

  document.getElementById("sha").addEventListener("click", () => {
    console.clear();

    checkSha().catch((err) => {
      console.error(err);
      alert(err);
    });
  });
});