wsbuilder 2.0.1__tar.gz → 2.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/CHANGELOG.md +11 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/CONTEXT.md +7 -1
- {wsbuilder-2.0.1/src/wsbuilder.egg-info → wsbuilder-2.0.2}/PKG-INFO +1 -1
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/llm.md +15 -2
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/_version.py +3 -3
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/graph.py +199 -6
- {wsbuilder-2.0.1 → wsbuilder-2.0.2/src/wsbuilder.egg-info}/PKG-INFO +1 -1
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder.egg-info/scm_version.json +2 -2
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_accel.py +212 -4
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_graph.py +20 -3
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_graph_gguf.py +28 -5
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/CODEOWNERS +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/SECURITY.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/dependabot.yml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/rulesets/protect-main.json +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/rulesets/protect-tags-v.json +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/workflows/codeql.yml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/workflows/docs-pages.yml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/workflows/package-build.yml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/workflows/pr-policy.yml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/workflows/publish-packages.yml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/workflows/release-from-main.yml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.github/workflows/sync-rulesets.yml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/.gitignore +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/CONTRIBUTING.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/LICENSE +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/MANIFEST.in +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/README.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/SUPPORT.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/benchmarks/graph_llm.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/benchmarks/http1_keepalive.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/404.html +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/CNAME +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/advanced.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/ai-predict.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/api-map.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/app-http.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/architecture.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/assets/wsbuilder-mark.svg +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/beginners.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/cache-security.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/cache.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/capabilities.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/changelog.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/data-orm.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/dns.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/full-project.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/getting-started.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/help/index.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/http-versions.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/http.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/ia.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/index.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/install.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/intermediate.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/mdns.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/network-edge.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/observability-tasks.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/observability.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/persistence.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/proxy.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/reference/index.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/rpc.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/security.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/stylesheets/extra.css +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/tasks.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/tls-certificates.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/docs/websocket.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/examples/full-project/.gitignore +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/examples/full-project/README.md +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/examples/full-project/app.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/examples/full-project/pyproject.toml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/examples/full-project/upstream.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/mkdocs.yml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/pyproject.toml +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/requirements-docs.txt +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/setup.cfg +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/__init__.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/__main__.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/_accel_kernels.c +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/_access.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/_coerce.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/_pragma.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/accel.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/app.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/bpe.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/cache.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/caches.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/codec.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/constants.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/cookies.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/db_replicas.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/dns.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/framework.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/graph_convert.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/headers.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/hpack.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/http.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/http1.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/http2.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/http3.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/http3_client.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/http3_server.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/http3_websocket.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/ia.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/ia_audio.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/ia_lm.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/ia_text.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/llm.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/logs.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/mdns.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/metrics.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/models.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/ndjson_model.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/orm.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/pki.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/predicts.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/proxyi/__init__.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/proxyi/_common.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/proxyi/core.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/proxyi/routing.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/proxyi/stats.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/qpack.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/__init__.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/address.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/client.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/connection.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/crypto.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/frames.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/packet.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/recovery.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/tls.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/quic/varint.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/rpc.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/security.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/server.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/spm.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/tasks.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/weights.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder/ws.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder.egg-info/SOURCES.txt +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder.egg-info/dependency_links.txt +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder.egg-info/entry_points.txt +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder.egg-info/requires.txt +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder.egg-info/scm_file_list.json +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/src/wsbuilder.egg-info/top_level.txt +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_app_threads.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_bpe.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_cache.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_caches.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_codec.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_contributing_policy.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_demo_monitor.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_dns.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_docs.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_documentation_files.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_example_full_project.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_headers_cookies.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_hpack.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_http.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_http1.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_http2.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_http3.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_http3_server.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_http3_streaming.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_http3_streaming_udp.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_ia.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_ia_lm.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_ia_text_audio.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_llm.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_llm_graph_store.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_logs.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_mdns.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_metrics.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_models.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_ndjson_model.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_optional_extras.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_orm.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_orm_replicas.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_pki.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_pr_identity_policy.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_predicts.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_proxyi.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_proxyi_routing.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_quants.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_quic_address.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_quic_client.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_quic_crypto.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_quic_protocol.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_quic_recovery.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_quic_tls.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_response_lifecycle.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_rpc.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_security.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_server.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_server_errors.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_tasks.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_weights.py +0 -0
- {wsbuilder-2.0.1 → wsbuilder-2.0.2}/tests/test_ws.py +0 -0
|
@@ -29,6 +29,17 @@ next pull request moves those entries under the new version heading.
|
|
|
29
29
|
|
|
30
30
|
### Added
|
|
31
31
|
|
|
32
|
+
- Graph models fuse matrices when they load: consecutive `linear` ops that read
|
|
33
|
+
the same value (Q/K/V, gate/up) run as one `matvec`/`matmul` over their
|
|
34
|
+
matrices stacked by rows, with fewer backend calls and thread-pool
|
|
35
|
+
synchronisations. The pattern is generic (same input and `layers`, same
|
|
36
|
+
dtype, encoding and width, a bias on all or none, tensors no other op uses;
|
|
37
|
+
an op that rewrites its input ends the group) and the parts are never stored
|
|
38
|
+
apart. Each row gives the doubles it gives alone, so logits and the KV cache
|
|
39
|
+
are identical bit for bit. `GraphModel(..., fuse=True)` and
|
|
40
|
+
`GraphModel.load(..., fuse=True)`; `fuse=False` turns it off and
|
|
41
|
+
`fused_linears` counts the groups. Qwen3-0.6B Q8_0 (native): decoding about
|
|
42
|
+
6-9% faster and prompt prefill 3-4% faster; Qwen3-4B Q4_K_M about 4-5%.
|
|
32
43
|
- Graph models attend a whole prompt batch per layer in one call: the
|
|
33
44
|
`attention` op has a batched runner that appends the keys and values of the
|
|
34
45
|
batch and attends every query to the positions up to its own (sliding
|
|
@@ -463,6 +463,12 @@ console_level="warning")` con `log(level, event, message="", exc=None,
|
|
|
463
463
|
`extend` por cabeza); `native` usa `wsb_attend_heads` (ABI 6: items
|
|
464
464
|
(query, cabeza KV) en rangos `item_start`/`item_end` para hilos, cada uno con
|
|
465
465
|
`wsb_attend`, también para `attend` de un token) y `wsb_kv_scatter`.
|
|
466
|
+
Fusión al cargar (`fuse=True` en `GraphModel`/`load`; `fused_linears` cuenta
|
|
467
|
+
los grupos): `_Compiler._linear_group` junta las `linear` seguidas con la
|
|
468
|
+
misma entrada, `layers`, dtype/encoding/ancho y bias (todas o ninguna) cuyos
|
|
469
|
+
tensores no usa otra op (`_tensor_uses`); `_StackedTensor` las apila por
|
|
470
|
+
filas en una sola matriz del backend (sin copias aparte) y la salida se corta
|
|
471
|
+
por op. Bit a bit igual que sin fusión.
|
|
466
472
|
Ops v1 en `GRAPH_OPS` (rms_norm, layer_norm, linear, add, mul,
|
|
467
473
|
scale, silu, gelu, gelu_tanh, relu, split, head_rms_norm, rope neox/interleaved
|
|
468
474
|
con `freq_factors`, `scaling` linear/yarn y `mscale`, attention con
|
|
@@ -501,7 +507,7 @@ console_level="warning")` con `log(level, event, message="", exc=None,
|
|
|
501
507
|
(por defecto un bucle; `native` usa `wsb_matmul`: decodifica cada
|
|
502
508
|
fila una vez a double y la aplica a grupos de 4 vectores con el mismo orden
|
|
503
509
|
de sumas que `wsb_matvec`; `MATMUL_RANGES_PER_THREAD` rangos de filas por
|
|
504
|
-
hilo). `GraphModel.load(path, accel=None)` o `$WSBUILDER_ACCEL`
|
|
510
|
+
hilo). `GraphModel.load(path, accel=None, fuse=True)` o `$WSBUILDER_ACCEL`
|
|
505
511
|
(`ACCEL_BACKENDS`: `auto`, `native`, `process`, `python`). `auto` prueba
|
|
506
512
|
`native` y cae a `python`; `process` (pool con `multiprocessing.shared_memory`)
|
|
507
513
|
solo si se pide, porque con spawn/forkserver los workers reimportan el script
|
|
@@ -428,8 +428,9 @@ rapido que un denso de su tamano.
|
|
|
428
428
|
|
|
429
429
|
### Ejecutar
|
|
430
430
|
|
|
431
|
-
`GraphModel.load(path, max_context=None, accel=None)` acepta
|
|
432
|
-
directorio; `accel` elige el backend de calculo (ver
|
|
431
|
+
`GraphModel.load(path, max_context=None, accel=None, fuse=True)` acepta
|
|
432
|
+
`model.json` o su directorio; `accel` elige el backend de calculo (ver
|
|
433
|
+
[Aceleracion](#aceleracion)) y `fuse` la fusion de matrices al cargar.
|
|
433
434
|
`max_context` (por defecto el menor de `config.context_length` y 4096) limita
|
|
434
435
|
prompt + respuesta; un prompt mas largo lanza `ValueError` y la generacion se
|
|
435
436
|
detiene con `finish_reason="length"` al llegar al limite.
|
|
@@ -555,6 +556,18 @@ model = GraphModel.load("qwen3-graph", accel=backend) # el llamador cierra back
|
|
|
555
556
|
vectores aleatorios y casos limite; la que no coincida se queda en Python
|
|
556
557
|
(aviso en `wsbuilder.logs`). `backend.vector_ops` lista las que van en C y
|
|
557
558
|
`NativeAccel(vector_ops=False)` las deja todas en Python.
|
|
559
|
+
- **Fusion de matrices**: al cargar, las ops `linear` seguidas que leen el
|
|
560
|
+
mismo valor (Q/K/V, gate/up) se apilan por filas en una sola matriz y corren
|
|
561
|
+
como un unico `matvec`/`matmul`: menos llamadas al backend y menos
|
|
562
|
+
sincronizaciones de hilos. La deteccion es generica (sin codigo por
|
|
563
|
+
arquitectura) y solo se fusionan ops con las mismas `layers`, matrices del
|
|
564
|
+
mismo dtype, codificacion y ancho, todas con bias o ninguna, y tensores que
|
|
565
|
+
ninguna otra op usa (embeddings atados y matrices compartidas quedan
|
|
566
|
+
aparte); una op que reescribe su entrada corta el grupo. Cada fila da los
|
|
567
|
+
mismos doubles que sola, asi que logits y cache KV son **identicos bit a
|
|
568
|
+
bit**; las partes no se guardan por separado (sin memoria extra).
|
|
569
|
+
`model.fused_linears` cuenta los grupos y `GraphModel.load(..., fuse=False)`
|
|
570
|
+
la desactiva.
|
|
558
571
|
- **Memoria**: `native` guarda cada matriz en un unico buffer con el dtype del
|
|
559
572
|
archivo (F32, F64, F16, BF16, Q8_0, Q4_0, Q4_1, Q5_0, Q5_1 o Q2_K a Q6_K)
|
|
560
573
|
y la multiplica directamente: Q4_K ocupa ~0,56 bytes por parametro, Q6_K
|
|
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
|
|
|
18
18
|
commit_id: str | None
|
|
19
19
|
__commit_id__: str | None
|
|
20
20
|
|
|
21
|
-
__version__ = version = '2.0.
|
|
22
|
-
__version_tuple__ = version_tuple = (2, 0,
|
|
21
|
+
__version__ = version = '2.0.2'
|
|
22
|
+
__version_tuple__ = version_tuple = (2, 0, 2)
|
|
23
23
|
|
|
24
|
-
__commit_id__ = commit_id = '
|
|
24
|
+
__commit_id__ = commit_id = 'gbaff36983'
|
|
@@ -482,6 +482,84 @@ def _integer(value, where, key):
|
|
|
482
482
|
return value
|
|
483
483
|
|
|
484
484
|
|
|
485
|
+
class _StackedTensor:
|
|
486
|
+
"""2-D tensors of one dtype and width stacked by rows, read like one tensor
|
|
487
|
+
(``raw_chunks``/``row_arrays``): the backend builds the stacked matrix in a
|
|
488
|
+
single buffer straight from the parts, which are never stored alone."""
|
|
489
|
+
|
|
490
|
+
def __init__(self, tensors):
|
|
491
|
+
self.tensors = tensors
|
|
492
|
+
self.dtype = tensors[0].dtype
|
|
493
|
+
self.encoding = getattr(tensors[0], "encoding", None)
|
|
494
|
+
self.shape = (sum(tensor.shape[0] for tensor in tensors), tensors[0].shape[1])
|
|
495
|
+
|
|
496
|
+
def raw_chunks(self):
|
|
497
|
+
# Every part must hold the bytes per row of the first one (the backend
|
|
498
|
+
# checks the total against the shape, which then fixes that size).
|
|
499
|
+
row_bytes = None
|
|
500
|
+
for tensor in self.tensors:
|
|
501
|
+
size = 0
|
|
502
|
+
for chunk in tensor.raw_chunks():
|
|
503
|
+
size += len(chunk)
|
|
504
|
+
yield chunk
|
|
505
|
+
if row_bytes is None and size % tensor.shape[0] == 0:
|
|
506
|
+
row_bytes = size // tensor.shape[0]
|
|
507
|
+
if size != (row_bytes or 0) * tensor.shape[0] or not size:
|
|
508
|
+
raise ValueError(f"tensor {_tensor_label(tensor)} has {size} bytes, which do "
|
|
509
|
+
f"not fit its {tensor.shape[0]} rows")
|
|
510
|
+
|
|
511
|
+
def row_arrays(self):
|
|
512
|
+
for tensor in self.tensors:
|
|
513
|
+
count = 0
|
|
514
|
+
for row in tensor.row_arrays():
|
|
515
|
+
count += 1
|
|
516
|
+
yield row
|
|
517
|
+
if count != tensor.shape[0]:
|
|
518
|
+
raise ValueError(f"tensor {_tensor_label(tensor)} has {count} rows, expected "
|
|
519
|
+
f"{tensor.shape[0]}")
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def _tensor_label(tensor):
|
|
523
|
+
name = getattr(tensor, "name", None)
|
|
524
|
+
return repr(name) if name is not None else f"of shape {list(tensor.shape)}"
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
def _tensor_uses(graph, embed_name):
|
|
528
|
+
"""How many times the graph names each tensor (``{i}`` and ``{e}`` expanded).
|
|
529
|
+
|
|
530
|
+
Only used to keep shared tensors out of fused ops: malformed parts are
|
|
531
|
+
skipped here and reported by the compiler.
|
|
532
|
+
"""
|
|
533
|
+
uses = {embed_name: 1}
|
|
534
|
+
|
|
535
|
+
def add(name):
|
|
536
|
+
uses[name] = uses.get(name, 0) + 1
|
|
537
|
+
|
|
538
|
+
def scan(ops, block):
|
|
539
|
+
for op in ops if isinstance(ops, list) else ():
|
|
540
|
+
if not isinstance(op, dict):
|
|
541
|
+
continue
|
|
542
|
+
layers = op.get("layers")
|
|
543
|
+
if block is not None and isinstance(layers, list) and block not in layers:
|
|
544
|
+
continue
|
|
545
|
+
names = [op.get(key) for key in OP_TENSOR_KEYS]
|
|
546
|
+
experts = op.get("experts")
|
|
547
|
+
if op.get("op") == "moe" and type(experts) is int and 0 < experts <= MAX_EXPERTS:
|
|
548
|
+
names += [op[key].replace("{e}", str(expert)) for key in MOE_EXPERT_KEYS
|
|
549
|
+
if isinstance(op.get(key), str) for expert in range(experts)]
|
|
550
|
+
for name in names:
|
|
551
|
+
if isinstance(name, str):
|
|
552
|
+
add(name if block is None else name.replace("{i}", str(block)))
|
|
553
|
+
|
|
554
|
+
blocks, head = graph.get("blocks"), graph.get("head")
|
|
555
|
+
if isinstance(blocks, dict) and type(blocks.get("count")) is int:
|
|
556
|
+
for block in range(blocks["count"]):
|
|
557
|
+
scan(blocks.get("ops"), block)
|
|
558
|
+
if isinstance(head, dict):
|
|
559
|
+
scan(head.get("ops"), None)
|
|
560
|
+
return uses
|
|
561
|
+
|
|
562
|
+
|
|
485
563
|
class _Compiler:
|
|
486
564
|
"""Turns graph ops into closures, checking names, attributes and shapes."""
|
|
487
565
|
|
|
@@ -490,6 +568,10 @@ class _Compiler:
|
|
|
490
568
|
self.attention_heads = [] # KV heads of each attention op, in order
|
|
491
569
|
self.attention_dims = [] # and their head_dim
|
|
492
570
|
self._ropes = {}
|
|
571
|
+
#: How many times the graph names each tensor; ``None`` disables the
|
|
572
|
+
#: fusion of linear ops (a tensor used twice is never fused).
|
|
573
|
+
self.tensor_uses = None
|
|
574
|
+
self.fused_linears = 0
|
|
493
575
|
|
|
494
576
|
def _vector_op(self, name, reference):
|
|
495
577
|
"""The backend's version of an element-wise op (see ``accel.VECTOR_OPS``),
|
|
@@ -570,6 +652,11 @@ class _Compiler:
|
|
|
570
652
|
raise GraphFormatError(f"{where} {kind}: 'layers' must list block numbers")
|
|
571
653
|
if block not in layers:
|
|
572
654
|
continue
|
|
655
|
+
group = self._linear_group(ops, index, sizes, block) if kind == "linear" else None
|
|
656
|
+
if group:
|
|
657
|
+
compiled.append(self._fused_linear(group, sizes, block))
|
|
658
|
+
fused.update(member for member, _op in group[1:])
|
|
659
|
+
continue
|
|
573
660
|
run = builder(self, op, f"{where} {kind}", sizes, block)
|
|
574
661
|
swiglu = self._swiglu(ops, index, sizes, location, block)
|
|
575
662
|
if swiglu is not None:
|
|
@@ -578,6 +665,103 @@ class _Compiler:
|
|
|
578
665
|
compiled.append(run)
|
|
579
666
|
return compiled
|
|
580
667
|
|
|
668
|
+
def _linear_group(self, ops, index, sizes, block):
|
|
669
|
+
"""Consecutive ``linear`` ops from ``index`` that can run as one product.
|
|
670
|
+
|
|
671
|
+
The pattern is generic (no architecture names): ops right after one
|
|
672
|
+
another that read the same input, with the same ``layers``, matrices
|
|
673
|
+
of the same dtype, encoding and width, all with a bias or none, each
|
|
674
|
+
tensor used by no other op of the graph (tied embeddings and shared
|
|
675
|
+
matrices stay apart), and distinct outputs; an op that writes its own
|
|
676
|
+
input ends the group. Returns ``[(index, op), ...]`` with two or more
|
|
677
|
+
ops, or ``None``; anything unusual (a missing tensor, a wrong shape)
|
|
678
|
+
is left to the plain op, which reports it.
|
|
679
|
+
"""
|
|
680
|
+
uses = self.tensor_uses
|
|
681
|
+
if uses is None:
|
|
682
|
+
return None
|
|
683
|
+
reader = self.weights.reader
|
|
684
|
+
first = ops[index]
|
|
685
|
+
source = first.get("in")
|
|
686
|
+
if not (isinstance(source, list) and len(source) == 1 and isinstance(source[0], str)
|
|
687
|
+
and source[0] in sizes):
|
|
688
|
+
return None
|
|
689
|
+
group, outs, kind = [], set(), None
|
|
690
|
+
for position in range(index, len(ops)):
|
|
691
|
+
op = ops[position]
|
|
692
|
+
if (not isinstance(op, dict) or op.get("op") != "linear" or op.get("in") != source
|
|
693
|
+
or op.get("layers") != first.get("layers")
|
|
694
|
+
or set(op) - _COMMON_KEYS - _OP_ATTRIBUTES["linear"]):
|
|
695
|
+
break
|
|
696
|
+
out, weight, bias = op.get("out"), op.get("weight"), op.get("bias")
|
|
697
|
+
if (not isinstance(out, str) or not out or out in outs
|
|
698
|
+
or not isinstance(weight, str) or not weight
|
|
699
|
+
or not (bias is None or isinstance(bias, str) and bias)):
|
|
700
|
+
break
|
|
701
|
+
weight = weight if block is None else weight.replace("{i}", str(block))
|
|
702
|
+
bias = bias if block is None or bias is None else bias.replace("{i}", str(block))
|
|
703
|
+
if weight not in reader or uses.get(weight) != 1 or (
|
|
704
|
+
bias is not None and (bias not in reader or uses.get(bias) != 1)):
|
|
705
|
+
break
|
|
706
|
+
tensor = reader.tensor(weight)
|
|
707
|
+
if len(tensor.shape) != 2 or tensor.shape[1] != sizes[source[0]]:
|
|
708
|
+
break
|
|
709
|
+
if bias is not None:
|
|
710
|
+
bias_tensor = reader.tensor(bias)
|
|
711
|
+
if tuple(bias_tensor.shape) != (tensor.shape[0],):
|
|
712
|
+
break
|
|
713
|
+
signature = (tensor.dtype, getattr(tensor, "encoding", None), tensor.shape[1],
|
|
714
|
+
bias is None)
|
|
715
|
+
if kind is None:
|
|
716
|
+
kind = signature
|
|
717
|
+
elif signature != kind:
|
|
718
|
+
break
|
|
719
|
+
group.append((position, op))
|
|
720
|
+
outs.add(out)
|
|
721
|
+
if out == source[0]:
|
|
722
|
+
break # later ops read the new value
|
|
723
|
+
return group if len(group) > 1 else None
|
|
724
|
+
|
|
725
|
+
def _fused_linear(self, group, sizes, block):
|
|
726
|
+
"""One product with the matrices of ``group`` stacked by rows: each row
|
|
727
|
+
gives the doubles it gives alone, and the output is cut back into the
|
|
728
|
+
values of the original ops."""
|
|
729
|
+
reader = self.weights.reader
|
|
730
|
+
source = group[0][1]["in"][0]
|
|
731
|
+
tensors, outs, biases = [], [], []
|
|
732
|
+
for _index, op in group:
|
|
733
|
+
name = self._tensor_name(op, "weight", "fused linear", block)
|
|
734
|
+
tensor = reader.tensor(name)
|
|
735
|
+
tensors.append(tensor)
|
|
736
|
+
bias_name = self._tensor_name(op, "bias", "fused linear", block, None)
|
|
737
|
+
if bias_name is not None:
|
|
738
|
+
biases += self.weights.vector(bias_name, "fused linear", tensor.shape[0])
|
|
739
|
+
outs.append(op["out"])
|
|
740
|
+
stacked = _StackedTensor(tensors)
|
|
741
|
+
matrix = self.weights.backend.prepare_matrix(stacked.shape[0], stacked.shape[1], stacked)
|
|
742
|
+
bias = self.weights.backend.prepare_vector(biases) if biases else None
|
|
743
|
+
bounds = []
|
|
744
|
+
start = 0
|
|
745
|
+
for out, tensor in zip(outs, tensors):
|
|
746
|
+
bounds.append((out, start, start + tensor.shape[0]))
|
|
747
|
+
sizes[out] = tensor.shape[0]
|
|
748
|
+
start += tensor.shape[0]
|
|
749
|
+
self.fused_linears += 1
|
|
750
|
+
matvec = self.weights.backend.matvec
|
|
751
|
+
matmul = self.weights.backend.matmul
|
|
752
|
+
|
|
753
|
+
def run(env, _ctx):
|
|
754
|
+
result = matvec(matrix, env[source], bias)
|
|
755
|
+
for out, begin, end in bounds:
|
|
756
|
+
env[out] = result[begin:end]
|
|
757
|
+
|
|
758
|
+
def run_batch(envs, _contexts):
|
|
759
|
+
for env, result in zip(envs, matmul(matrix, [env[source] for env in envs], bias)):
|
|
760
|
+
for out, begin, end in bounds:
|
|
761
|
+
env[out] = result[begin:end]
|
|
762
|
+
run.batch = run_batch
|
|
763
|
+
return run
|
|
764
|
+
|
|
581
765
|
def _swiglu(self, ops, index, sizes, location, block):
|
|
582
766
|
"""``silu`` then ``mul`` by another value, as one op when the backend has
|
|
583
767
|
``swiglu``; ``None`` when they do not fit the pattern.
|
|
@@ -1266,15 +1450,18 @@ class GraphModel:
|
|
|
1266
1450
|
(``"auto"``, ``"native"``, ``"process"`` or ``"python"``; default
|
|
1267
1451
|
``$WSBUILDER_ACCEL`` or ``"auto"``) or an
|
|
1268
1452
|
:class:`~wsbuilder.accel.AccelBackend`, which the caller then closes;
|
|
1269
|
-
``accel_name`` tells which one runs.
|
|
1453
|
+
``accel_name`` tells which one runs. ``fuse`` (default true) merges
|
|
1454
|
+
consecutive ``linear`` ops that read the same value into one product
|
|
1455
|
+
(see :meth:`_Compiler._linear_group`); the results are the same doubles.
|
|
1270
1456
|
"""
|
|
1271
1457
|
|
|
1272
1458
|
def __init__(self, spec, reader, tokenizer=None, *, max_context=None, path=None,
|
|
1273
|
-
accel=None):
|
|
1459
|
+
accel=None, fuse=True):
|
|
1274
1460
|
if isinstance(accel, AccelBackend):
|
|
1275
1461
|
self.accel, self._owns_accel = accel, False
|
|
1276
1462
|
else:
|
|
1277
1463
|
self.accel, self._owns_accel = create_backend(accel), True
|
|
1464
|
+
self._fuse = bool(fuse)
|
|
1278
1465
|
try:
|
|
1279
1466
|
self._setup(spec, reader, tokenizer, max_context, path)
|
|
1280
1467
|
except BaseException:
|
|
@@ -1314,14 +1501,15 @@ class GraphModel:
|
|
|
1314
1501
|
return self.accel.name
|
|
1315
1502
|
|
|
1316
1503
|
@classmethod
|
|
1317
|
-
def load(cls, path, *, max_context=None, accel=None):
|
|
1504
|
+
def load(cls, path, *, max_context=None, accel=None, fuse=True):
|
|
1318
1505
|
"""Load ``model.json`` (or the directory holding it), its weights and tokenizer,
|
|
1319
1506
|
or a ``.gguf`` file directly.
|
|
1320
1507
|
|
|
1321
1508
|
A GGUF file (recognised by its magic bytes) runs as it is: the graph
|
|
1322
1509
|
comes from its metadata (:func:`~wsbuilder.graph_convert.open_gguf`) and
|
|
1323
1510
|
the matrices are read from the file in their own dtype, without a
|
|
1324
|
-
conversion step. See the class docstring for ``max_context`` and
|
|
1511
|
+
conversion step. See the class docstring for ``max_context``, ``accel`` and
|
|
1512
|
+
``fuse``.
|
|
1325
1513
|
"""
|
|
1326
1514
|
from .ndjson_model import ModelReader
|
|
1327
1515
|
|
|
@@ -1334,7 +1522,7 @@ class GraphModel:
|
|
|
1334
1522
|
spec, tokenizer, weights = open_gguf(path)
|
|
1335
1523
|
try:
|
|
1336
1524
|
return cls(spec, weights, tokenizer, max_context=max_context, path=path,
|
|
1337
|
-
accel=accel)
|
|
1525
|
+
accel=accel, fuse=fuse)
|
|
1338
1526
|
except BaseException:
|
|
1339
1527
|
weights.close()
|
|
1340
1528
|
raise
|
|
@@ -1343,7 +1531,8 @@ class GraphModel:
|
|
|
1343
1531
|
tokenizer = _spec_tokenizer(spec, base_dir)
|
|
1344
1532
|
reader = ModelReader(_safe_path(base_dir, spec["weights"], "weights"))
|
|
1345
1533
|
try:
|
|
1346
|
-
return cls(spec, reader, tokenizer, max_context=max_context, path=path, accel=accel
|
|
1534
|
+
return cls(spec, reader, tokenizer, max_context=max_context, path=path, accel=accel,
|
|
1535
|
+
fuse=fuse)
|
|
1347
1536
|
except BaseException:
|
|
1348
1537
|
reader.close()
|
|
1349
1538
|
raise
|
|
@@ -1375,6 +1564,8 @@ class GraphModel:
|
|
|
1375
1564
|
f"graph.blocks.count is {count} but the weights hold only {tensor_total} "
|
|
1376
1565
|
f"tensors"
|
|
1377
1566
|
)
|
|
1567
|
+
if self._fuse:
|
|
1568
|
+
compiler.tensor_uses = _tensor_uses(graph, embed_name)
|
|
1378
1569
|
self._blocks = []
|
|
1379
1570
|
for block in range(count):
|
|
1380
1571
|
sizes = {"x": self.hidden_size}
|
|
@@ -1395,6 +1586,8 @@ class GraphModel:
|
|
|
1395
1586
|
self.logits_size = sizes["logits"]
|
|
1396
1587
|
self._attention_heads = list(compiler.attention_heads)
|
|
1397
1588
|
self._attention_dims = list(compiler.attention_dims)
|
|
1589
|
+
#: Number of fused linear groups (``fuse=True``); 0 when nothing matched.
|
|
1590
|
+
self.fused_linears = compiler.fused_linears
|
|
1398
1591
|
# Tied embeddings: the head already stored the matrix; reuse its rows.
|
|
1399
1592
|
self._embed_matrix = compiler.weights.matrices.get(embed_name)
|
|
1400
1593
|
self._embed_tensor = self.reader.tensor(embed_name)
|
|
@@ -19,6 +19,7 @@ from pathlib import Path
|
|
|
19
19
|
from unittest import mock
|
|
20
20
|
|
|
21
21
|
from test_graph import (
|
|
22
|
+
QWEN2,
|
|
22
23
|
QWEN3,
|
|
23
24
|
TOKENS,
|
|
24
25
|
graph_logits,
|
|
@@ -27,6 +28,7 @@ from test_graph import (
|
|
|
27
28
|
)
|
|
28
29
|
from test_quants import RUNTIME_TYPES, random_blocks
|
|
29
30
|
from wsbuilder import GraphModel, ModelReader, ModelWriter, accel, convert_to_graph
|
|
31
|
+
from wsbuilder.graph import _StackedTensor
|
|
30
32
|
from wsbuilder.weights import DEQUANTIZED_TYPES
|
|
31
33
|
|
|
32
34
|
ROOT = Path(__file__).resolve().parents[1]
|
|
@@ -588,8 +590,8 @@ class TestGraphModels(_Close):
|
|
|
588
590
|
q8_tensor="blk.0.ffn_down.weight")
|
|
589
591
|
self.q8 = convert_to_graph(gguf, os.path.join(self.directory, "q8"))
|
|
590
592
|
|
|
591
|
-
def load(self, path, accel_option):
|
|
592
|
-
model = GraphModel.load(path, accel=accel_option)
|
|
593
|
+
def load(self, path, accel_option, **options):
|
|
594
|
+
model = GraphModel.load(path, accel=accel_option, **options)
|
|
593
595
|
self.addCleanup(model.close)
|
|
594
596
|
return model
|
|
595
597
|
|
|
@@ -664,7 +666,9 @@ class TestGraphModels(_Close):
|
|
|
664
666
|
def check(fused):
|
|
665
667
|
with open(self.f32) as handle:
|
|
666
668
|
spec_ops = len(json.load(handle)["graph"]["blocks"]["ops"])
|
|
667
|
-
|
|
669
|
+
# Without the linear fusion, so that only silu/mul change the count.
|
|
670
|
+
model = self.load(self.f32, fast, fuse=False)
|
|
671
|
+
reference = self.load(self.f32, plain, fuse=False)
|
|
668
672
|
self.assertEqual(len(model._blocks[0]), spec_ops - fused)
|
|
669
673
|
self.assertEqual(len(reference._blocks[0]), spec_ops)
|
|
670
674
|
self.assertEqual(graph_logits(model, TOKENS), graph_logits(reference, TOKENS))
|
|
@@ -747,7 +751,8 @@ class TestGraphModels(_Close):
|
|
|
747
751
|
window=window):
|
|
748
752
|
model = self.load(self.f32, backend)
|
|
749
753
|
attention = [run for run in model._blocks[0] if hasattr(run, "batch")]
|
|
750
|
-
|
|
754
|
+
# q/k/v, o, gate/up and down (fused) + attention
|
|
755
|
+
self.assertEqual(len(attention), 5)
|
|
751
756
|
self.batched_equals_stepped(model, tokens)
|
|
752
757
|
|
|
753
758
|
@unittest.skipUnless(HAS_COMPILER, "no C compiler")
|
|
@@ -1339,5 +1344,208 @@ class TestNativeSimd(unittest.TestCase):
|
|
|
1339
1344
|
self.assertEqual(out[0], 8.0)
|
|
1340
1345
|
|
|
1341
1346
|
|
|
1347
|
+
class TestFusedLinear(_Close):
|
|
1348
|
+
"""Consecutive linear ops on one input run as one stacked product."""
|
|
1349
|
+
|
|
1350
|
+
def setUp(self):
|
|
1351
|
+
directory = tempfile.TemporaryDirectory()
|
|
1352
|
+
self.addCleanup(directory.cleanup)
|
|
1353
|
+
self.directory = directory.name
|
|
1354
|
+
|
|
1355
|
+
def convert(self, cfg, name, dtype=None):
|
|
1356
|
+
source = write_gguf(os.path.join(self.directory, f"{name}.gguf"), cfg,
|
|
1357
|
+
random_weights(cfg))
|
|
1358
|
+
return convert_to_graph(source, os.path.join(self.directory, name), dtype=dtype)
|
|
1359
|
+
|
|
1360
|
+
def backends(self):
|
|
1361
|
+
yield "python", "python"
|
|
1362
|
+
if HAS_COMPILER:
|
|
1363
|
+
native = accel.NativeAccel(threads=3)
|
|
1364
|
+
self.addCleanup(native.close)
|
|
1365
|
+
yield "native", native
|
|
1366
|
+
|
|
1367
|
+
def pair(self, path, backend):
|
|
1368
|
+
models = []
|
|
1369
|
+
for fuse in (True, False):
|
|
1370
|
+
model = GraphModel.load(path, accel=backend, fuse=fuse)
|
|
1371
|
+
self.addCleanup(model.close)
|
|
1372
|
+
models.append(model)
|
|
1373
|
+
self.assertEqual(models[1].fused_linears, 0)
|
|
1374
|
+
return models
|
|
1375
|
+
|
|
1376
|
+
def assert_same(self, fused, plain):
|
|
1377
|
+
"""Same logits and cache, token by token (matvec) and in batches (matmul)."""
|
|
1378
|
+
self.assertEqual(graph_logits(fused, TOKENS), graph_logits(plain, TOKENS))
|
|
1379
|
+
for batch in (32, 3):
|
|
1380
|
+
with mock.patch("wsbuilder.graph.PREFILL_BATCH", batch):
|
|
1381
|
+
caches = []
|
|
1382
|
+
for model in (fused, plain):
|
|
1383
|
+
cache = model.new_cache()
|
|
1384
|
+
model._prefill(TOKENS[:-1], cache)
|
|
1385
|
+
caches.append((model.forward(TOKENS[-1], cache.length, cache), cache))
|
|
1386
|
+
self.assertEqual(caches[0][0], caches[1][0])
|
|
1387
|
+
self.assertEqual(caches[0][1].slots, caches[1][1].slots)
|
|
1388
|
+
self.assertEqual(fused.generate(TOKENS[:4], max_tokens=5, temperature=0),
|
|
1389
|
+
plain.generate(TOKENS[:4], max_tokens=5, temperature=0))
|
|
1390
|
+
|
|
1391
|
+
def check(self, path, groups, merged):
|
|
1392
|
+
"""``groups`` stacked products in all, ``merged`` ops fewer per block."""
|
|
1393
|
+
for label, backend in self.backends():
|
|
1394
|
+
with self.subTest(model=os.path.basename(os.path.dirname(path)), accel=label):
|
|
1395
|
+
fused, plain = self.pair(path, backend)
|
|
1396
|
+
self.assertEqual(fused.fused_linears, groups)
|
|
1397
|
+
self.assertEqual([len(plain_ops) - len(ops) for ops, plain_ops
|
|
1398
|
+
in zip(fused._blocks, plain._blocks)], merged)
|
|
1399
|
+
self.assert_same(fused, plain)
|
|
1400
|
+
|
|
1401
|
+
def edit(self, path, change, part="blocks"):
|
|
1402
|
+
with open(path) as handle:
|
|
1403
|
+
spec = json.load(handle)
|
|
1404
|
+
change(spec["graph"][part]["ops"], spec)
|
|
1405
|
+
with open(path, "w") as handle:
|
|
1406
|
+
json.dump(spec, handle)
|
|
1407
|
+
|
|
1408
|
+
def test_qkv_and_gate_up_fuse_with_gqa_sizes(self):
|
|
1409
|
+
# q has 32 rows, k and v 16 (GQA): q/k/v and gate/up merge per block.
|
|
1410
|
+
for dtype in (None, "F16"):
|
|
1411
|
+
path = self.convert(QWEN3, f"qwen3-{dtype}", dtype)
|
|
1412
|
+
self.check(path, 4, [3, 3])
|
|
1413
|
+
|
|
1414
|
+
def test_biases_are_stacked_too(self):
|
|
1415
|
+
path = self.convert(QWEN2, "qwen2")
|
|
1416
|
+
with open(path) as handle:
|
|
1417
|
+
ops = json.load(handle)["graph"]["blocks"]["ops"]
|
|
1418
|
+
self.assertTrue(any(op.get("bias") for op in ops if op["op"] == "linear"))
|
|
1419
|
+
self.check(path, 4, [3, 3])
|
|
1420
|
+
|
|
1421
|
+
def test_the_default_is_on_and_can_be_turned_off(self):
|
|
1422
|
+
path = self.convert(QWEN3, "qwen3")
|
|
1423
|
+
model = GraphModel.load(path, accel="python")
|
|
1424
|
+
self.addCleanup(model.close)
|
|
1425
|
+
self.assertEqual(model.fused_linears, 4)
|
|
1426
|
+
model = GraphModel.load(path, accel="python", fuse=False)
|
|
1427
|
+
self.addCleanup(model.close)
|
|
1428
|
+
self.assertEqual(model.fused_linears, 0)
|
|
1429
|
+
|
|
1430
|
+
def test_process_backend_stacks_in_shared_memory(self):
|
|
1431
|
+
backend = accel.ProcessAccel(workers=2, min_macs=0)
|
|
1432
|
+
self.addCleanup(backend.close)
|
|
1433
|
+
path = self.convert(QWEN3, "qwen3")
|
|
1434
|
+
fused, plain = self.pair(path, backend)
|
|
1435
|
+
self.assertEqual(fused.fused_linears, 4)
|
|
1436
|
+
self.assert_same(fused, plain)
|
|
1437
|
+
reference = GraphModel.load(path, accel="python", fuse=False)
|
|
1438
|
+
self.addCleanup(reference.close)
|
|
1439
|
+
self.assertEqual(graph_logits(fused, TOKENS), graph_logits(reference, TOKENS))
|
|
1440
|
+
|
|
1441
|
+
def test_a_bias_on_some_ops_only_is_not_merged(self):
|
|
1442
|
+
path = self.convert(QWEN2, "qwen2")
|
|
1443
|
+
|
|
1444
|
+
def change(ops, _spec):
|
|
1445
|
+
del ops[2]["bias"] # q and v keep a bias, k has none: no q/k/v group
|
|
1446
|
+
self.edit(path, change)
|
|
1447
|
+
self.check(path, 2, [1, 1])
|
|
1448
|
+
|
|
1449
|
+
def test_a_part_with_the_wrong_size_is_refused(self):
|
|
1450
|
+
class Part:
|
|
1451
|
+
dtype, encoding = "F32", "raw"
|
|
1452
|
+
|
|
1453
|
+
def __init__(self, name, rows, data):
|
|
1454
|
+
self.name, self.shape, self.data = name, (rows, 2), data
|
|
1455
|
+
|
|
1456
|
+
def raw_chunks(self):
|
|
1457
|
+
yield self.data
|
|
1458
|
+
|
|
1459
|
+
def row_arrays(self):
|
|
1460
|
+
values = array.array("f", self.data)
|
|
1461
|
+
for start in range(0, len(values) - 1, 2):
|
|
1462
|
+
yield values[start:start + 2]
|
|
1463
|
+
|
|
1464
|
+
row = struct.pack("<2f", 1.0, 2.0)
|
|
1465
|
+
# 3 + 1 rows of bytes for parts of 2 + 2 rows: the total alone would fit.
|
|
1466
|
+
stacked = _StackedTensor([Part("a", 2, row * 3), Part("b", 2, row)])
|
|
1467
|
+
backends = [accel.PythonAccel() if backend == "python" else backend
|
|
1468
|
+
for _label, backend in self.backends()]
|
|
1469
|
+
for backend in backends:
|
|
1470
|
+
with self.subTest(accel=backend.name):
|
|
1471
|
+
with self.assertRaisesRegex(ValueError, "tensor '[ab]' has (3 rows|8 bytes)"):
|
|
1472
|
+
backend.prepare_matrix(4, 2, stacked)
|
|
1473
|
+
good = _StackedTensor([Part("a", 2, row * 2), Part("b", 1, row)])
|
|
1474
|
+
for backend in backends:
|
|
1475
|
+
with self.subTest(accel=backend.name, parts="good"):
|
|
1476
|
+
self.assertEqual(backend.prepare_matrix(3, 2, good).row(2), [1.0, 2.0])
|
|
1477
|
+
|
|
1478
|
+
def test_an_op_between_stops_the_group(self):
|
|
1479
|
+
path = self.convert(QWEN3, "qwen3")
|
|
1480
|
+
|
|
1481
|
+
def change(ops, _spec):
|
|
1482
|
+
# Rewrite h between q and k: k and v read the new value.
|
|
1483
|
+
ops.insert(2, {"op": "scale", "in": ["h"], "out": "h", "value": 0.5})
|
|
1484
|
+
self.edit(path, change)
|
|
1485
|
+
self.check(path, 4, [2, 2])
|
|
1486
|
+
|
|
1487
|
+
def test_a_linear_that_writes_its_input_ends_the_group(self):
|
|
1488
|
+
path = self.convert(QWEN3, "qwen3")
|
|
1489
|
+
|
|
1490
|
+
def change(ops, _spec):
|
|
1491
|
+
# q, then v writing h (16 x 16), then k reading the new h.
|
|
1492
|
+
q, k, v = ops[1:4]
|
|
1493
|
+
v["out"] = "h"
|
|
1494
|
+
ops[1:4] = [q, v, k]
|
|
1495
|
+
for op in ops:
|
|
1496
|
+
if op["op"] == "attention":
|
|
1497
|
+
op["in"] = ["q", "k", "h"]
|
|
1498
|
+
self.edit(path, change)
|
|
1499
|
+
self.check(path, 4, [2, 2])
|
|
1500
|
+
|
|
1501
|
+
def test_different_layers_are_not_merged(self):
|
|
1502
|
+
path = self.convert(QWEN3, "qwen3")
|
|
1503
|
+
|
|
1504
|
+
def change(ops, _spec):
|
|
1505
|
+
ops[3]["layers"] = [0, 1] # v: every block, but not the same 'layers'
|
|
1506
|
+
self.edit(path, change)
|
|
1507
|
+
self.check(path, 4, [2, 2])
|
|
1508
|
+
|
|
1509
|
+
def test_different_dtypes_are_not_merged(self):
|
|
1510
|
+
path = self.convert(QWEN3, "qwen3")
|
|
1511
|
+
mixed = os.path.join(os.path.dirname(path), "mixed.ndjson")
|
|
1512
|
+
with ModelReader(os.path.join(os.path.dirname(path), "weights.ndjson")) as reader:
|
|
1513
|
+
writer = ModelWriter(architecture=reader.architecture, name="mixed")
|
|
1514
|
+
for name in reader.names():
|
|
1515
|
+
tensor = reader.tensor(name)
|
|
1516
|
+
dtype = "F16" if name == "blk.0.attn_k.weight" else tensor.dtype
|
|
1517
|
+
writer.add_tensor(name, list(tensor.to_array()), shape=tensor.shape,
|
|
1518
|
+
dtype=dtype)
|
|
1519
|
+
writer.write(mixed)
|
|
1520
|
+
|
|
1521
|
+
def change(_ops, spec):
|
|
1522
|
+
spec["weights"] = "mixed.ndjson"
|
|
1523
|
+
self.edit(path, change)
|
|
1524
|
+
# Block 0: q (F32), k (F16), v (F32) stay apart; block 1 merges q/k/v.
|
|
1525
|
+
self.check(path, 3, [1, 3])
|
|
1526
|
+
|
|
1527
|
+
def test_shared_matrices_are_not_merged(self):
|
|
1528
|
+
path = self.convert(QWEN3, "qwen3")
|
|
1529
|
+
|
|
1530
|
+
def change(ops, _spec):
|
|
1531
|
+
ops[3]["weight"] = "blk.{i}.attn_k.weight" # k and v use one matrix
|
|
1532
|
+
self.edit(path, change)
|
|
1533
|
+
self.check(path, 2, [1, 1])
|
|
1534
|
+
|
|
1535
|
+
def test_the_embedding_matrix_is_never_merged(self):
|
|
1536
|
+
path = self.convert(QWEN3, "qwen3")
|
|
1537
|
+
|
|
1538
|
+
def change(ops, _spec):
|
|
1539
|
+
ops.insert(1, {"op": "linear", "in": ["x"], "out": "aux",
|
|
1540
|
+
"weight": "token_embd.weight"})
|
|
1541
|
+
self.edit(path, change, part="head")
|
|
1542
|
+
for label, backend in self.backends():
|
|
1543
|
+
with self.subTest(accel=label):
|
|
1544
|
+
fused, plain = self.pair(path, backend)
|
|
1545
|
+
self.assertEqual(fused.fused_linears, 4)
|
|
1546
|
+
self.assertEqual(len(fused._head), len(plain._head))
|
|
1547
|
+
self.assert_same(fused, plain)
|
|
1548
|
+
|
|
1549
|
+
|
|
1342
1550
|
if __name__ == "__main__":
|
|
1343
1551
|
unittest.main()
|