zziang commited on
Commit
db88d9c
·
verified ·
1 Parent(s): c6333a5

export: drop removed kwarg, host-side numpy

Browse files
Files changed (1) hide show
  1. kaggle/export_browser.py +15 -12
kaggle/export_browser.py CHANGED
@@ -49,6 +49,10 @@ def patch_fp32() -> None:
49
  p.write_text(s)
50
 
51
 
 
 
 
 
52
  def cosine(a, b) -> float:
53
  a = np.asarray(a, dtype=np.float64).reshape(-1)
54
  b = np.asarray(b, dtype=np.float64).reshape(-1)
@@ -121,17 +125,17 @@ def stage_lm(mm, torch) -> dict:
121
  torch.onnx.export(wrapper, (embeds, mask, pos, *past), str(path),
122
  input_names=names_in, output_names=names_out,
123
  dynamic_axes=dyn, opset_version=20, do_constant_folding=True,
124
- use_external_data_format=True, dynamo=False)
125
  log(f"{path.name}: {path.stat().st_size / 1e6:.0f} MB")
126
 
127
  sess = ort.InferenceSession(str(path), providers=["CPUExecutionProvider"])
128
- feeds = {"inputs_embeds": embeds.numpy(), "attention_mask": mask.numpy(),
129
- "position_ids": pos.numpy()}
130
  for i in range(n):
131
- feeds[f"past_k_{i}"] = past[2 * i].numpy()
132
- feeds[f"past_v_{i}"] = past[2 * i + 1].numpy()
133
  got = sess.run(None, feeds)
134
- cos = [cosine(ref[i].numpy(), got[i]) for i in range(3)]
135
  log(f"validate hidden cos={cos[0]:.6f} k0={cos[1]:.6f} v0={cos[2]:.6f}")
136
  return {"graph": "lm", "mb": round(path.stat().st_size / 1e6, 1),
137
  "cos_hidden": cos[0], "layers": n}
@@ -152,8 +156,8 @@ def stage_agg(mm, torch) -> dict:
152
  dynamic_axes={"latent": {1: "t"}}, opset_version=20,
153
  do_constant_folding=True, dynamo=False)
154
  sess = ort.InferenceSession(str(path), providers=["CPUExecutionProvider"])
155
- got = sess.run(None, {"latent": dummy.numpy()})[0]
156
- c = cosine(ref.numpy(), got)
157
  log(f"aggregator.onnx {path.stat().st_size / 1e6:.0f} MB cos={c:.6f}")
158
  return {"graph": "aggregator", "mb": round(path.stat().st_size / 1e6, 1),
159
  "cos": c, "dim_in": dim_in, "dim_out": int(ref.shape[-1])}
@@ -187,13 +191,12 @@ def stage_voc(mm, torch) -> dict:
187
  path = OUT / "voc.onnx"
188
  torch.onnx.export(voc, (dummy,), str(path), input_names=["latent"],
189
  output_names=["waveform"], dynamic_axes={"latent": {1: "t"}},
190
- opset_version=20, do_constant_folding=True,
191
- use_external_data_format=True, dynamo=False)
192
  sess = ort.InferenceSession(str(path), providers=["CPUExecutionProvider"])
193
- got = sess.run(None, {"latent": dummy.numpy()})[0]
194
  if got.ndim == 2:
195
  got = got[:, None]
196
- c = cosine(ref.numpy(), got)
197
  log(f"voc.onnx {path.stat().st_size / 1e6:.0f} MB cos={c:.6f} out={got.shape}")
198
  return {"graph": "voc", "mb": round(path.stat().st_size / 1e6, 1), "cos": c}
199
 
 
49
  p.write_text(s)
50
 
51
 
52
+ def np_(t):
53
+ return t.detach().cpu().numpy()
54
+
55
+
56
  def cosine(a, b) -> float:
57
  a = np.asarray(a, dtype=np.float64).reshape(-1)
58
  b = np.asarray(b, dtype=np.float64).reshape(-1)
 
125
  torch.onnx.export(wrapper, (embeds, mask, pos, *past), str(path),
126
  input_names=names_in, output_names=names_out,
127
  dynamic_axes=dyn, opset_version=20, do_constant_folding=True,
128
+ dynamo=False)
129
  log(f"{path.name}: {path.stat().st_size / 1e6:.0f} MB")
130
 
131
  sess = ort.InferenceSession(str(path), providers=["CPUExecutionProvider"])
132
+ feeds = {"inputs_embeds": np_(embeds), "attention_mask": np_(mask),
133
+ "position_ids": np_(pos)}
134
  for i in range(n):
135
+ feeds[f"past_k_{i}"] = np_(past[2 * i])
136
+ feeds[f"past_v_{i}"] = np_(past[2 * i + 1])
137
  got = sess.run(None, feeds)
138
+ cos = [cosine(np_(ref[i]), got[i]) for i in range(3)]
139
  log(f"validate hidden cos={cos[0]:.6f} k0={cos[1]:.6f} v0={cos[2]:.6f}")
140
  return {"graph": "lm", "mb": round(path.stat().st_size / 1e6, 1),
141
  "cos_hidden": cos[0], "layers": n}
 
156
  dynamic_axes={"latent": {1: "t"}}, opset_version=20,
157
  do_constant_folding=True, dynamo=False)
158
  sess = ort.InferenceSession(str(path), providers=["CPUExecutionProvider"])
159
+ got = sess.run(None, {"latent": np_(dummy)})[0]
160
+ c = cosine(np_(ref), got)
161
  log(f"aggregator.onnx {path.stat().st_size / 1e6:.0f} MB cos={c:.6f}")
162
  return {"graph": "aggregator", "mb": round(path.stat().st_size / 1e6, 1),
163
  "cos": c, "dim_in": dim_in, "dim_out": int(ref.shape[-1])}
 
191
  path = OUT / "voc.onnx"
192
  torch.onnx.export(voc, (dummy,), str(path), input_names=["latent"],
193
  output_names=["waveform"], dynamic_axes={"latent": {1: "t"}},
194
+ opset_version=20, do_constant_folding=True, dynamo=False)
 
195
  sess = ort.InferenceSession(str(path), providers=["CPUExecutionProvider"])
196
+ got = sess.run(None, {"latent": np_(dummy)})[0]
197
  if got.ndim == 2:
198
  got = got[:, None]
199
+ c = cosine(np_(ref), got)
200
  log(f"voc.onnx {path.stat().st_size / 1e6:.0f} MB cos={c:.6f} out={got.shape}")
201
  return {"graph": "voc", "mb": round(path.stat().st_size / 1e6, 1), "cos": c}
202