import copy
import json
import re
import qwen_n4_peer
from family_peer import SHARED_KEYS, same
from qwen38_n1_verdict import integer, require
ENGINE = 'tensorfold'
FAMILY = 'qwen'
FAMILIES = (FAMILY,)
TITLE = 'Qwen3.8'
Q3_FAMILIES = ('qwen38',)
SLOTS = 4
CONTEXT_PER_SLOT = 262144
MAX_TOKENS = 8192
TF_COMMIT = '1157ea5b0f636469aa550aab8d8da7b22083dcd9'
TF_VERSION = '1.2.0' TARGET_REPO = 'TensorFold/Qwen3.8-27B-MLX-4bit'
TARGET_REVISION = '22d8d538154e0e5b6f8dcb1fcc74a60a0798104c'
DRAFTER_REPO = 'z-lab/Qwen3.8-27B-DFlash2'
DRAFTER_REVISION = '50307d4c4cde6860d4eee73e2547cd786fe8e8a4'
DRAFTER_BITS = 4 MODEL_ID = 'qwen3.8-27b' TEMPLATE_SHA256 = 'c3cf9e34abf4f9e36c2d72165aa9c132d3e2a725b6c2586aaa3a8af9d7a81041'
EFFORT = 'xhigh'
BUDGET_USER, BUDGET_TOOL = 2048, 512
LABELS = {'none': f'TensorFold {TF_COMMIT[:7]} (native Qwen3.8-27B MLX 4-bit, no drafter)',
'dflash2': f'TensorFold {TF_COMMIT[:7]} + DFlash2 (native Qwen3.8-27B MLX 4-bit)'}
CANONICAL_KEYS = SHARED_KEYS NATIVE = {'temperature': 0, 'top_p': 1.0, 'top_k': 0, 'min_p': 0.0, 'reasoning_effort': EFFORT,
'chat_template_kwargs': {'enable_thinking': True}}
SAMPLING = {'temperature': 0, 'top_k': 0, 'top_p': 1.0, 'min_p': 0.0,
'repetition_penalty': 'Unavailable:TensorFold has no repetition penalty (hf2q Qwen: 1.05 over 64)'}
_CONTROLS = {'engine': ENGINE, 'source_commit': TF_COMMIT, 'version': TF_VERSION,
'target': f'{TARGET_REPO}@{TARGET_REVISION}', 'chat_template_sha256': TEMPLATE_SHA256,
'cache_family': FAMILY, 'context_per_slot': CONTEXT_PER_SLOT, 'slots': SLOTS, 'parallel': 'fixed',
'kv_cache': 'bf16', 'prompt_cache': 'auto', 'slot_resume': True, 'keep_warm_seconds': 900,
'idle_unload_seconds': 0, 'learn': False, 'recall': False, 'loop_guard': False,
'default_max_tokens': MAX_TOKENS, 'server_temperature': 0, 'repetition_penalty': 'Unavailable',
'request_temperature': 0, 'request_top_p': 1.0, 'request_top_k': 0, 'request_min_p': 0.0,
'request_reasoning_effort': EFFORT, 'request_enable_thinking': True,
'request_thinking_budget_user': BUDGET_USER, 'request_thinking_budget_tool': BUDGET_TOOL,
'request_parallel_tool_calls': 'omitted'}
CONTROLS = {'none': {**_CONTROLS, 'drafter': None, 'drafter_bits': None},
'dflash2': {**_CONTROLS, 'drafter': f'{DRAFTER_REPO}@{DRAFTER_REVISION}', 'drafter_bits': DRAFTER_BITS}}
NATIVE_KEYS = frozenset({'tensorfold_source_binary_bound', 'tensorfold_target_snapshot_bound',
'tensorfold_drafter_state_bound', 'tensorfold_controls_bound',
'tensorfold_tokenize_render_audit_bound', 'tensorfold_slots_context_readiness_verified'})
FINISH = ('stop', 'tool_calls', 'length')
CALL_ID = re.compile(r'call_[0-9a-f]{24}')
TOP = frozenset({'id', 'object', 'created', 'model', 'choices', 'usage', 'exact_mode', 'tensorfold', 'speculative'})
CHUNK = frozenset({'id', 'object', 'created', 'model', 'choices'})
EXTRAS = frozenset({'exact_mode', 'tensorfold', 'speculative'})
RUNTIME = frozenset({'enable_thinking', 'reasoning_effort', 'engine', 'tokens_per_second', 'seconds',
'prefill_seconds', 'prefill_widths', 'prefill_raised', 'time_to_first_token', 'sampling',
'drafts', 'token_sha', 'token_sha256', 'min_rows'})
RUNTIME_OPTIONAL = frozenset({'gpu_seconds', 'loop'}) SPECULATIVE = frozenset({'rounds', 'drafted', 'accepted', 'acceptance_rate', 'tokens_per_round'})
SAME_GGUF_UNAVAILABLE = None NATIVE_COMPARISON = ('native-comparison campaign; not the verdict of record: the Qwen3.8 same-GGUF peer '
'verdict comes from the campaign of record, and the TensorFold arm is reported '
'separately as native_peer (ADR-063 Q4)')
CLIENT_MODULES = ('native_peers.py', 'native_peer_tensorfold.py', 'native_peer_tensorfold_ready.py',
'test_native_peer_tensorfold.py')
PROCESSES = ('tensorfold', 'tensorfold-native') BASELINES = {'tensorfold_native': LABELS['none'], 'tensorfold_dflash2_native': LABELS['dflash2']}
AUDIT = 'native_peer_tensorfold_audit'
PEER_CASE_REFUSAL = ('a native TensorFold --peer-case needs --native-comparison (Qwen3.8 has a runnable same-GGUF '
'peer, ADR-063 Q4)')
def _family(family):
require(family == FAMILY, 'the native TensorFold arm serves Qwen3.8 only')
def variant(case):
return 'dflash2' if '--drafter' in case['serve']['argv'] else 'none'
def label(case):
return LABELS[variant(case)]
def q3_role(case):
return (f'{label(case)}: separately labeled different-artifact native comparison, task makespan only; '
'not the same-GGUF peer verdict (ADR-063 Q4)')
def preflight(keys, family):
_family(family)
from coding_family import preflight as family_preflight
return (set(keys) - family_preflight(set(), family) - qwen_n4_peer.HF_ONLY) | NATIVE_KEYS
def controls_variant(controls):
found = [name for name, expected in CONTROLS.items() if same(controls, expected)]
require(len(found) == 1, 'TensorFold launch controls differ from the declared native settings')
return found[0]
def validate_controls(controls, family):
_family(family)
controls_variant(controls)
def thinking_budget(messages):
require(isinstance(messages, list) and bool(messages), 'TensorFold message chain required')
latest = next((m for m in reversed(messages) if not (isinstance(m, dict) and m.get('role') == 'system')), None)
return BUDGET_TOOL if isinstance(latest, dict) and latest.get('role') == 'tool' else BUDGET_USER
def request_controls(request, family):
_family(family)
extra = sorted(set(request) - CANONICAL_KEYS)
require(not extra, 'unsupported TensorFold override: ' + ','.join(extra))
require(set(request) == CANONICAL_KEYS, 'incomplete canonical TensorFold request')
require(request['model'] == MODEL_ID, 'TensorFold model id must be ' + MODEL_ID)
require(type(request['max_completion_tokens']) is int and request['max_completion_tokens'] == MAX_TOKENS,
'canonical output allowance required')
require(request['tool_choice'] == 'auto' and request['logprobs'] is False
and type(request['stream']) is bool, 'canonical tool/transport controls required')
budget = qwen_n4_peer.request_controls(request)['reasoning_budget_tokens'] require(budget == thinking_budget(request['messages']), 'Qwen thinking ceiling rule drift')
native = {k: v for k, v in request.items() if k != 'hf2q_overflow_policy'}
return {**native, 'tools': native_tools(request['tools']), **copy.deepcopy(NATIVE), 'thinking_budget': budget}
def _sorted(value):
if isinstance(value, dict):
return {key: _sorted(value[key]) for key in sorted(value)}
return [_sorted(item) for item in value] if isinstance(value, list) else value
def native_tools(tools):
require(isinstance(tools, list) and all(isinstance(t, dict) and isinstance(t.get('function'), dict)
and t['function'].get('strict') is False for t in tools),
'canonical tool schemas carry strict false')
return [_sorted({**t, 'function': {k: v for k, v in t['function'].items() if k != 'strict'}}) for t in tools]
def canonical_tools(tools):
require(isinstance(tools, list) and all(isinstance(t, dict) and isinstance(t.get('function'), dict)
and 'strict' not in t['function']
and json.dumps(t) == json.dumps(_sorted(t)) for t in tools),
'TensorFold tool schemas must be the sorted, strict-free hf2q render')
return [{**t, 'function': {**t['function'], 'strict': False}} for t in tools]
def canonical(request, family):
_family(family)
require('hf2q_overflow_policy' not in request, 'TensorFold request must use native transport fields')
for key, value in NATIVE.items():
require(key in request and same(request[key], value), 'TensorFold native control drift: ' + key)
require(type(request.get('thinking_budget')) is int and request['thinking_budget'] == thinking_budget(
request.get('messages')), 'TensorFold thinking budget drift')
stripped = {k: v for k, v in request.items() if k not in NATIVE and k != 'thinking_budget'}
stream = stripped.get('stream') is True
require(set(stripped) - {'stream_options'} == CANONICAL_KEYS - {'hf2q_overflow_policy'}
and ('stream_options' in stripped) == stream
and (not stream or same(stripped['stream_options'], {'include_usage': True})),
'TensorFold request carries a field outside the closed envelope')
return {**stripped, 'tools': canonical_tools(stripped['tools']), 'hf2q_overflow_policy': 'reject'}
def compact_json(text):
inside = escaped = False
for ch in text:
if inside:
if escaped:
escaped = False
elif ch == '\\':
escaped = True
elif ch == '"':
inside = False
elif ch == '"':
inside = True
elif ch in ' \t\r\n':
return False
return not inside
def _tool_calls(calls, delta):
require(isinstance(calls, list) and bool(calls), 'TensorFold tool_calls must be a nonempty array')
for call in calls:
function = call.get('function') if isinstance(call, dict) else None
require(isinstance(function, dict), 'TensorFold tool-call shape')
if not delta:
require(set(call) == {'id', 'type', 'function'} and set(function) == {'name', 'arguments'},
'unknown TensorFold tool-call field')
elif 'id' in call:
require(set(call) == {'index', 'id', 'type', 'function'} and set(function) == {'name', 'arguments'}
and function['arguments'] == '', 'TensorFold streamed tool-call head')
else:
require(set(call) == {'index', 'function'} and set(function) == {'arguments'},
'TensorFold streamed tool-call arguments')
require(not delta or type(call['index']) is int, 'TensorFold tool-call index')
require(all(isinstance(v, str) for v in function.values()), 'TensorFold tool-call fields must be strings')
require('id' not in call or (call['type'] == 'function' and CALL_ID.fullmatch(call['id']) is not None),
'TensorFold tool-call id is not call_<24 hex>')
require(delta or compact_json(function['arguments']), 'TensorFold tool-call arguments are not compact JSON')
def _finish(value, allow_none):
require((allow_none and value is None) or value in FINISH,
f'TensorFold finish_reason {value!r} (cancelled/content_filter/unknown refuse)')
def _extras(obj):
require(obj.get('exact_mode') == 'exact' and isinstance(obj.get('stop_sequence', ''), str),
'TensorFold exact_mode/stop_sequence extras')
runtime = obj.get('tensorfold')
require(isinstance(runtime, dict) and RUNTIME <= set(runtime) <= RUNTIME | RUNTIME_OPTIONAL,
'TensorFold runtime extras envelope')
require(runtime['enable_thinking'] is True and runtime['reasoning_effort'] == EFFORT,
'TensorFold request did not render thinking at effort xhigh')
require(runtime['engine'] == 'qwen27' and runtime['sampling'] == 'greedy' and type(runtime['drafts']) is bool,
'TensorFold engine/greedy/draft runtime facts')
require(isinstance(runtime['token_sha256'], str) and re.fullmatch(r'[0-9a-f]{64}', runtime['token_sha256'])
and runtime['token_sha'] == runtime['token_sha256'][:12], 'TensorFold reply token sha')
speculative = obj.get('speculative')
require(isinstance(speculative, dict) and SPECULATIVE <= set(speculative) <= SPECULATIVE | {'proposer'},
'TensorFold speculative extras envelope')
for key in ('rounds', 'drafted', 'accepted'):
integer(speculative[key], 'TensorFold speculative ' + key)
require(speculative['accepted'] <= speculative['drafted'], 'TensorFold accepted more drafts than it drafted')
def check_unary(obj):
require(isinstance(obj, dict) and TOP <= set(obj) <= TOP | {'stop_sequence'}
and obj['object'] == 'chat.completion' and type(obj['created']) is int, 'TensorFold unary envelope')
choices = obj['choices']
require(isinstance(choices, list) and len(choices) == 1 and isinstance(choices[0], dict)
and set(choices[0]) == {'index', 'message', 'finish_reason'}, 'TensorFold unary choice')
message = choices[0]['message']
require(isinstance(message, dict) and {'role', 'content'} <= set(message)
<= {'role', 'content', 'reasoning_content', 'tool_calls'}, 'TensorFold unary message fields')
calls = message.get('tool_calls')
require(isinstance(message['content'], str) or (message['content'] is None and calls is not None),
'TensorFold content is null only beside tool calls')
require('reasoning_content' not in message or (isinstance(message['reasoning_content'], str)
and bool(message['reasoning_content'])),
'TensorFold reasoning_content')
if calls is not None:
_tool_calls(calls, False)
_finish(choices[0]['finish_reason'], False)
_extras(obj)
def check_frame(obj):
require(isinstance(obj, dict) and obj.get('object') == 'chat.completion.chunk'
and type(obj.get('created')) is int, 'TensorFold SSE envelope')
if obj.get('choices') == []:
require(set(obj) == CHUNK | {'usage'}, 'TensorFold SSE usage frame')
return
choices = obj.get('choices')
require(isinstance(choices, list) and len(choices) == 1 and isinstance(choices[0], dict)
and set(choices[0]) == {'index', 'delta', 'finish_reason'}, 'TensorFold SSE choice')
delta, finish = choices[0]['delta'], choices[0]['finish_reason']
require(isinstance(delta, dict) and set(delta) <= {'role', 'content', 'reasoning_content', 'tool_calls'},
'unknown TensorFold delta field')
_finish(finish, True)
if finish is None:
require(set(obj) == CHUNK, 'TensorFold SSE chunk fields')
else: require(delta == {} and CHUNK | EXTRAS <= set(obj) <= CHUNK | EXTRAS | {'stop_sequence'},
'TensorFold SSE final chunk')
_extras(obj)
if 'tool_calls' in delta:
_tool_calls(delta['tool_calls'], True)
def usage(value, turn):
require(isinstance(value, dict) and set(value) == {'prompt_tokens', 'completion_tokens', 'total_tokens',
'prompt_tokens_details', 'completion_tokens_details'},
'TensorFold usage envelope')
prompt = integer(value['prompt_tokens'], 'TensorFold prompt tokens', CONTEXT_PER_SLOT)
completion = integer(value['completion_tokens'], 'TensorFold completion tokens', MAX_TOKENS)
require(integer(value['total_tokens'], 'TensorFold total tokens') == prompt + completion,
'TensorFold usage total contradiction')
require(prompt + MAX_TOKENS <= CONTEXT_PER_SLOT,
'TensorFold prompt plus full output exceeds the 262144 workload context')
details, thought = value['prompt_tokens_details'], value['completion_tokens_details']
require(isinstance(details, dict) and set(details) == {'cached_tokens'}, 'TensorFold cache details envelope')
require(isinstance(thought, dict) and set(thought) == {'reasoning_tokens'}, 'TensorFold reasoning details envelope')
cached = integer(details['cached_tokens'], 'TensorFold cached tokens', prompt)
reasoning = integer(thought['reasoning_tokens'], 'TensorFold reasoning tokens', completion)
return {**value, 'engine_kind': ENGINE, 'cache_observation': {
'status': 'Observed', 'cached_tokens': cached, 'source_field': 'usage.prompt_tokens_details.cached_tokens',
'semantics': 'prefix-restored tokens (chat.zig:569); a background warm prefill of the next turn '
'(warm.zig) can contribute', 'full_prefix_reuse': 'PendingPostOracle'},
'reasoning_observation': {'status': 'Observed', 'reasoning_tokens': reasoning,
'source_field': 'usage.completion_tokens_details.reasoning_tokens'}}
def readiness(base, model, output, packet):
from native_peer_tensorfold_ready import readiness as ready
return ready(base, model, output, packet)
def closing(base, output):
from native_peer_tensorfold_ready import closing as close
return close(base, output)
def campaign_identity(plan):
from native_peer_tensorfold_ready import campaign_identity as identity
return identity(plan)
def launch_problems(name, artifact, hf2q_sha256):
from native_peer_tensorfold_ready import launch_problems as problems
return problems(name, artifact, hf2q_sha256)