Files
jobtrackingapp/docs/redesign/phase-9/p9-sec-02/model/probe.py
T
cesnimda 30e251a593
MariaDB persistence qualification / mariadb (push) Has been cancelled
fix: qualify patched Python model and cache dependencies
2026-10-25 08:18:21 +01:00

58 lines
3.2 KiB
Python

"""Actual offline CPU inference; synthetic weights test compatibility, never quality."""
from pathlib import Path
import importlib.metadata
import json
import os
import resource
import sys
import time
from verify import verify_fixture
HERE = Path(__file__).resolve().parent
manifests = json.loads((HERE / 'ARTIFACTS.json').read_text())
kind = sys.argv[1]
fixture = verify_fixture(Path(sys.argv[2]), manifests[kind])
assert os.environ['HF_HUB_OFFLINE'] == os.environ['TRANSFORMERS_OFFLINE'] == '1'
os.environ.pop('AI_SERVICE_SKIP_MODEL_LOAD', None)
os.environ.pop('AI_SERVICE_EAGER_MODEL_LOAD', None)
os.environ['AI_SERVICE_TOKEN'] = 'p9-local-qualification-only'
sys.path.insert(0, '/app')
import torch
import app
from fastapi.testclient import TestClient
torch.set_num_threads(1)
started = time.monotonic()
# Only this isolated qualification process selects synthetic weights; no inference is mocked.
app.MODEL_NAME = str(fixture)
assert app._ensure_runtime_loaded(), app.MODEL_LOAD_ERROR
assert app.model.__class__.__name__ == 'BartForConditionalGeneration'
assert app.model.training is False
assert str(app.device) == 'cpu' and not app.GPU_AVAILABLE
text = 'Åse Østby builds accessible Python and C# applications for reliable Norwegian career workflows.'
encoded = app.tokenizer(text, return_tensors='pt')
assert app.tokenizer.decode(encoded.input_ids[0], skip_special_tokens=True) == text
summary = app._model_summarize(text, 24, 8)
assert isinstance(summary, str) and summary
long_input = app.tokenizer(text * 400, return_tensors='pt', truncation=True, max_length=1024)
assert long_input.input_ids.shape == (1, 1024)
assert long_input.input_ids.dtype == torch.int64
with torch.no_grad():
logits = app.model(**long_input, decoder_input_ids=torch.tensor([[2, 0, 10]])).logits
assert torch.isfinite(logits).all()
assert app._model_summarize(text * 400, 24, 8)
client = TestClient(app.app)
assert client.get('/health').json()['model_loaded'] is True
assert client.post('/summarize', json={'text': text}).status_code == 401
headers = {'X-Ai-Service-Token': 'p9-local-qualification-only'}
body = {'text': text, 'max_length': 24, 'min_length': 8}
first = client.post('/summarize', json=body, headers=headers)
second = client.post('/summarize', json=body, headers=headers)
assert first.status_code == second.status_code == 200, first.text
assert first.json()['cached'] is False and second.json()['cached'] is True
assert first.json()['summary'] == second.json()['summary']
verify_fixture(fixture, manifests[kind])
elapsed = time.monotonic() - started
assert elapsed < 60
print(json.dumps({'status': 'PASS', 'kind': kind, 'scope': 'Real synthetic BART/tokenizer/native CPU generation plus sidecar summary/auth/cache contract; no full DistilBART or semantic-quality claim.', 'modelClass': app.model.__class__.__name__, 'device': str(app.device), 'cudaAvailable': torch.cuda.is_available(), 'seconds': elapsed, 'maxRssKiB': resource.getrusage(resource.RUSAGE_SELF).ru_maxrss, 'versions': {name: importlib.metadata.version(name) for name in ['torch','transformers','filelock','huggingface-hub','tokenizers','safetensors']}, 'summary': summary, 'bounded1024Tokens': True, 'finiteLogits': True, 'cacheRepeatedRead': True, 'fixtureUnchanged': True}), flush=True)