Instructions to use apus-ailab/APUS-Reflex with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use apus-ailab/APUS-Reflex with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("apus-ailab/APUS-Reflex", device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 13,951 Bytes
5c6d744 | 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | """Export actual 16-layer Qwen3.5-9B prefix, preserving its shared norm/head."""
import argparse
import copy
from datetime import datetime, timezone
import hashlib
import json
from pathlib import Path
import re
import shutil
LAYER = re.compile(r"^model\.language_model\.layers\.(\d+)\.")
READOUT = {
"model.language_model.embed_tokens.weight",
"model.language_model.norm.weight",
"lm_head.weight",
}
def file_sha(path):
h = hashlib.sha256()
with Path(path).open("rb") as f:
for block in iter(lambda: f.read(8 * 1024 * 1024), b""):
h.update(block)
return h.hexdigest()
def source_snapshot(source):
"""Stat all root model files; ignore unrelated nested evaluation directories."""
return {
p.name: {
"bytes": p.stat().st_size,
"mtime_ns": p.stat().st_mtime_ns,
"inode": p.stat().st_ino,
}
for p in sorted(Path(source).iterdir())
if p.is_file()
}
def assert_source_unchanged(source, before):
if source_snapshot(source) != before:
raise ValueError(
"source model files changed during export; refusing verified publication"
)
def verify_artifact(path, depth):
"""Default launcher gate: rehash every weight, not only config and index."""
path = Path(path)
config = json.loads((path / "config.json").read_text())
text = config.get("text_config", {})
if (
config.get("model_type") != "qwen3_5"
or text.get("hidden_size") != 4096
or text.get("vocab_size") != 248320
or text.get("num_hidden_layers") != depth
or len(text.get("layer_types", [])) != depth
or (path / "adapter_config.json").exists()
):
raise ValueError("backend architecture/depth mismatch")
if depth == 32:
check_source(path)
return
if depth != 16 or (path / "EXPORT_INCOMPLETE").exists():
raise ValueError("only complete 16/32 exports accepted")
manifest = json.loads((path / "export-low-provenance.json").read_text())
if (
manifest.get("status") != "verified"
or manifest.get("actual_depth") != 16
or manifest.get("all_retained_tensors_bit_identical") is not True
):
raise ValueError("low export provenance is not verified")
for name, field in (
("config.json", "export_config_sha256"),
("model.safetensors.index.json", "export_index_sha256"),
):
if file_sha(path / name) != manifest[field]:
raise ValueError("low config/index SHA mismatch")
weights = json.loads((path / "model.safetensors.index.json").read_text())[
"weight_map"
]
select_keys(weights, 16, 16)
entries = manifest["export_weight_files"]
expected = {entry["file"]: entry for entry in entries}
if len(expected) != len(entries) or set(expected) != set(weights.values()):
raise ValueError("low weight manifest/index mismatch")
for name, entry in expected.items():
if Path(name).name != name or not name.endswith(".safetensors"):
raise ValueError("invalid low shard path")
if (path / name).stat().st_size != entry["bytes"] or file_sha(
path / name
) != entry["sha256"]:
raise ValueError(f"low weight SHA mismatch: {name}")
def tensor_sha(tensor):
import torch
raw = memoryview(tensor.contiguous().view(torch.uint8).numpy()).cast("B")
h = hashlib.sha256()
for start in range(0, len(raw), 8 * 1024 * 1024):
h.update(raw[start : start + 8 * 1024 * 1024])
return h.hexdigest()
def prefix_config(config, depth=16):
result = copy.deepcopy(config)
text = result["text_config"]
if (
not 0 < depth < text["num_hidden_layers"]
or len(text["layer_types"]) != text["num_hidden_layers"]
):
raise ValueError("invalid depth or layer_types")
text["num_hidden_layers"] = depth
text["layer_types"] = text["layer_types"][:depth]
if "mlp_only_layers" in text:
text["mlp_only_layers"] = [i for i in text["mlp_only_layers"] if i < depth]
return result
def select_keys(keys, depth, full_depth):
kept, removed, seen = [], [], set()
for key in sorted(keys):
match = LAYER.match(key)
if match:
i = int(match.group(1))
if not 0 <= i < full_depth:
raise ValueError("layer outside full depth")
seen.add(i)
(kept if i < depth else removed).append(key)
elif key in READOUT or key.startswith("model.visual."):
kept.append(key)
else:
raise ValueError(f"unexpected weight namespace: {key}")
if seen != set(range(full_depth)) or not READOUT <= set(kept):
raise ValueError("missing decoder, embedding, final norm or lm_head")
return kept, removed
def check_source(source):
config = json.loads((source / "config.json").read_text())
text = config.get("text_config", {})
if (
config.get("model_type") != "qwen3_5"
or config.get("architectures") != ["Qwen3_5ForConditionalGeneration"]
or text.get("num_hidden_layers") != 32
or text.get("hidden_size") != 4096
or text.get("vocab_size") != 248320
or text.get("intermediate_size") != 12288
or config.get("tie_word_embeddings") is not False
or text.get("tie_word_embeddings") is not False
or (source / "adapter_config.json").exists()
):
raise ValueError("only merged, untied dense Qwen3.5-9B full32 accepted")
depth = json.loads((source / "depth_config.json").read_text())
if depth.get("exit_depth") != 16 or depth.get("full_depth") != 32:
raise ValueError("source must declare trained exits 16/32")
if text.get("layer_types") != (["linear_attention"] * 3 + ["full_attention"]) * 8:
raise ValueError("unexpected Qwen3.5-9B hybrid schedule")
weight_map = json.loads((source / "model.safetensors.index.json").read_text())[
"weight_map"
]
kept, removed = select_keys(weight_map, 16, 32)
manifest = json.loads((source / "release-manifest.json").read_text())
expected = {r["path"]: r["sha256"] for r in manifest["files"]}
source_files = []
for name in sorted(set(weight_map.values())):
if Path(name).name != name or not name.endswith(".safetensors"):
raise ValueError("weight shard must be a simple safetensors filename")
observed = file_sha(source / name)
if expected.get(name) != observed:
raise ValueError(f"source weight SHA mismatch: {name}")
source_files.append(
{"file": name, "sha256": observed, "bytes": (source / name).stat().st_size}
)
for name in (
"config.json",
"depth_config.json",
"model.safetensors.index.json",
"tokenizer.json",
"tokenizer_config.json",
"chat_template.jinja",
):
if name not in expected or file_sha(source / name) != expected[name]:
raise ValueError(f"source metadata SHA mismatch: {name}")
return config, weight_map, kept, removed, source_files
def copy_weights(source, output, weight_map, kept):
from safetensors import safe_open
from safetensors.torch import save_file
shards = [
n
for n in sorted(set(weight_map.values()))
if any(weight_map[k] == n for k in kept)
]
output_map, files, manifests = {}, [], []
total = 0
for ordinal, name in enumerate(shards, 1):
selected = sorted(k for k in kept if weight_map[k] == name)
with safe_open(source / name, framework="pt", device="cpu") as src:
if set(src.keys()) != {k for k, v in weight_map.items() if v == name}:
raise ValueError("source shard/index mismatch")
tensors = {k: src.get_tensor(k) for k in selected}
total += sum(t.numel() * t.element_size() for t in tensors.values())
out_name = f"model-low16-{ordinal:05d}-of-{len(shards):05d}.safetensors"
save_file(tensors, output / out_name, metadata={"format": "pt"})
with safe_open(output / out_name, framework="pt", device="cpu") as dst:
if set(dst.keys()) != set(selected):
raise ValueError("exported keys mismatch")
for key in selected:
original, exported = tensors[key], dst.get_tensor(key)
checksum = tensor_sha(original)
if (
original.dtype != exported.dtype
or original.shape != exported.shape
or checksum != tensor_sha(exported)
):
raise ValueError(f"exported tensor changed: {key}")
manifests.append(
{
"name": key,
"shape": list(original.shape),
"dtype": str(original.dtype),
"sha256": checksum,
}
)
output_map[key] = out_name
files.append(
{
"file": out_name,
"sha256": file_sha(output / out_name),
"bytes": (output / out_name).stat().st_size,
}
)
print(
json.dumps(
{"verified_export_shard": out_name, "tensor_count": len(selected)}
),
flush=True,
)
if set(output_map) != set(kept):
raise ValueError("export index incomplete")
(output / "model.safetensors.index.json").write_text(
json.dumps(
{"metadata": {"total_size": total}, "weight_map": output_map}, indent=2
)
+ "\n"
)
return files, manifests
def export(source, output):
from safetensors import safe_open
source, output = Path(source).resolve(), Path(output).resolve()
if output.exists() or source in output.parents:
raise ValueError("output must be a new directory outside source model")
before = source_snapshot(source)
config, weight_map, kept, removed, source_files = check_source(source)
for name in sorted(set(weight_map.values())):
with safe_open(source / name, framework="pt", device="cpu") as handle:
if set(handle.keys()) != {k for k, v in weight_map.items() if v == name}:
raise ValueError("source index/shard mismatch")
for key, shape in {
"lm_head.weight": (248320, 4096),
"model.language_model.embed_tokens.weight": (248320, 4096),
"model.language_model.norm.weight": (4096,),
}.items():
if (
weight_map[key] == name
and tuple(handle.get_slice(key).get_shape()) != shape
):
raise ValueError(f"unexpected shared readout shape: {key}")
output.mkdir(parents=True, exist_ok=False)
(output / "EXPORT_INCOMPLETE").write_text(
"Only provenance status=verified denotes success.\n"
)
(output / "config.json").write_text(
json.dumps(prefix_config(config), indent=2) + "\n"
)
for name in (
"tokenizer.json",
"tokenizer_config.json",
"chat_template.jinja",
"generation_config.json",
"special_tokens_map.json",
"added_tokens.json",
"vocab.json",
"merges.txt",
"preprocessor_config.json",
"processor_config.json",
"video_preprocessor_config.json",
"LICENSE",
):
if (source / name).is_file():
shutil.copy2(source / name, output / name)
files, tensors = copy_weights(source, output, weight_map, kept)
provenance = {
"format": "openjev.vllm-low-prefix.v1",
"status": "verified",
"created_utc": datetime.now(timezone.utc).isoformat(),
"source": str(source),
"source_full_depth": 32,
"actual_depth": 16,
"dynamic_continuation": False,
"selection": "original decoder 0..15 + shared embed/final norm/lm_head + all visual tensors",
"source_config_sha256": file_sha(source / "config.json"),
"source_depth_config": json.loads((source / "depth_config.json").read_text()),
"source_release_manifest_sha256": file_sha(source / "release-manifest.json"),
"source_index_sha256": file_sha(source / "model.safetensors.index.json"),
"source_weight_files": source_files,
"export_config_sha256": file_sha(output / "config.json"),
"export_index_sha256": file_sha(output / "model.safetensors.index.json"),
"export_weight_files": files,
"retained_tensors": tensors,
"removed_tensors": removed,
"all_retained_tensors_bit_identical": True,
"vllm_numerical_parity": "not established by CPU export; requires independent GPU comparison",
"script_sha256": file_sha(__file__),
}
assert_source_unchanged(source, before)
provenance["source_files_stat_before_after"] = before
provenance["source_files_unchanged_during_export"] = True
(output / "export-low-provenance.json").write_text(
json.dumps(provenance, indent=2) + "\n"
)
(output / "EXPORT_INCOMPLETE").unlink()
return provenance
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--source", type=Path, required=True)
p.add_argument("--output", type=Path, required=True)
args = p.parse_args()
result = export(args.source, args.output)
print(
json.dumps(
{
"status": result["status"],
"output": str(args.output),
"actual_depth": 16,
"retained_tensors": len(result["retained_tensors"]),
}
),
flush=True,
)
if __name__ == "__main__":
main()
|