Instructions to use NghiaNguyen1529/octen-embedding-0.6b-directml-onnx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use NghiaNguyen1529/octen-embedding-0.6b-directml-onnx with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("NghiaNguyen1529/octen-embedding-0.6b-directml-onnx") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Notebooks
- Google Colab
- Kaggle
Add fix_octen_dml.py reproduction script
Browse files- fix_octen_dml.py +171 -0
fix_octen_dml.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Fix Octen-Embedding-0.6B ONNX for DirectML compatibility.
|
| 3 |
+
|
| 4 |
+
Root cause: dynamo torch.onnx.export generates `val_41 = [-1]` used in Reshape
|
| 5 |
+
shapes for multi-head attention Q/K/V projections. ONNX resolves -1 differently
|
| 6 |
+
per input (16 for Q with 2048-dim output, 8 for K/V with 1024-dim output).
|
| 7 |
+
DirectML's execution provider needs concrete shape values at graph-capture time
|
| 8 |
+
and crashes on symbolic -1 dims.
|
| 9 |
+
|
| 10 |
+
Fix: Replace val_41=[-1] with three concrete head-count constants:
|
| 11 |
+
- val_41_q = [16] (Q projection: num_attention_heads=16)
|
| 12 |
+
- val_41_k = [8] (K projection: num_key_value_heads=8)
|
| 13 |
+
- val_41_v = [8] (V projection: num_key_value_heads=8)
|
| 14 |
+
|
| 15 |
+
Create separate Concat nodes for each shape and reconnect the Reshape consumers
|
| 16 |
+
based on the Q/K/V naming pattern:
|
| 17 |
+
- node_view{4L+0} (Q) -> val_50_q
|
| 18 |
+
- node_view{4L+1} (K) -> val_50_k
|
| 19 |
+
- node_view{4L+2} (V) -> val_50_v
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
import sys
|
| 23 |
+
import os
|
| 24 |
+
import shutil
|
| 25 |
+
from pathlib import Path
|
| 26 |
+
|
| 27 |
+
import onnx
|
| 28 |
+
import numpy as np
|
| 29 |
+
from onnx import helper, TensorProto
|
| 30 |
+
|
| 31 |
+
# octen-embedding-0.6b attention heads
|
| 32 |
+
NUM_Q_HEADS = 16
|
| 33 |
+
NUM_KV_HEADS = 8
|
| 34 |
+
HEAD_DIM = 128
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def patch_octen_for_directml(input_path: str, output_path: str):
|
| 38 |
+
print(f"Loading: {input_path}")
|
| 39 |
+
m = onnx.load(input_path)
|
| 40 |
+
g = m.graph
|
| 41 |
+
initials = {i.name: i for i in g.initializer}
|
| 42 |
+
|
| 43 |
+
# --- 1. Create new head-count initializers ---
|
| 44 |
+
def make_int64_initializer(name: str, value: int) -> TensorProto:
|
| 45 |
+
return helper.make_tensor(name, TensorProto.INT64, [1], [value])
|
| 46 |
+
|
| 47 |
+
new_inits = [
|
| 48 |
+
make_int64_initializer("val_41_q", NUM_Q_HEADS),
|
| 49 |
+
make_int64_initializer("val_41_k", NUM_KV_HEADS),
|
| 50 |
+
make_int64_initializer("val_41_v", NUM_KV_HEADS),
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
# Add them to the graph (before the original val_41 so they're available)
|
| 54 |
+
val_41_idx = next(i for i, init in enumerate(g.initializer) if init.name == "val_41")
|
| 55 |
+
for offset, init in enumerate(new_inits):
|
| 56 |
+
g.initializer.insert(val_41_idx + offset, init)
|
| 57 |
+
|
| 58 |
+
# --- 2. Create new Concat nodes for val_50_q, val_50_k, val_50_v ---
|
| 59 |
+
# Original: val_50 = Concat([val_0 (batch), val_1 (seq), val_41 (-1), val_49 (128)])
|
| 60 |
+
# New: val_50_q = Concat([val_0, val_1, val_41_q(16), val_49])
|
| 61 |
+
# val_50_k = Concat([val_0, val_1, val_41_k(8), val_49])
|
| 62 |
+
# val_50_v = Concat([val_0, val_1, val_41_v(8), val_49])
|
| 63 |
+
#
|
| 64 |
+
# The original val_50 Concat has attribute axis=0
|
| 65 |
+
val_50_node = next(n for n in g.node if "val_50" in n.output)
|
| 66 |
+
|
| 67 |
+
for suffix, val_name in [("q", "val_41_q"), ("k", "val_41_k"), ("v", "val_41_v")]:
|
| 68 |
+
new_concat = helper.make_node(
|
| 69 |
+
"Concat",
|
| 70 |
+
inputs=[val_50_node.input[0], val_50_node.input[1], val_name, val_50_node.input[3]],
|
| 71 |
+
outputs=[f"val_50_{suffix}"],
|
| 72 |
+
name=f"node_val_50_{suffix}",
|
| 73 |
+
axis=0,
|
| 74 |
+
)
|
| 75 |
+
# Insert after the original val_50 Concat
|
| 76 |
+
val_50_pos = next(i for i, n in enumerate(g.node) if n.name == val_50_node.name)
|
| 77 |
+
g.node.insert(val_50_pos + 1 + {"q": 0, "k": 1, "v": 2}[suffix], new_concat)
|
| 78 |
+
|
| 79 |
+
# --- 3. Reconnect Reshape consumers ---
|
| 80 |
+
# Per layer L (0..27):
|
| 81 |
+
# linear_{7L+0} (Q weight dims [1024,2048]) -> node_view_{4L+0}
|
| 82 |
+
# linear_{7L+1} (K weight dims [1024,1024]) -> node_view_{4L+1}
|
| 83 |
+
# linear_{7L+2} (V weight dims [1024,1024]) -> node_view_{4L+2}
|
| 84 |
+
#
|
| 85 |
+
# So: node_view_{4L+0} uses val_50_q, node_view_{4L+1} uses val_50_k,
|
| 86 |
+
# node_view_{4L+2} uses val_50_v
|
| 87 |
+
|
| 88 |
+
consumers = [n for n in g.node if "val_50" in n.input]
|
| 89 |
+
import re
|
| 90 |
+
|
| 91 |
+
q_patches = 0
|
| 92 |
+
k_patches = 0
|
| 93 |
+
v_patches = 0
|
| 94 |
+
|
| 95 |
+
for n in consumers:
|
| 96 |
+
name = n.name
|
| 97 |
+
match = re.match(r"node_view_(\d+)$", name)
|
| 98 |
+
if match:
|
| 99 |
+
idx = int(match.group(1))
|
| 100 |
+
# Q: idx % 4 == 0, K: idx % 4 == 1, V: idx % 4 == 2
|
| 101 |
+
if idx % 4 == 0:
|
| 102 |
+
replacement = "val_50_q"
|
| 103 |
+
q_patches += 1
|
| 104 |
+
elif idx % 4 == 1:
|
| 105 |
+
replacement = "val_50_k"
|
| 106 |
+
k_patches += 1
|
| 107 |
+
elif idx % 4 == 2:
|
| 108 |
+
replacement = "val_50_v"
|
| 109 |
+
v_patches += 1
|
| 110 |
+
else:
|
| 111 |
+
print(f" WARNING: unexpected index {idx} for {name}, skipping")
|
| 112 |
+
continue
|
| 113 |
+
elif name == "node_view":
|
| 114 |
+
# node_view (no suffix) is Q for layer 0
|
| 115 |
+
replacement = "val_50_q"
|
| 116 |
+
q_patches += 1
|
| 117 |
+
else:
|
| 118 |
+
print(f" WARNING: unexpected consumer {name}, skipping")
|
| 119 |
+
continue
|
| 120 |
+
|
| 121 |
+
# Replace val_50 with the specific variant in the Reshape's inputs
|
| 122 |
+
new_inputs = [
|
| 123 |
+
replacement if inp == "val_50" else inp for inp in n.input
|
| 124 |
+
]
|
| 125 |
+
del n.input[:]
|
| 126 |
+
n.input.extend(new_inputs)
|
| 127 |
+
|
| 128 |
+
print(f" Q reshapes patched: {q_patches}")
|
| 129 |
+
print(f" K reshapes patched: {k_patches}")
|
| 130 |
+
print(f" V reshapes patched: {v_patches}")
|
| 131 |
+
print(f" Total: {q_patches + k_patches + v_patches}")
|
| 132 |
+
|
| 133 |
+
# --- 4. Clean up: remove original val_41 and val_50 (optional, but cleaner) ---
|
| 134 |
+
# We keep them to avoid breaking anything else that might reference them.
|
| 135 |
+
# val_50 only has the 84 Reshape consumers; val_41 might be used elsewhere.
|
| 136 |
+
|
| 137 |
+
# --- 5. Check op checksums / validate ---
|
| 138 |
+
try:
|
| 139 |
+
onnx.checker.check_model(m)
|
| 140 |
+
print(" ONNX validation: PASSED")
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f" ONNX validation WARNING: {e}")
|
| 143 |
+
print(" (non-critical, DirectML may still accept it)")
|
| 144 |
+
|
| 145 |
+
# --- 6. Save ---
|
| 146 |
+
# Handle external data files
|
| 147 |
+
output_dir = os.path.dirname(output_path) or "."
|
| 148 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 149 |
+
|
| 150 |
+
# If the model has external data, copy the .data file
|
| 151 |
+
input_dir = os.path.dirname(input_path)
|
| 152 |
+
data_file = input_path + ".data"
|
| 153 |
+
if os.path.exists(data_file):
|
| 154 |
+
output_data = output_path + ".data"
|
| 155 |
+
print(f" Copying external data: {data_file} -> {output_data}")
|
| 156 |
+
shutil.copy2(data_file, output_data)
|
| 157 |
+
|
| 158 |
+
onnx.save(m, output_path)
|
| 159 |
+
size_mb = os.path.getsize(output_path) / (1024 * 1024)
|
| 160 |
+
print(f" Saved: {output_path} ({size_mb:.1f} MB)")
|
| 161 |
+
print("Done!")
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
if __name__ == "__main__":
|
| 165 |
+
if len(sys.argv) < 2:
|
| 166 |
+
print("Usage: python fix_octen_dml.py <input.onnx> [output.onnx]")
|
| 167 |
+
sys.exit(1)
|
| 168 |
+
|
| 169 |
+
input_path = sys.argv[1]
|
| 170 |
+
output_path = sys.argv[2] if len(sys.argv) > 2 else input_path.replace(".onnx", "_dml.onnx")
|
| 171 |
+
patch_octen_for_directml(input_path, output_path)
|