Author SHA1 Message Date
nghianguyenandClaude Opus 5.5 c1c26c400b fix(ANSModelPackage): seal model packages with CSPRNG keys and nonces, not a seed
pack() derived the content key, the ephemeral X25519 private key and every
nonce base from an 8-bit nonceSeed. Anyone holding a package could try the
256 seeds and recover its content key without the base private key.

The content key, ephemeral key, wrap nonce and nonce bases now come from
the OS CSPRNG (BCryptGenRandom / getrandom / arc4random_buf). Nonce bases
are redrawn until distinct within a package. The seeded path survives only
as insecureDeterministicForTests + testSeed, honoured only when built with
ANSMODEL_DETERMINISTIC_PACK_FOR_TESTS; otherwise pack() refuses. The
on-disk format is unchanged. Same change in ANSTSCORE, ANSMINDCORE and
ANSCORE, which keep pack() in step.

Regression test: two packs of identical inputs have different content
keys, ephemeral keys, wrap nonces and ciphertext, neither key is one a
seed could make, and both still open with the base key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 18:15:13 +10:00
nghianguyenandClaude Opus 5 cb964907f0 feat(ANSLS): know ANSMIND as 1009, and stop filing unknown products under it
The gateway's product table already runs to 1009 ANSMIND and 1010 ANSGW.
The licensing library stopped at 1008, so a real ANSMIND licence could not
be named, deactivated or listed here, while every product name it did not
recognise quietly became product 1009. With 1009 now a real product, that
fallback would mint an ANSMIND trial for a typo.

ANSPRODUCTS gains ANSMIND = 1009 and ANSGW = 1010, and both are added
wherever the product table is spelled out: ActivateTrialLicense,
DeactivateLicense, ActivateLicense, ActivateLicenseWithCustomHWID and
ListLicenses, which now reports an ANSMIND and an ANSGW row like the
gateway does.

Unknown names no longer map to any id. ActivateTrialLicense returns 0, its
existing failure value, instead of generating a key. ListLicenses skips
the row. ActivateLicense and ActivateLicenseWithCustomHWID keep passing
the name through as the registration name: they never assigned an id, the
key's own validation data rejects a name it was not issued for, and the
gateway resolves an unknown product the same way.

The ANSVIS master-licence rule in LicenseVerification is untouched.

Separately, the fallback loops in ANSUtilities, ANSAWSS3 and ANSLLM asked
for product 1008 under the registration name "ANSSRT". The product is
ANSTS, so a genuine ANSTS licence could never satisfy them. Only that
string changes; the loops still stop at 1008.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 10:38:31 +10:00
nghianguyenandClaude Opus 5 cd99fcf6d9 feat(ANSODEngine): run a sealed model on TensorRT without unsealing it
A .ansmodel reaching the GPU path went to ModelOptimizer, which knows one
way to open a model — unzip it — and failed on the first byte. Even past
that, the TensorRT engine only ever parsed an ONNX file, so the one thing
the format exists to prevent, a decrypted model on disk, was the only way
it could have run.

The builder now takes its bytes from memory when it has them. The path it
is given still names the plan that gets cached beside it, because a plan
is a built artefact and carries no model: the second run loads it and
never needs the ONNX at all. For a package the plan lands in the folder
the zip flow used, so a model converted from a zip reuses the plan that
zip already built.

OptimizeModelStr recognises a package and skips the unzip-and-optimise
leg entirely: the handle below it opens the package in memory and builds
the plan from the decrypted bytes, which is the same work by a shorter
road.

Still on the file path: the other TensorRT handlers (segmentation, pose,
classification). Only the YOLO detector reads a package today.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 09:38:45 +10:00
nghianguyenandClaude Opus 5 1e369fdfef test(ANSODEngine): let the harness run a model this machine actually has
Every test in that file names a model and a video from whichever
workstation it was written on, so anywhere else the harness prints
"Model zip file is not exist" and exits before it reaches the GPU. That
is what happened on the machine that is meant to be testing CUDA and
TensorRT.

Given a model and a video on the command line it runs those instead,
with an optional engine type and detection type. With no arguments it
behaves exactly as before, so nobody's workflow changes.

The model may be a .zip or a sealed .ansmodel, because the engine now
opens either — and that is the only way to watch a sealed model run on
a GPU, which is the last thing in this format that has never been seen
working end to end.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 08:28:50 +10:00
nghianguyenandClaude Opus 5 b69a0fbcba fix(build): find nlohmann/json where the machine actually keeps it
The package library's Windows branch pointed at vcpkg alone. On a real
Windows build machine the header lives in the ANSLibs tree, so anything
linking this library failed with "Cannot open include file:
'nlohmann/json.hpp'" while the engines compiled fine — they add ANSLibs
to their own include path and never noticed.

Now it is searched for by name, with the ANSLibs tree, vcpkg and the
usual system paths as hints, and the configure says where it found it.
One code path for every platform instead of an assumption per platform.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 08:00:51 +10:00
nghianguyenandClaude Opus 5 e76d3b81e2 fix(cuda): spell the fast-math flag the way nvcc spells it
Every .cu file failed on CUDA 13 with "nvcc fatal: Unknown option
'--use-fast-math'". The flag is --use_fast_math, with underscores; older
toolkits accepted the hyphenated form as well and this one does not, so
the engine layer could not compile a single kernel on a current CUDA.

Found on the Windows machine with CUDA 13.0, where it stopped the build
at the first two kernels.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 07:58:49 +10:00
nghianguyenandClaude Opus 5 eb84d48e5c fix(build): do not demand a policy older CMake has never heard of
Line two set CMP0167 unconditionally. That policy arrived in CMake 3.30
and this project asks for 3.28, so every CMake between the two failed
the configure at line two with "Policy CMP0167 is not known to this
version" — before it had done anything at all. Found on the Windows
machine that builds the CUDA engines, where it stopped the build dead.

Guarded, so a newer CMake still gets the quiet FindBoost the policy was
set for and an older one configures instead of refusing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 07:46:43 +10:00
nghianguyenandClaude Opus 5 3798e3513b fix(YOLO12OD): decode an end-to-end head instead of reading it as a grid
This engine took the feature count from outputShape[1] unconditionally.
For a yolo26 export with end2end on — [1, 300, 6], rows of
(x1 y1 x2 y2 score class) with the suppression already inside the graph
— that means 296 classes, coordinates read as class scores, and boxes
covering the frame. No error, just nonsense, which is exactly what the
Labelling Tool produced from the same model before its decoder was
fixed.

Its sibling ANSONNXYOLO has recognised that head from the tensor shape
for a while; this one had not, and the backend marker cannot tell them
apart because it names the runtime rather than the head.

The branch fires only where the tensor cannot be a grid: the 6 must be
the last axis with a detection count before it, hundreds rather than the
thousands of anchors a two-class [1, 6, 8400] export carries.

The decoder reuses this engine's own scaleCoords rather than repeating
the letterbox arithmetic, so both heads undo the padding the same way
and agree about where a box is, and the rounding into the integral box
type is spelled out instead of left to a narrowing conversion.

NOT COMPILED. The engine layer needs CUDA and the ANSLibs tree, neither
of which exists on this machine, so this is verified by reading: the
shape rule matches the one the other three products now share, every
symbol it uses is declared in this translation unit, and the arithmetic
is delegated to the function the grid path already trusts.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 06:55:45 +10:00
nghianguyenandClaude Opus 5 ea877a61bc fix(keys): decide at configure time whether this build has the key
Whether the model-package key is present was an __has_include test
inside the header, and that test leaves no dependency for the build
system. A key emitted after a build had already compiled the file would
not take effect until someone cleaned, and the resulting engine looks
exactly like one built without a key — which on a release machine means
shipping an engine that silently cannot open a package.

CMake now decides it and says which case it is, so emitting the key and
re-configuring is what installs it, visibly.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 05:56:58 +10:00
nghianguyenandClaude Opus 5 3c9e0b79f5 fix(ANSModelPackage): read the head off the graph, not off the package's word
A package whose family says "yolo" may hold an end-to-end export. Boxes
took the family from the backend marker, and the marker names the
runtime, not the head — so a fast yolo26 export arrived declared as
plain yolo while its graph emits [1, N, 6] with the NMS inside it.
Decoding that as a [1, 4+nc, N] grid reads coordinates as confidences:
detections everywhere, and nothing that looks like a failure.

OpenModelPackage now reads the first output's dims from the model it has
just decrypted, and corrects the family with the reason in the warning
rather than swapping it silently. It costs nothing there: the bytes are
in hand. The legacy marker is left alone, so anything converting back to
a zip still writes what ANSCORE expects.

A two-class v8 export is [1, 6, 8400] and must not be mistaken for one,
which is why the 6 has to be the last axis with a detection count before
it. Both cases are tested, and the package this Mac exported before the
writer was fixed now reads as yolo10 here too. 46 checks, no failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 00:19:15 +10:00
nghianguyenandClaude Opus 5 b1196ee8ba chore: ignore the in-tree build/ directory
Only cmake-build-*/ and out/ were ignored, so configuring into build/ —
the directory the ANSCORE_DEBUGVIEW_STRIP usage note in CMakeLists.txt
shows — left an untracked build/ in git status. build-mac/ is untracked
as well and stays that way; this pattern matches build/ alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 23:39:56 +10:00
nghianguyenandClaude Opus 5 6abe6d6694 fix(build): configure without CUDA instead of failing at generate
tests/CMakeLists.txt added every test directory unconditionally, so a
machine with no CUDA toolkit — every macOS one — died at generate time on
"links to CUDA::cudart but the target was not found", taking the
cross-platform targets down with it.

Gate each test the way its subject is already gated: the five that need
the toolkit behind ANSCORE_HAS_CUDA, the ANSIO one behind WIN32, and the
ANSONVIF one behind both. ANSONVIF itself uses no CUDA; its test names
CUDA::cudart and CUDA::nvjpeg only because it drives capture through
ANSCV and was copied from the ANSCV test.

Two non-test targets had the same problem. ANSCV genuinely needs the
toolkit — ANSGpuFrameOps.h includes <cuda_runtime.h> unconditionally and
calls cudaMemcpy2D/cudaMallocPitch — so it moves into the existing CUDA
block beside the engine modules. ANSLibsLoader binds cudart at runtime
and ships its own stubs, but still compiles against the runtime header
for the types and links CUDA::cudart_static, so it is gated too.

Also call enable_testing(), which the project never did: without it the
add_test() under tests/ reached nothing and ctest found no tests. Plain
enable_testing(), not include(CTest), to avoid its extra targets.

A CUDA machine is unchanged: every gate holds there, ANSCV keeps its
place in the subdirectory order, and the only new output is
CTestTestfile.cmake.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 23:38:57 +10:00
nghianguyenandClaude Opus 5 c82b2a67bc feat(ANSModelPackage): take the base key the ceremony gives this build
The reader has been able to open packages for a while and has held no
key, so it refused all of them. This is the seam that hands it one.

The base private key is not in this repository and never will be. It is
written by the ceremony (ANSMINDCORE's ansmodel-keys) into a generated
header that is gitignored and produced from the key vault on the machine
that builds a release. ANSModelKeys.h uses that file when it is there
and says "no keys" when it is not, so a developer's checkout builds and
runs with no secret in it: every zip still loads, and a package is
refused with a sentence that says why.

Keys are settled once — what the build carries, plus a developer's
environment — unless a caller sets a keyring outright, which now means
exactly that rather than depending on which call ran first.

Checked with the real keys against the packages ANSMINDCORE's side
produced: a package sealed by a box that holds only the public half
opens here byte for byte, and one signed by the new release key verifies
against the built-in root. If those two products disagreed, a model that
runs in one would not run in the other, which is the failure this format
exists to avoid. 40 checks in the package test, no failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 23:05:00 +10:00
nghianguyenandClaude Opus 5 8e50120759 feat(ANSModelPackage): ask the model its input size when nothing else says
A package now carries what its model expects on the input, but every
package written before that does not — and neither does the zip, which
is most of what is in the field. The engine was then left to assume
640x640, which is wrong for a classification export at 224 and for
anything trained at a rectangular size.

The model itself always knows. ANSOnnxShape reads the first input's
shape straight out of the ONNX protobuf, from memory or from a file,
walking the top level and SEEKING past the nodes and the initializers
rather than reading them — the fields it wants sit after all of the
weight on the wire, and a 324 MB model answers in ten milliseconds.

OpenModelPackage uses it only when the manifest is silent, which costs
nothing there because the bytes are already decrypted in hand. An axis
the graph leaves open is reported as zero and a file that cannot be
parsed is a failure: both mean "this model does not say", and the caller
keeps the default it had. Being wrong about an input size would be worse
than being silent about it.

Checked against python-onnx on all 46 models on this machine, static and
dynamic, four-dimensional and not, with no disagreements. The test
writes ONNX graphs byte by byte, with node and initializer ahead of the
input as a real export has them, and packs one into a package whose
manifest says nothing — which is the case this exists for. 40 checks,
no failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 22:47:47 +10:00
nghianguyenandClaude Opus 5 080910c8ac feat(ODEngine): open a model package and run it without unpacking it
An engine's model has always been a path to a password-protected zip:
ANSODBase inflates it into a folder, and every engine then picks out
train_last.onnx, classes.names and model_config.json. A .ansmodel
package replaces all four with one sealed file, and this is the part
that turns it back into what the engines already understand.

ANSODBase::LoadModel and ::Initialize now recognise a .ansmodel path and
take a second route: the package is opened with the process keyring, the
model is decrypted into memory, and the classes, task and input geometry
come out of the manifest instead of loose files. Nothing is extracted,
so the unprotected model never touches the disk — which is the reason
the format exists, and it is one function's job to keep.

The engines follow it down. BasicOrtHandler gains a constructor that
takes the bytes rather than a path; it keeps a view of them, not a copy,
because the weights are in RAM either way once a session exists and a
copy would only double what every loaded model costs. The owner of the
plaintext is ANSODBase, which holds it for as long as the engine object
lives — that is what lets the DirectML recovery path rebuild a session
after a driver fault — and wipes it on teardown.

Wired through ANSONNXYOLO (and so the classification facade), ANSONNXOBB,
ANSONNXPOSE, ANSONNXSEG and YOLO12OD. YOLOOD is left alone: it loads
darknet weights and OpenCV DNN graphs, neither of which the writer
produces. A zip still loads exactly as before, on every engine.

Keys come from SetProcessModelKeys, as in ANSMINDCORE, with the same
development fallback from the environment. With none set, a package is
refused with a sentence saying so and every zip still works, so a build
made before the key ceremony runs everything except packages.

The engine layer needs CUDA to compile, so the contract it depends on is
checked where it can be: ANSModelPackage-UnitTest now opens the fixture
ANSMINDCORE wrote through the same glue ANSODBase uses, and covers the
refusal a keyless build gives. 32 checks, no failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 22:21:39 +10:00
nghianguyenandClaude Opus 5 6c164c3d0f feat(ANSModelPackage): read the ANS model package here too
The second reader. A package is one encrypted file — cleartext header,
sealed manifest, model bytes sealed in chunks — and this opens it: the
content key unwrapped with X25519 against the base key, the header
verified as Ed25519 signed by a release key the root vouches for, the
model decrypted into memory and handed to ANSModelSource, which the
engines already take.

It is a new library rather than code inside the engine layer, and
deliberately free of the heavy dependencies around it: no CUDA, no
OpenCV, no LabVIEW. That is what lets it build and be tested on every
platform, including the ones where the engine layer does not build.

THE TWO IMPLEMENTATIONS MUST AGREE, so this does not merely claim to:
tests/ANSModelPackage-UnitTest/data/interop.ansmodel was written by
ANSMINDCORE's packer, and the test opens it here — manifest, classes and
a chunked payload, byte for byte. A change to either wire format shows
up as this failing. The fixture's keys are derived by published rules,
recorded beside it, so anyone can rebuild it.

The AEAD and the small primitives are copies of ANSMINDCORE's, not
reimplementations, for the same reason. Monocypher 4.0.2 is vendored
here as it is there, including the RFC 8032 pair — its default EdDSA
signs with BLAKE2b and would not interoperate.

Verified by compiling and running the test directly: 19 checks, RFC
8439, RFC 7748 and RFC 8032 vectors, the interop fixture, a round trip,
a flipped bit, an unknown root, a revoked signer, and rubbish bytes.
ANSCORE's own CMake configure fails on this machine for reasons that
predate this change — eight errors with or without it, all missing
dependencies — so the new CMake targets still need a real build box.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 21:24:53 +10:00
nghianguyenandClaude Opus 5 5820a5e61b feat(ODEngine): load a model from memory, not only from a path
Every engine built its session from a file path, which is why opening an
encrypted model package means inflating it to a directory first: the
unprotected model lands on disk, is optimised, and is zipped back into a
cache. The new package format cannot avoid that from the outside — the
engines have to be able to take bytes.

ANSModelSource.h introduces a ModelSource (a path, or bytes with a name
for logs) and one MakeSession that builds from either. The six ONNX
engines — classification, OBB, pose, segmentation, YOLO12 and YOLO —
each gain a load overload taking it, with the existing string form
delegating so no caller changes. The Windows wide-path conversion and
the from-memory call now live in one place instead of the eleven copies
they were spread across; YOLOOD, which had only the Windows branch,
becomes portable as a side effect.

The factories are named rather than implicit, so a call with a plain
string literal still resolves to the path overload without ambiguity.

Not compiled here: this layer builds only where CUDA is present, so
verification on this machine was the new header compiled and run, plus a
structural check that every engine declares and defines both overloads,
calls MakeSession exactly once, and has no raw session construction
left. It needs a build on a CUDA box before it ships.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 20:29:59 +10:00
nghianguyenandClaude Fable 5 430c8057b8 ANSFR: report v1.4 emotion class names verbatim in the attribute JSON
The legacy display remap in FaceAttributeToJsonString rewrote most
emotions to "Attentive", which hid the v1.4 classifier's real classes.
The remap now applies ONLY to the legacy OpenVINO model's lowercase
classes (sad/happy/neutral/anger/surprise - v1.1 output unchanged);
every other name, i.e. the classes read from emotionsrecognition.names
(Angry, Fear, Happy, Neutral, Sad), passes through verbatim. A retrained
classifier with a different .names list needs no code change.

Verified on the classroom video: faces previously labelled "Attentive"
now read "Angry (100%)"; smiling faces read "Happy (100%)". Parity test
updated to check each pack against its own emotion vocabulary.

Note for client code: v1.4 deployments no longer emit "Attentive" -
anything matching that string must handle Angry/Fear/Sad instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-28 16:50:47 +10:00
nghianguyenandClaude Fable 5 024d55197b ANSFR: raise default slow-attribute refresh interval to 20 frames
With the heavier v1.4 ONNX age-gender model, interval 20 benchmarks at
throughput parity with the v1.1 pack (16.8 ms / 59 fps vs 16.0 ms / 62
fps on RTX 3070, classroom.mp4, ~3.1 faces/frame) versus 19.9 ms / 50
fps at the old default of 5. Attributes refresh every ~0.8 s on a 25 fps
camera (1.6 s on stable scenes, faster when faces appear), which is
ample for age/gender/emotion; head pose and recognition still run every
frame. Deployments wanting fresher attributes can lower it at runtime
via SetANSRFAttributeInterval. Full test suite passes at the new
default; GetANSRFModelInfo reports "attribute_interval":20.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-27 02:09:29 +10:00
nghianguyenandClaude Fable 5 f34817c620 ANSFR: configurable slow-attribute refresh interval
New API SetANSRFAttributeInterval(Handle, frames) / UpdateAttributeInterval
(1..60, default 5) controls how often the slow attributes (age/gender +
emotions) refresh per camera; head pose and recognition are unaffected.
GetANSRFModelInfo now reports "attribute_interval". The adaptive logic's
stable-scene branch previously capped at a hardcoded 10 - it now doubles
the configured interval instead (default behaviour 10/5/2 unchanged).

Motivation: the v1.4 ViT age-gender model is heavier per attribute frame
than the old OpenVINO IR. Interval sweep on RTX 3070 (classroom.mp4,
~3.1 faces/frame) vs the v1.1 baseline at 16.0 ms / 62 fps:
  interval  5 (default): 19.9 ms / 50 fps  (+3.9 ms)
  interval 10:           17.1 ms / 58 fps  (+1.1 ms)
  interval 20:           16.5 ms / 61 fps  (parity)
Recommendation: default on 8 GB GPUs; interval 10 on small-VRAM machines.

ANSFR-UnitTest: --bench old|new [interval] runs the sweep.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-27 01:57:54 +10:00
nghianguyenandClaude Fable 5 89b2efe182 ANSFR-UnitTest: point tests at the official SharedCore model packs
The re-packed ANS_FaceRecognizer_v1.4.zip (ONNX attribute models only, no
OpenVINO agegender/emotion IRs) is now the official pack and lives in the
canonical distribution folder C:\Projects\SharedCore next to v1.1.

Centralize every hardcoded pack path into three file-scope constants
(kFaceRecognizerV14Zip, kFaceRecognizerV11Zip, kFaceDetectorZip) used by
the model-switch test, classroom demo, benchmark, and parity test - a
future pack bump is a one-line edit. Verified against the official path:
adaface + ONNX attribute backends load and bench at 17.9 ms avg. The
slimmer pack was separately validated from a clean extraction (all
TestModelSwitchV14 checks pass without the OpenVINO fallback files).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-27 01:40:59 +10:00
nghianguyenandClaude Fable 5 13eacc4ab2 ANSFR: persist explicit recognizer-model choice across restarts
SetANSRFRecognizerModel now records the choice in the face database
(SETTINGS key RECOGNIZER_MODEL_PREF), and LoadEngine restores it when no
explicit choice was made in the current session - so a runtime switch to
ArcFace (or AdaFace) survives server restarts without re-calling the API.

Kept separate from EMBEDDING_MODEL on purpose: only an actual API call is
recorded as a preference. An automatic ArcFace fallback (pre-v1.4 zip
without adafacerecognizer.onnx) is not persisted, so upgrading such a
deployment to the v1.4 pack still gets the AdaFace default.

ANSFR-UnitTest: TestModelSwitchV14 (now also runnable via --test) gains a
simulated restart - release handle, recreate on the same database, and
assert the persisted ArcFace choice is restored and still recognizes the
enrolled user. Verified ALL PASSED.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:47:32 +10:00
nghianguyenandClaude Fable 5 06829ae0e3 ANSFR-UnitTest: output-format parity test between v1.1 and v1.4 packs
TestOutputFormatParity (--parity) runs the identical enroll + inference
flow through the v1.1 pack (arcface + OpenVINO attributes) and the v1.4
pack (adaface + ONNX attributes) and asserts the emitted JSON is
format-identical: same result-object fields, same attribute keys
(Gender/Age/AgeYears/Emotion/Pose), same value vocabularies (Male/Female,
the four Age buckets, Happy/Neutral/Attentive emotion labels with (NN%)),
and that both packs recognize the enrolled user. Verified ALL PASSED on
the RTX 3070 test machine.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:34:46 +10:00
nghianguyenandClaude Fable 5 103c053ebb ANSFR: emit numeric AgeYears in face attributes; clearer demo overlays
- FaceAttributeToJsonString now adds "AgeYears":"<n>" beside the existing
  "Age" bucket (Kids/Young Adults/Adults/Elders) - additive key, existing
  consumers parsing "Age" are unaffected.
- Classroom demo overlay: explicit per-line labels (Gender: / Age: /
  Emotion:) and the numeric age in parentheses, e.g. "Age: Adults (28)".

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:27:25 +10:00
nghianguyenandClaude Fable 5 775d4d6b16 ANSFR-UnitTest: classroom attribute-overlay demo + v1.1 vs v1.4 benchmark
- ClassroomAttributesVideoTest (default main): plays classroom.mp4 through
  the v1.4 pipeline and overlays user name, Gender/Age, and Emotion parsed
  from each result's extra_info JSON; saves an annotated frame every 30
  frames to C:\Programs\DemoAssets for headless review.
- --bench old|new|both: per-pack speed benchmark (100 measured frames after
  10 warmup, full pipeline with attributes on) run one pack per process.
  RTX 3070 laptop results: v1.1 arcface+OpenVINO 17.8 ms avg (56.2 fps) vs
  v1.4 adaface+ONNX/TRT 18.2 ms avg (55.1 fps) at 3.1 faces/frame — the
  new models carry no throughput cost.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:20:43 +10:00
nghianguyenandClaude Fable 5 ac4369302a ANSFR: support ANS_FaceRecognizer v1.4 models (AdaFace + ONNX attributes)
- Recognizer: adafacerecognizer.onnx (AdaFace, new default) with automatic
  fallback to ansfacerecognizer.onnx (ArcFace) for pre-v1.4 zips. AdaFace
  consumes BGR: new AdaFace handler in ONNXEngine skips the BGR2RGB swap,
  and the TensorRT paths gate their cvtColor on m_swapRB.
- New C API: SetANSRFRecognizerModel (0=AdaFace, 1=ArcFace),
  GetANSRFRecognizerModel, GetANSRFModelInfo. A live switch reloads the
  recognizer and regenerates all stored embeddings from the enrolled face
  crops; the generating model is persisted in a new SETTINGS table
  (EMBEDDING_MODEL) so upgrading an ArcFace database to the AdaFace
  default migrates automatically on LoadEngine.
- Attributes: prefer agegender.onnx (ViT: gender_logits [male,female] +
  age_pred in years; also handles the single-tensor research export) and
  emotionsrecognition.onnx (YOLOv11-cls, classes from .names) behind new
  IAgeGenderEstimator/IEmotionsClassifier interfaces, falling back to the
  legacy OpenVINO IRs. EP follows hardware: NVIDIA uses TensorRT EP with
  disk-cached engines, AMD DirectML, Intel OpenVINO.
- Emotion JSON mapping: handle the new Fear class and the never-matched
  surprise label (both map to Attentive).
- ANSFR-UnitTest: TestModelSwitchV14 covers default model selection, ONNX
  attribute backends, enrollment, and adaface<->arcface switching.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:06:10 +10:00
nghianguyenandClaude Opus 5 881d783fa3 ANSFR: re-enable CheckFaceEmbedding validation
Comments out the unconditional `return 1` added in b5a88b9, so the function
runs its real body again: the argument guards (null Handle / zero-length
buffer -> -1), cv::imdecode, and ANSFacialRecognition::CheckFace with the
detector confidence threshold, isValidFace's geometry gate and the
_minFaceSize filter.

The bypass line is left in place commented rather than deleted, so it stays
easy to flip back on.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 20:05:54 +10:00
nghianguyenandClaude Opus 5 c9ff9ee2f3 ANSFR-UnitTest: point the harness at the local ANSVIS deployment
Repoints the hardcoded fixtures at the installed ANSVIS Server layout rather
than the old E:/C: DemoAssets trees:

  - classroom.mp4 now read from C:\ProgramData\ANSCENTER\Shared
  - TestCompleteFR1 loads the GPU-specific ServerOptimised zips
    (ANS_FaceRecognizer_v1.1_NVIDIAGeForceRTX4070LaptopGPU.zip) and re-enables
    the InsertUser call ahead of InsertFace
  - main() runs ANSVISTest()

Also comments out the --gpu argument handling in main(), so the harness no
longer sets CUDA_VISIBLE_DEVICES and the single-GPU bisect flag is inert until
that block is restored.

Paths remain machine-specific; this is a developer harness, not a test suite.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 19:38:57 +10:00
nghianguyenandClaude Opus 5 b5a88b9335 ANSFR: bypass CheckFaceEmbedding validation for now
CheckFaceEmbedding returns 1 unconditionally, ahead of the JPEG decode and the
CheckFace() call, so every caller is told the submitted image is acceptable.

Deliberate temporary bypass, not a fix. What stops running behind it:

  - argument guards -- a null Handle or a zero-length buffer used to return -1
    and now returns 1
  - cv::imdecode, so an undecodable JPEG is no longer rejected
  - ANSFacialRecognition::CheckFace, and with it the detector confidence
    threshold, isValidFace's geometry gate (5 landmarks inside the box, roll
    <= 27 deg, yaw asymmetry <= 0.5, pitch 0.15-0.75, mouth/eye ratio 0.6-1.6)
    and the _minFaceSize filter

Enrollment callers that relied on this as a pre-check now accept any input.
Revert this commit to restore the checks.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 17:37:20 +10:00
nghianguyenandClaude Opus 5 b300cbf7a9 ANSLS: enumerate GPUs over DXGI so detection survives a missing OpenCL ICD
hwinfo::getAllGPUs() is an OpenCL device list -- the prebuilt HWiNFO.lib has no
Win32_VideoController path, only clGetPlatformIDs/clGetDeviceIDs/clGetDeviceInfo.
A machine whose driver never registered an OpenCL ICD therefore reported no GPU
at all, even with a healthy WDDM driver driving the display. Two consequences:
the system-info JSON emitted "GPU":"", and CheckHardwareInformation() silently
fell back to the CPU engine on a box holding a perfectly good CUDA card.

DXGI needs only a WDDM display driver, so it sees the adapter either way.

CheckHardwareInformation() now classifies adapters by PCI vendor id. Exact ids
also retire the substring matching and its "ATI" inside "CORPORATION" workaround.
It falls back to the OpenCL list when DXGI returns nothing.

GetSystemHardwareInformation() keeps its OpenCL loop untouched and merely appends
DXGI adapters that OpenCL did not already report, so the JSON stays byte-identical
-- same keys, order, indices and values -- on any machine that already worked.
Appended entries reuse OpenCL's vendor spelling (OpenClStyleVendor) and read the
driver version from the display-adapter class key, since DXGI exposes neither.

Verified on a dual-adapter box: the GPU node matches the pre-change output
exactly, and forcing the append path yields the same shape with values sourced
from DXGI plus the registry.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 16:16:38 +10:00
nghianguyenandClaude Opus 4.8 fa48516792 gitignore Claude Code per-user local settings
.claude/settings.local.json is machine-specific permission config; it
was tracked by accident and showed as perpetually modified on every
machine. Untracked (kept on disk) and ignored.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 11:57:24 +10:00
nghianguyenandClaude Opus 4.8 fda0f6f86f ANSFR: name the real cause when enrollment cannot compute embeddings
Enrolling a face while the recognizer engine is down used to store the
user row and silently drop the embedding: InsertUser is a pure DB write,
Feature() returns empty when uninitialized, and InsertFace's size check
misreported that as "Invalid embedding generated" — blaming the face.
The result surfaces hours later as every match returning Unknown/2.0
with nothing in the log saying why (seen on the A400: 45 users, zero
embeddings).

- ANSFRBase gains a public IsInitialized() accessor (flag existed but
  was protected).
- InsertFace / InsertMultipleFaces gate on it before computing the
  embedding and log the true cause: recognizer not initialized, face
  NOT enrolled, fix engine errors then re-enroll.
- The residual invalid-embedding message now reports actual vs expected
  size and the remaining causes (unusable crop / recognizer reloading).
- Reload() warns when the database has users but zero embeddings — the
  one line that identifies stale half-enrolled databases at startup.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 11:36:22 +10:00
nghianguyenandClaude Opus 4.8 799f755d5a ANSFR: purge stale ServerOptimised zip when engine rebuilds from ONNX
A ServerOptimised zip is a per-GPU cache of a previous engine build. When
the engine profile changes (batch gate, TRT/CUDA upgrade), the cached
engine inside it goes stale: every re-extract (e.g. after Temp cleanup)
restores the old engine and costs a full ONNX rebuild, forever, because
the server only regenerates the zip when the file is missing.

Engine now records rebuiltFromOnnx() + engineFilePath() in
buildLoadNetwork (set only on the generate-from-ONNX branch; cache hits
and alternative-precision loads don't raise it). After a rebuild,
ANSFaceRecognizer::Initialize deletes the stale same-GPU engine variants
from the model folder and the supplied optimised zip, so the server's
next optimize pass recreates it with the fresh engine.

Safety: only zips whose parent folder is ServerOptimised/-ized qualify —
base model zips can never match. Engine purge is scoped to the
"<model>.engine.<GPU>." prefix, leaving other GPUs' engines intact on
multi-GPU machines. All deletes are best-effort (error_code); multi-GPU
pool paths never set the flag, so the purge is a no-op there.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 11:21:48 +10:00
nghianguyenandClaude Opus 4.8 da9d922498 ANSFR: gate recognizer batch size by GPU VRAM
Batch-4 was tuned for 8 GB GPUs; its TRT workspace (~54 MiB per batch
unit) crowds out the other engines on smaller cards. VramCappedBatch
keeps the configured batch on 8 GB-class cards and up (threshold
7.5 GiB — 8 GB cards report ~8188 MiB) and drops to single-face batches
below that: identical results, multi-face frames just run sequentially,
~160 MiB freed on a 4 GB RTX A400. Also caps the legacy 8/32 LoadEngine
profile, whose .b32 engine (~1.7 GB workspace) would be fatal on small
cards. Initialize() and OptimizeModel() share the same call so their
profiles cannot drift.

Small cards rebuild the recognizer engine once (filename loses the .b4
suffix); 8 GB+ machines keep their cached engines unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 10:44:22 +10:00
nghianguyen 6939775960 Merge branch 'main' of ssh://anscenter.ddns.net:2222/anscenter/ANSCORE
# Conflicts:
#	core/anslicensing/hwid.cpp
2026-08-21 10:31:45 +10:00
nghianguyenandClaude Opus 4.8 60a8b39d24 licensing: HWID diagnosability, matcher widening, delete-condition fix
- hwid.cpp: BitStream3 buffer 125 -> 160 bits (writes totalled 147 bits
  and overflowed the old allocation; encoded HWID bytes unchanged).
  Log component availability (uuid/cpu/mac/hdd) once per distinct HWID,
  and warn when WMI yields no components at all.
- Matcher widened only, generation untouched: exact-string match
  short-circuits, UUID match accepts any WMI list entry (generator
  hashes the last valid one), fuzzy-match point breakdown is logged so
  field status-12 rejections are diagnosable.
- ANSLicense.cpp: fix inverted delete condition (!p != NULL) at 6 sites;
  detect the shared "empty fingerprint" HWID of WMI-broken PCs and warn
  with repair steps; mask activation keys in error strings.
- licensingclient.h: explain SetKey/validation/NTP failures on stderr.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 13:11:16 +10:00
nghianguyenandClaude Opus 4.8 d2479b9b07 GpuMemoryGuard: scale VRAM safety margin to card size
The margin was a fixed 768 MiB — 3% of a 24 GiB compute card but 19% of
a 4 GiB card (RTX A400), where free VRAM never clears it, so CanLoad()
refused every engine and UnderPressure() skipped every frame. Now
total/8 clamped to [192, 768] MiB: cards >= 6 GiB keep exactly the old
margin, only small cards change. ANSCENTER_GPU_MARGIN_MB / SetMargin()
still override (0 = auto). Refusal log now reports total VRAM alongside
free, which is what made this diagnosable.

Verified on the A400 test PC: detector + recognizer load and match.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 13:10:10 +10:00
73 changed files with 10139 additions and 671 deletions
-329
View File
@@ -1,329 +0,0 @@
{
"permissions": {
"allow": [
"Bash(grep -v \"\\\\[EzMemoryManagerPlugin\\\\]\\\\|filesystem_win\\\\|at System\\\\|at NationalInstruments\\\\|Memory Leak Tracking\\\\|System\\\\.Argument\\\\|DNError\\\\|Parameter name\\\\|Inner Exception\\\\|^[0-9]*\t[0-9.]*\t[0-9]*\t$\" \"C:/Users/nghia/Downloads/ANSLEGION1.log\")",
"Bash(grep -v \"^$\")",
"Bash(grep -v \"^[^\t]*\t[^\t]*\t18924\t *$\")",
"Bash(dumpbin //EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Bash(\"/c/Program Files \\(x86\\)/Windows Kits/10/bin/10.0.26100.0/x64/dumpbin.exe\" //EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Bash(\"/c/Program Files \\(x86\\)/Windows Kits/10/bin/10.0.26100.0/x64/dumpbin.exe\" /EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Bash(\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe\" /EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Bash(\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe\" //EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Read(//c/Program Files/ANSCENTER/**)",
"Bash(powershell.exe -NoProfile -Command \"[System.Environment]::GetEnvironmentVariable\\('PATH','Machine'\\) -split ';' | Select-String -Pattern 'ANSCENTER|Shared'\")",
"Bash(cmd.exe //c 'dir /AL \"C:\\\\Program Files\\\\ANSCENTER\\\\ANSVIS\\\\data\" 2>&1 | findstr /i \"junction symlink\"')",
"Bash(cmd.exe //c 'dir /AL \"C:\\\\Program Files\\\\ANSCENTER\\\\ANSVIS\\\\data\"')",
"PowerShell(Get-ChildItem \"C:\\\\Program Files\\\\ANSCENTER\\\\ANSVIS\\\\data\" -Force | Where-Object { $_.LinkType } | Select-Object Name, LinkType, Target | Format-Table -AutoSize)",
"Bash(awk '{print \"start: \"$2\"s\"}')",
"Bash(awk '{print \"end: \"$2\"s\"}')",
"Bash(awk '{ *)",
"Bash(awk '{v[NR]=$1} END {asort\\(v\\); n=length\\(v\\); printf \"count=%d\\\\nmedian=%.1fms\\\\np90=%.1fms\\\\np95=%.1fms\\\\np99=%.1fms\\\\nmax=%.1fms\\\\n\", n, v[int\\(n*0.5\\)], v[int\\(n*0.9\\)], v[int\\(n*0.95\\)], v[int\\(n*0.99\\)], v[n]}')",
"Bash(awk '{v[NR]=$1} END {asort\\(v\\); n=length\\(v\\); printf \"slow_inf_count=%d \\(over %d total inferences = %.1f%%\\)\\\\nmedian=%.1fms max=%.1fms\\\\n\", n, 10456, 100.0*n/10456, v[int\\(n*0.5\\)], v[n]}')",
"Bash(awk '{v[NR]=$1} END {asort\\(v\\); n=length\\(v\\); if\\(n>0\\){printf \"slow_getImage_count=%d median=%.1fms max=%.1fms\\\\n\", n, v[int\\(n*0.5\\)], v[n]}}')",
"Bash(awk -F= '{print $2}')",
"Bash(git -C \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\" status --short engines/ONNXEngine/ONNXEngine.cpp)",
"Bash(git -C \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\" diff engines/ONNXEngine/ONNXEngine.cpp)",
"Bash(git -C \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\" status --short)",
"Bash(grep -E \"\\\\.\\(cpp|h\\)$\")",
"Bash(awk '{print \"MEDIA_Leak heartbeats: \"$1}')",
"Bash(awk '{print \"ANSMOT heartbeats: \"$1}')",
"Bash(awk '{print \"PROC_MEM samples: \"$1}')",
"Bash(awk 'NR==1 || NR%15==0 { *)",
"Bash(awk 'NR==1 || NR%10==0 || NR==78 {print}')",
"Bash(sort -t= -k2 -n)",
"Bash(awk ' *)",
"Bash(sort -k3 -t= -n)",
"Bash(awk '{print \"MEDIA_Leak: \"$1}')",
"Bash(awk '{print \"BYTETracker heartbeats: \"$1}')",
"Bash(awk '{print \"PROC_MEM: \"$1}')",
"Bash(sort -k2 -t= -n)",
"Bash(awk 'NR==1 || NR%30==0 || NR==571 { *)",
"Bash(awk 'NR==2 || NR==80 || NR==160 || NR==240 || NR==285 {print}')",
"Bash(grep -n \"^\\\\s*return\\\\s\\\\+\\\\\\(true\\\\|false\\\\\\);$\\\\|^}\\\\s*$\" C:/Projects/CLionProjects/ANSCORE/engines/TensorRTAPI/include/engine/EngineRunInference.inl)",
"Bash(awk '$1 > 267 && $1 < 1100')",
"Bash(awk 'NR>267 && /^}/ {print NR\": \"$0; c++; if\\(c>=3\\)exit}' C:/Projects/CLionProjects/ANSCORE/engines/TensorRTAPI/include/engine/EngineRunInference.inl)",
"Bash(awk '{print \"TRT_Leak: \"$1}')",
"Bash(awk '{print \"Pool_Leak: \"$1}')",
"Bash(awk '{print \"BYTETracker: \"$1}')",
"Bash(awk 'NR==1 || NR%70==0 || NR==889 { *)",
"Read(//c/Users/nghia/Downloads/**)",
"Bash(awk '{print \"OCR_Leak: \"$1}')",
"Bash(awk 'NR==1 || NR%20==0 || NR==157 { *)",
"Bash(xargs file *)",
"Bash(xargs grep *)",
"Bash(awk '{print \"BYTETracker heartbeat: \"$1}')",
"Bash(awk 'NR==1 || NR%30==0 || NR==352 { *)",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/total=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"YOLO_Timing total ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/preproc=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"YOLO_Timing preproc ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/inf=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"YOLO_Timing inference ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"MEDIA_Timing getImage total ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/pull=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"MEDIA_Timing pull ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/convert=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"MEDIA_Timing convert ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{print $2}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"TRT_Slow timestamps \\(sec\\): n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk -F'total=' '{print $2}')",
"Bash(awk '{print $1}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"TRT_Slow total ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"SLOW memcpy timestamps \\(sec\\): n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk -F. '{print int\\($1/100\\)}')",
"Bash(awk -F' ' '{print $3}')",
"Bash(awk '$1 > 200')",
"Bash(awk -F'net=' '{print $2}')",
"Bash(awk -F'bytesMB=' '{print $2}')",
"Bash(awk -F'bytesMB=' '{gsub\\(/[^0-9.]/,\"\",$2\\); print $2}')",
"Bash(awk -F'ids_tot=' '{print $2}')",
"Bash(dumpbin /symbols \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release/bin/ANSCV.dll\")",
"Bash(mkdir -p \"C:/ProgramData/ANSCENTER\")",
"Bash(touch \"C:/ProgramData/ANSCENTER/ansvisdebugview.txt\")",
"Read(//c/ProgramData/ANSCENTER/**)",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"YOLO_total: n=%d p50=%.1f p95=%.1f p99=%.1f max=%.1f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"YOLO_preproc: n=%d p50=%.1f p95=%.1f p99=%.1f max=%.1f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"YOLO_inf: n=%d p50=%.1f p95=%.1f p99=%.1f max=%.1f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"MEDIA_total: n=%d p50=%.2f p95=%.2f p99=%.2f max=%.2f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"MEDIA_convert: n=%d p50=%.2f p95=%.2f p99=%.2f max=%.2f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(python -c ' *)",
"Bash(python)",
"Bash(git config *)",
"Bash(git submodule *)",
"Bash(git rm *)",
"Bash(rm -rf .git/modules/3rdparty/libyuv)",
"Bash(git add *)",
"Read(//c/ProgramData/Jh7O7nUe7vS/Models/EngineModels/B-IN_ANS_VehicleDetection_v2.0_67345015/**)",
"Bash(xxd)",
"Bash(icacls \"C:\\\\ProgramData\\\\Jh7O7nUe7vS\\\\Models\\\\EngineModels\\\\B-IN_ANS_VehicleDetection_v2.0_67345015\\\\train_last.onnx\")",
"Bash(grep -oE '<Data[^>]*>[^<]{0,400}</Data>' \"C:/Users/nghia/Downloads/error.xml\")",
"Bash(grep -oE '<Data[^>]*>[^<]{0,500}</Data>' \"/c/Users/nghia/Downloads/error.xml\")",
"Read(//tmp/**)",
"Bash(grep -oE '<Data[^>]*>[^<]{0,400}</Data>' \"/c/Users/nghia/Downloads/error.xml\")",
"Bash(echo \"Exit: $?\")",
"Bash(python -)",
"Bash(git show *)",
"Bash(dumpbin //dependents \"C:\\\\\\\\ProgramData\\\\\\\\ANSCENTER\\\\\\\\Shared\\\\\\\\ANSLicensingSystem.dll\")",
"Bash(grep -v \"//\")",
"Bash(grep -v \"^ *//\")",
"Bash(python strip_folder_lock.py)",
"Bash(tasklist //FI \"IMAGENAME eq ANSLPR-UnitTest.exe\")",
"Bash(taskkill //PID 41856 //F)",
"PowerShell(Stop-Process -Id 41856 -Force)",
"PowerShell(Start-Process -FilePath \"taskkill.exe\" -ArgumentList \"/PID\",\"41856\",\"/F\" -Verb RunAs -Wait)",
"Bash(npm root *)",
"Bash(npm list *)",
"Bash(npm install *)",
"Bash(NODE_PATH=\"C:/home/alex/.npm-global/node_modules\" node build_anslib_logging_guide.js)",
"Bash(python \"C:/Users/nghia/AppData/Roaming/Claude/local-agent-mode-sessions/skills-plugin/d8e35aa4-a14e-4e20-b921-ba1b9a3cce86/cdda7cc8-a1c7-42ff-98b4-473ec3e8b9fb/skills/docx/scripts/office/validate.py\" \"C:/Projects/CLionProjects/ANSCORE/docs/ANSLIB_Logging_Guide.docx\")",
"Bash(python \"C:/Users/nghia/AppData/Roaming/Claude/local-agent-mode-sessions/skills-plugin/d8e35aa4-a14e-4e20-b921-ba1b9a3cce86/cdda7cc8-a1c7-42ff-98b4-473ec3e8b9fb/skills/docx/scripts/office/soffice.py\" --headless --convert-to pdf --outdir \"C:/Projects/CLionProjects/ANSCORE/docs\" \"C:/Projects/CLionProjects/ANSCORE/docs/ANSLIB_Logging_Guide.docx\")",
"Bash(sort -t: -k1 -u)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff core/ANSLicensingSystem/ANSLicense.cpp)",
"Bash(awk 'NR>=154022 && NR<=156428 {print}' \"C:/Users/nghia/Downloads/AVNET-8845HS2.log\")",
"Bash(awk 'NR>=156350 && NR<=156428 {print}' \"C:/Users/nghia/Downloads/AVNET-8845HS2.log\")",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE log --oneline -10 -- modules/ANSFR/OpenVINODeviceConfig.h modules/ANSFR/OpenVINODeviceConfig.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE log --oneline -5 -- core/ANSLibsLoader/EPLoader.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show --stat 69787b0)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show --stat 97d8149)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show 97d8149 -- engines/ONNXEngine/OpenVINODeviceConfig.h modules/ANSFR/ANSFR.cpp)",
"Bash(awk 'NR>=7106 && NR<=7250 && /ONNXYOLO|ANSCUSTOM|DetectObjects/ {print}' \"C:/Users/nghia/Downloads/AVNET-8845HS5.log\")",
"Bash(awk 'NR>=5850 && NR<=7115 && /ONNXYOLO|ANSCUSTOM_Infer|ANSONNXCL_pp|cls\\\\] calling/' \"C:/Users/nghia/Downloads/AVNET-8845HS5.log\")",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show --stat 3a527d2)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show 3a527d2 -- modules/ANSODEngine/ANSCUSTOMDETECTOR.cpp modules/ANSODEngine/ANSONNXYOLO.cpp modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show 3a527d2 -- modules/ANSODEngine/ANSEngineCommon.h)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff HEAD modules/ANSODEngine/ANSONNXYOLO.cpp modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff HEAD modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff HEAD modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE log --all --oneline --since='3 weeks ago' -- modules/ANSODEngine/ANSONNXYOLO.cpp modules/ANSODEngine/ANSONNXCL.cpp engines/ONNXEngine/ONNXEngine.cpp engines/ONNXEngine/ONNXEngine.h)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show --stat 844d739)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE log -p --grep=\"mutex\\\\|lock\\\\|hang\\\\|stall\\\\|concurrent\\\\|thread\" --since=\"3 weeks ago\" -- modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show 844d739 -- modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(awk '$2>=835 && $2<=860 && /1853990010|ANSCUSTOM|ANSONNXCL|ANSONNXYOLO/' \"C:/Users/nghia/Downloads/AVNET-8845HS7.log\")",
"Bash(awk '$2>=836 && $2<=837 && /ANSCUSTOM|ANSONNXCL/' \"C:/Users/nghia/Downloads/AVNET-8845HS7.log\")",
"Bash(awk '$2>=836.0 && $2<=836.9' \"C:/Users/nghia/Downloads/AVNET-8845HS7.log\")",
"Bash(grep -nB1 -A3 \"DirectML\\\\|DisableMemPattern\\\\|SetExecutionMode\\\\|ExtendedOptimization\\\\|ORT_SEQUENTIAL\" \"C:/Projects/CLionProjects/ANSCORE/engines/ONNXEngine/ONNXEngine.cpp\")",
"Bash(grep -nE \"^\\\\}$|warmupModel\\\\\\(|#if 0|#endif|ANSONNXCL_legacy_Init\" C:/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE restore modules/ANSODEngine/ANSONNXCL.h modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE status --short modules/)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff --stat HEAD modules/ANSODEngine/ANSONNXCL.cpp modules/ANSODEngine/ANSONNXOBB.cpp modules/ANSODEngine/ANSONNXPOSE.cpp modules/ANSODEngine/ANSONNXSEG.cpp modules/ANSODEngine/ANSYOLO12OD.cpp engines/ONNXEngine/ONNXEngine.cpp engines/ONNXEngine/ONNXSAM3.cpp)",
"Bash(awk -F'[:\\(\\)]' '{gsub\\(/^ +| +$/,\"\",$2\\); split\\($2,a,\" \"\\); ms=a[1]; if \\(ms+0 >= 500\\) print $0}')",
"Bash(awk -F'[:\\(\\)]' '{split\\($2,a,\" \"\\); ms=a[1]; if \\(ms+0 >= 100\\) print $0}')",
"Bash(grep -cE \"^\\\\s*Plate: [^ ]+$\" \"C:/Users/nghia/Downloads/Log4.txt\")",
"Bash(grep -oE \"Plate: [^ ]+$\" \"C:/Users/nghia/Downloads/Log5.txt\")",
"Bash(awk -F'\\\\t' '{print $3}')",
"Bash(awk -F'\\\\t' '{print $3}' \"C:/Users/nghia/Downloads/AVNET-8845HS_twoconsoleapps.log\")",
"Bash(awk -F'\\\\t' '{print $3}' \"C:/Users/nghia/Downloads/AVNET-8845HS_issue.log\")",
"Bash(awk -F'\\\\t' '$2+0 >= 49 && $2+0 <= 52 {print NR\": \"$0}' \"C:/Users/nghia/Downloads/AVNET-8845HS_issue.log\")",
"Bash(awk -F'\\\\t' 'BEGIN{prev=0} {t=$2+0; if\\(t-prev>5\\){print NR\": GAP=\"t-prev\"s \"$0} prev=t}' \"C:/Users/nghia/Downloads/AVNET-8845HS3.log\")",
"Bash(awk -F'\\\\t' '{print $3}' \"C:/Users/nghia/Downloads/AVNET-8845HS3.log\")",
"Bash(awk -F'\\\\t' '{print $3}' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=20 && $2+0<=22' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=20.5 && $2+0<=21.5' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=21.0 && $2+0<=21.7' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=21.05 && $2+0<=21.23' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=17 && $2+0<=20' \"C:/Users/nghia/Downloads/AVNET-8845HS6.log\")",
"Bash(awk -F'\\\\t' '$2+0>=17.4 && $2+0<=17.7' \"C:/Users/nghia/Downloads/AVNET-8845HS6.log\")",
"Bash(awk -F'\\\\t' '$2+0>=12 && $2+0<=18' \"C:/Users/nghia/Downloads/AVNET-8845HS6.log\")",
"Bash(git -C \"C:/Projects/CLionProjects/ANSCORE\" status)",
"Bash(git -C \"C:/Projects/CLionProjects/ANSCORE\" diff --stat modules/ANSODEngine/ANSONNXCL.h modules/ANSODEngine/ANSONNXCL.cpp modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(awk -F'\\\\t' '{print $4}' \"/c/Users/nghia/Downloads/AVNET-8845HS7.log\")",
"Bash(\"C:/Users/nghia/AppData/Local/Programs/CLion 2026.1/bin/cmake/win/x64/bin/cmake.exe\" --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLPR-UnitTest -j 30)",
"Bash(cmd.exe /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" >NUL && \"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe\" --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLPR-UnitTest -j 30')",
"Bash(cmd.exe //c 'call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" >NUL && \"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe\" --build \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\" --target ANSLPR-UnitTest -j 30')",
"PowerShell($env:VS_VARS = '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\"'; $cmd = \"call $env:VS_VARS && `\"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe`\" --build `\"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release`\" --target ANSLPR-UnitTest -j 30\"; cmd.exe /c $cmd 2>&1 | Select-Object -Last 60)",
"PowerShell(cmd.exe /c $cmd 2>&1)",
"PowerShell($cmd = 'call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" >NUL && \"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe\" --build \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\" --target ANSLPR-UnitTest -j 30')",
"Bash(command -v dumpbin)",
"Bash(dumpbin /SYMBOLS \"/c/Projects/CLionProjects/ANSCORE/cmake-build-release/bin/ANSOCR.dll\")",
"Bash(\"/c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64/dumpbin.exe\" //SYMBOLS \"/c/Projects/CLionProjects/ANSCORE/cmake-build-release/bin/ANSOCR.dll\")",
"Bash(cmake --build . --target ANSODEngine -j 8)",
"PowerShell(@\\(Get-ChildItem -Path \"C:\\\\Program Files\\\\Microsoft Visual Studio\" -Filter \"vcvars64.bat\" -Recurse -ErrorAction SilentlyContinue\\) + @\\(Get-ChildItem -Path \"C:\\\\Program Files \\(x86\\)\\\\Microsoft Visual Studio\" -Filter \"vcvars64.bat\" -Recurse -ErrorAction SilentlyContinue\\) | ForEach-Object { $_.FullName })",
"PowerShell($d = Get-Item C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\bin\\\\ANSODEngine.dll; \"$\\($d.Name\\) | $\\($d.Length\\) bytes | $\\($d.LastWriteTime\\)\")",
"Bash(awk '{if \\($0 ~ /=0+$/\\) print \"ZERO\"; else print \"NONZERO\"}')",
"Bash(awk '{print $4}' \"C:\\\\Users\\\\nghia\\\\Downloads\\\\ANSAIBOX-Q1A01S3_memoryleak1.log\")",
"Bash(awk -F'\\\\t' 'NF>=4 && $3 ~ /^[0-9]+$/ {print $3}' \"C:\\\\Users\\\\nghia\\\\Downloads\\\\ANSAIBOX-Q1A01S3_memoryleak1.log\")",
"PowerShell($dll = \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\bin\\\\anslicensing.dll\"; if \\(Test-Path $dll\\) { Get-Process | Where-Object { try { $_.Modules | Where-Object { $_.FileName -eq $dll } } catch {} } | Select-Object Id, ProcessName } else { \"DLL not present at $dll\" })",
"PowerShell($p = \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\bin\\\\anslicensing.dll\"; try { $fs = [System.IO.File]::Open\\($p, 'Open', 'ReadWrite', 'None'\\); $fs.Close\\(\\); \"OK: writable, no other handle\" } catch { \"LOCKED: $\\($_.Exception.Message\\)\" })",
"PowerShell($candidates = @\\(\"handle64.exe\",\"handle.exe\"\\); $found = $null; foreach \\($c in $candidates\\) { $p = Get-Command $c -ErrorAction SilentlyContinue; if \\($p\\) { $found = $p.Source; break } }; if \\($found\\) { Write-Output \"FOUND: $found\"; & $found -nobanner -accepteula \"anslicensing.dll\" 2>$null } else { Write-Output \"handle.exe not in PATH\" })",
"PowerShell(Get-CimInstance Win32_Process -Filter \"ProcessId=22688\")",
"PowerShell(Get-CimInstance Win32_Process -Filter \"ParentProcessId=22688\")",
"Bash(taskkill //F //PID 50352)",
"Bash(awk -F'\\\\t' '/\\\\[TRT_Leak\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/\\\\[MEDIA_Leak\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/\\\\[MEDIA_Leak\\\\]/ {print $2, $4}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk 'NR==1 || NR%50==0 || NR>510')",
"Bash(awk -F'\\\\t' '/\\\\[TRT_Leak\\\\]/ {if \\($4 !~ /creates=0 destroys=0 net=0 cached=0/\\) print NR\": \"$0}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk '-F\\\\t' ' *)",
"Bash(awk -F'\\\\t' '/\\\\[TRT_Build\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/VRAM/ {print $2, $4}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk 'NR==1 || NR%2000==0')",
"Bash(awk -F'\\\\t' '/\\\\[ALPR_Track\\\\]|\\\\[ALPR_TrackId\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/\\\\[ANSCUSTOM_Infer\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(sort -t= -k2 -n -r)",
"Bash(awk -F'\\\\t' '/\\\\[ALPR_TrackId\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/\\\\[ANSMOT\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk '-F\\\\t' '/\\\\[ANSMOT\\\\] BYTETracker state/ { *)",
"Bash(awk -F'\\\\t' '/setInputShape/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/CUDA graph CAPTURE starting/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk '-F\\\\t' '/setInputShape/{ *)",
"Bash(python -m pip install reportlab --quiet)",
"Bash(python build_anscore_devguide_pdf.py)",
"Bash(python -c \"from pypdf import PdfReader; r = PdfReader\\(r'C:/Projects/CLionProjects/ANSCORE/ANSODEngine_Tracker_Stabilization_Guide.pdf'\\); print\\('pages:', len\\(r.pages\\)\\)\")",
"Bash(python -c \"import reportlab.pdfbase.pdfmetrics; print\\('pages: \\(pypdf not available — file size confirms\\)'\\)\")",
"Bash(python -m pip install pypdf --quiet)",
"Bash(python -c \"from pypdf import PdfReader; r = PdfReader\\(r'C:/Projects/CLionProjects/ANSCORE/ANSODEngine_Tracker_Stabilization_Guide.pdf'\\); print\\('pages:', len\\(r.pages\\)\\); print\\('first page text \\(head\\):', r.pages[0].extract_text\\(\\)[:300].replace\\(chr\\(10\\), ' '\\)\\)\")",
"Bash(awk -F'\\\\t' '{print NR}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' 'END {print \"lines:\", NR, \" duration_s:\", $2, \" rate_lines/s:\", NR/$2}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(grep -nE \"RunInference|RunInferencesBatch|infer\\\\\\\\\\(\\\\\\\\\\)|set_input_tensor|inference_request|m_inferRequest|mutex|lock_guard\" C:/Projects/CLionProjects/ANSCORE/engines/OpenVINOEngine/src/**/*.cpp C:/Projects/CLionProjects/ANSCORE/engines/OpenVINOEngine/include/**/*.h)",
"Bash(grep -nE \"DetectObjects|TryLockWithTimeout|ortRun|->Run\\\\\\\\\\(|m_ortEngine|infer|RunInference\" C:/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(grep -nE \"ONNXYOLO::detect|ort_session->Run|set_input|detect\\\\\\\\\\(.*const.*Mat|m_inputBuffer|m_input_tensor|allocator|ortInput|run_options|preprocess|postprocess|class ONNXYOLO\" C:/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/ANSONNXYOLO.cpp C:/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/ANSONNXYOLO.h)",
"Bash(ninja ANSODEngine)",
"PowerShell($env:Path = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Tools\\\\Llvm\\\\x64\\\\bin;\" + $env:Path; & \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 2>&1 | Out-Null; Get-Command ninja -ErrorAction SilentlyContinue | Select-Object Source)",
"PowerShell($ninjaPaths = @\\(\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\\\IDE\\\\CommonExtensions\\\\Microsoft\\\\CMake\\\\Ninja\\\\ninja.exe\", \"C:\\\\ProgramData\\\\chocolatey\\\\bin\\\\ninja.exe\", \"C:\\\\tools\\\\ninja\\\\ninja.exe\"\\); foreach \\($p in $ninjaPaths\\) { if \\(Test-Path $p\\) { Write-Output $p } })",
"PowerShell(cmd /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 >nul 2>&1 && cd /d C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release && \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\\\IDE\\\\CommonExtensions\\\\Microsoft\\\\CMake\\\\Ninja\\\\ninja.exe\" ANSODEngine 2>&1')",
"Bash(python build_anslpr_devguide_pdf.py)",
"Bash(where dumpbin.exe)",
"Bash(where llvm-symbolizer.exe)",
"Bash(where cdb.exe)",
"Bash(\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/llvm-symbolizer.exe\" --obj=\"C:/Projects/AIVISION/ANSFallDetection/ANSCustomFall/Dll1/x64/Release/ANSCustomFall.dll\" 0xb1d1)",
"Bash(cmake --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLicensingSystem -- /m)",
"Bash(cmake --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLicensingSystem)",
"Bash(cmd /c \"\\\\\"C:\\\\\\\\Program Files\\\\\\\\Microsoft Visual Studio\\\\\\\\2022\\\\\\\\Community\\\\\\\\VC\\\\\\\\Auxiliary\\\\\\\\Build\\\\\\\\vcvars64.bat\\\\\" >nul && cmake --build C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\cmake-build-release --target ANSLicensingSystem 2>&1\")",
"Bash(cmd /c \"call \\\\\"C:\\\\\\\\Program Files\\\\\\\\Microsoft Visual Studio\\\\\\\\2022\\\\\\\\Community\\\\\\\\VC\\\\\\\\Auxiliary\\\\\\\\Build\\\\\\\\vcvars64.bat\\\\\" && cmake --build C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\cmake-build-release --target ANSLicensingSystem\")",
"PowerShell($vcvars = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\"; cmd /c \"`\"$vcvars`\" && cmake --build C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release --target ANSLicensingSystem 2>&1\" 2>&1 | Select-Object -Last 120)",
"Bash(grep -rn \"^using namespace std\" \"C:/ANSLibs/anslicensing/\" 2>/dev/null | head -5; grep -rn \"^using namespace std\" \"C:/Projects/CLionProjects/ANSCORE/core/anslicensing/\" 2>/dev/null | head -5)",
"PowerShell(Start-Process powershell -Verb RunAs -ArgumentList '-NoProfile','-Command',\"try { Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace root\\\\wmi | Select-Object InstanceName,CurrentTemperature | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt } catch { '$_' | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt }\" -Wait; Get-Content C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt)",
"Bash(ls \"C:/Projects/AIVISION/RTM_POSE\")",
"Bash(ls C:/Projects/AIVISION/ANSLittering 2>&1 | head -50)",
"WebFetch(domain:www.example-code.com)",
"Bash(find / -name \"CkHttp.h\" 2>/dev/null | head; echo \"---\"; find / -name \"CkRest.h\" 2>/dev/null | head)",
"Bash(ls cmake-build-* build out)",
"Bash(ls CMakePresets.json)",
"Bash(cmake --build . --target ANSUtilities)",
"Bash(cmd /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" && cmake --build . --target ANSUtilities')",
"Bash(ls \"C:/Projects/CLionProjects/AIBOX\" 2>&1 | head -30)",
"mcp__desktop-commander__list_directory",
"mcp__desktop-commander__read_file",
"PowerShell(& \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\build_ansutil.bat\" 2>&1)",
"Bash(ls -la /c/Projects/ANSVIS 2>/dev/null || echo \"ANSVIS directory does not exist\")",
"PowerShell($cmake = \"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe\"; & $cmake --build \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release-clangcl\" --target ANSUtilities -j 8 2>&1 | Select-Object -Last 40)",
"Bash(cmake --build C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release-clangcl --target ANSODEngine -j 8)",
"Bash(cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release-clangcl\" --target ANSODEngine -j 8)",
"Bash(cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release-clangcl\" --target all -j 8)",
"Bash(cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" --target ANSODEngine -j 8)",
"Bash(cmd.exe /c 'call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 >nul && cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" --target ANSODEngine -j 8')",
"PowerShell($ErrorActionPreference = 'Continue'; cmd /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 >nul 2>&1 && set' | Where-Object { $_ -match '^\\(PATH|INCLUDE|LIB|LIBPATH|VC|WindowsSdk|UCRT|VSCMD_\\)' } | ForEach-Object { $name, $value = $_ -split '=', 2; [Environment]::SetEnvironmentVariable\\($name, $value\\) }; cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" --target ANSODEngine -j 8 2>&1 | Select-Object -Last 30)",
"Bash(sleep 20 && cat \"C:/Users/nghia/AppData/Local/Temp/clear-elevated.txt\" 2>&1 | grep -E \"\\(clear-flag|before clear|with clear|after clear|RESULT|EvtClearLog\\)\" | head -20)",
"Bash(ls \"C:\\\\Projects\\\\SharedCore\")",
"Bash(ls \"C:\\\\Projects\\\\SharedCore\\\\ep\")",
"PowerShell(Get-ChildItem \"C:\\\\Projects\\\\SharedCore\\\\ep\\\\directml\" | Select-Object Name, Length, @{n='Ver';e={$_.VersionInfo.FileVersion}})",
"PowerShell(& \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\tools\\\\Check-SharedCoreVersions.ps1\" -Root \"C:\\\\Projects\\\\SharedCore\" -CompareWith \"C:\\\\ProgramData\\\\ANSCENTER\\\\Shared\")",
"PowerShell(Write-Output \"=== SSH dir contents ===\"; if \\(Test-Path \"$env:USERPROFILE\\\\.ssh\"\\) { Get-ChildItem \"$env:USERPROFILE\\\\.ssh\" -Force | Select-Object Name, Length } else { Write-Output \"No .ssh directory\" }; Write-Output \"=== ssh-agent service ===\"; Get-Service ssh-agent | Select-Object Name, Status, StartType; Write-Output \"=== git remote ===\"; git -C C:\\\\Projects\\\\CLionProjects\\\\ANSCORE remote -v)",
"Read(//c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/**)",
"Read(//c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/bin/**)",
"Bash(tasklist)",
"Bash(echo \"=== Removing stale CMake caches \\(safe: regenerated by CLion\\) ===\" *)",
"Read(//c/ANSLibs/cudnn/**)",
"Read(//c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/include/**)",
"Read(//c/Program Files \\(x86\\)/Intel/oneAPI/**)",
"Read(//c/Program Files \\(x86\\)/**)",
"Bash('C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe' /SYMBOLS C:/ANSLibs/faiss/lib/faiss.lib)",
"Bash(winget --version)",
"Bash(winget search *)",
"Bash(winget install *)",
"Bash('C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe' -latest -property installationVersion)",
"Bash('C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe' -latest -property displayName)",
"Bash('C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe' -latest -property catalog_productDisplayVersion)",
"Bash(winget list *)",
"Bash(\"C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe\" -latest -property channelId)",
"Bash(./vs_installer.exe update *)",
"Bash(echo \"launch exit: $?\")",
"Bash('C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe' /ARCHIVEMEMBERS C:/ANSLibs/ODHUBYOLODarknet/lib/odhublib.lib)",
"Bash('C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe' /HEADERS C:/ANSLibs/ODHUBYOLODarknet/lib/odhublib.lib)",
"Read(//c/ANSLibs/TensorRT/lib/**)",
"Read(//c/ANSLibs/TensorRT/include/**)",
"Bash(ls -la --block-size=M nvinfer*.dll nvonnx*.dll)",
"Bash(stat -c '%n %s bytes' nvinfer*.dll nvonnx*.dll)",
"Bash(cd \"C:/Projects/SharedCore\" *)",
"Bash(cd \"C:/Projects/CLionProjects/ANSCORE\" *)",
"Read(//c/ANSLibs/**)",
"Read(//c/Windows/System32/**)",
"Bash(grep -niE \"ep/|ep\\\\\\\\\\\\\\\\|libraryDir|AddDllDirectory|SetDllDirectory|LoadLibrary|directml|openvino|cuda|cpu|subdir|\\\\\\\\\\\\\"ep\\\\\\\\\\\\\"|selectEP|chooseEP|provider\" EPLoader.cpp)",
"Bash(grep -viE \"^\\\\s*//|comment\")",
"Bash(echo \"=== Search for a 1.24 ONNX Runtime SDK \\(headers/lib\\) anywhere on disk ===\" *)",
"WebSearch",
"WebFetch(domain:github.com)",
"WebFetch(domain:raw.githubusercontent.com)",
"WebFetch(domain:onnxruntime.ai)",
"Bash([ -f \"$CM/OpenVINOTargets-debug.cmake\" ])",
"Bash(mv C:/ANSLibs/OpenVINO/runtime/cmake/OpenVINOTargets-debug.cmake C:/ANSLibs/OpenVINO/runtime/cmake/OpenVINOTargets-debug.cmake.disabled)",
"Bash(xargs -n1 basename)",
"PowerShell(cmd /c 'set \"PATH=C:\\\\ANSLibs\\\\Python311;C:\\\\ANSLibs\\\\Python311\\\\Scripts;%PATH%\" && call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" >nul && call \"C:\\\\ANSLibs\\\\OpenVINO\\\\setupvars.bat\" >nul && cd /d C:\\\\Projects\\\\onnxruntime-ov && build.bat --update --config Release --use_openvino GPU --build_shared_lib --skip_tests --cmake_generator \"Visual Studio 17 2022\" 2>&1')",
"Bash(ls \"C:/Program *)",
"Bash(\"C:/Program Files/CMake/bin/cmake.exe\" --version)",
"Read(//c/Program Files/JetBrains/CLion 2026.1.3/bin/ninja/win/x64/**)",
"Bash(\"C:/Program Files/JetBrains/CLion 2026.1.3/bin/ninja/win/x64/ninja.exe\" -C \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" -n)",
"Bash(sed -n '462,466p' core/ANSLibsLoader/EPLoader.cpp)",
"Bash(sed -n '576,581p' engines/ONNXEngine/ONNXEngine.cpp)",
"Bash(sed -n '63,66p' modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(awk 'NR>=1240 && NR<=1322 && /::|void |int |bool |Initialize|Detect|Run|LoadModel|Inference/{last=NR\": \"$0} END{}' modules/ANSODEngine/ANSFaceDetectorEngine.cpp)",
"Bash(grep -viE \"//|comment\")",
"Bash(sed -n '1,40p' tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp)",
"Bash(awk '/int main[ ]*\\\\\\(/{f=1} f{print NR\": \"$0} f&&/^}/{exit}' tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp)",
"Bash(cmd /c \"C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\tools\\\\\\\\_run_frtest.bat\")",
"Bash(mv __TRACKED_VAR__/ANSFR.dll.working __TRACKED_VAR__/ANSFR.dll)",
"Bash(md5sum __TRACKED_VAR__/ANSFR.dll)",
"Bash(BIN=\"C:/Projects/CLionProjects/ANSCORE/cmake-build-release/bin\" *)",
"Bash(echo \"=== first 30 lines \\(structure\\) ===\")",
"Read(//c/Users/Neo/Downloads/**)",
"Bash(echo \"\")",
"Bash(echo \"=== key error markers ===\")",
"Bash(sed -n '695,725p' modules/ANSODEngine/SCRFDFaceDetector.cpp)",
"Bash(sed -n '160,210p' modules/ANSODEngine/NV12PreprocessHelper.cpp)",
"Bash(sed -n '1,12p' modules/ANSODEngine/SCRFDFaceDetector.cpp)",
"Bash(sed -n '1,12p' modules/ANSODEngine/NV12PreprocessHelper.cpp)",
"Bash(sed -n '188,205p' modules/ANSODEngine/NV12PreprocessHelper.cpp)",
"Bash('C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/bin/cuobjdump.exe' C:/Projects/ANSTrainingTools/ODHUB/darknet/build/darknet/x64/odhublib.dll)"
]
}
}
+7
View File
@@ -6,6 +6,7 @@ Release/
out/ out/
# CLion / CMake # CLion / CMake
build/
.idea/ .idea/
cmake-build-debug/ cmake-build-debug/
cmake-build-release/ cmake-build-release/
@@ -42,3 +43,9 @@ ipch/
# Windows # Windows
Thumbs.db Thumbs.db
Desktop.ini Desktop.ini
# Claude Code per-user local settings (machine-specific permissions)
.claude/settings.local.json
# The key ceremony writes this; it carries the base private key and must
# never be committed. See ANSMINDCORE docs/model-package-keys.md.
core/ANSModelPackage/ANSModelKeysGenerated.h
+167
View File
@@ -0,0 +1,167 @@
Monocypher as a whole is dual-licensed. Choose whichever licence you
want from the two licences listed below.
The first licence is a regular 2-clause BSD licence. The second licence
is the CC-0 from Creative Commons. It is intended to release Monocypher
to the public domain. The BSD licence serves as a fallback option.
See the individual files for specific information about who contributed
to what file during which years. See below for special notes.
Licence 1 (2-clause BSD)
------------------------
Copyright (c) 2017-2023, Loup Vaillant
Copyright (c) 2017-2019, Michael Savage
Copyright (c) 2017-2023, Fabio Scotoni
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Licence 2 (CC-0)
----------------
> CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
> LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
> ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
> INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
> REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
> PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
> THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
> HEREUNDER.
### Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work
of authorship and/or a database (each, a "Work").
Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without
fear of later claims of infringement build upon, modify, incorporate in
other works, reuse and redistribute as freely as possible in any form
whatsoever and for any purposes, including without limitation commercial
purposes. These owners may contribute to the Commons to promote the
ideal of a free culture and the further production of creative, cultural
and scientific works, or to gain reputation or greater distribution for
their Work in part through the use and efforts of others.
For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or
she is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under
its terms, with knowledge of his or her Copyright and Related Rights in
the Work and the meaning and intended legal effect of CC0 on those
rights.
1. **Copyright and Related Rights.** A Work made available under CC0 may
be protected by copyright and related or neighboring rights
("Copyright and Related Rights"). Copyright and Related Rights
include, but are not limited to, the following:
- the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
- moral rights retained by the original author(s) and/or
performer(s); publicity and privacy rights pertaining to a person's
image or likeness depicted in a Work;
- rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
- rights protecting the extraction, dissemination, use and reuse of
data in a Work;
- database rights (such as those arising under Directive 96/9/EC of
the European Parliament and of the Council of 11 March 1996 on the
legal protection of databases, and under any national
implementation thereof, including any amended or successor version
of such directive); and
- other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.
2. **Waiver.** To the greatest extent permitted by, but not in
contravention of, applicable law, Affirmer hereby overtly, fully,
permanently, irrevocably and unconditionally waives, abandons, and
surrenders all of Affirmer's Copyright and Related Rights and
associated claims and causes of action, whether now known or unknown
(including existing as well as future claims and causes of action),
in the Work (i) in all territories worldwide, (ii) for the maximum
duration provided by applicable law or treaty (including future time
extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"Waiver"). Affirmer makes the Waiver for the benefit of each member
of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal
or equitable action to disrupt the quiet enjoyment of the Work by the
public as contemplated by Affirmer's express Statement of Purpose.
3. **Public License Fallback.** Should any part of the Waiver for any
reason be judged legally invalid or ineffective under applicable law,
then the Waiver shall be preserved to the maximum extent permitted
taking into account Affirmer's express Statement of Purpose. In
addition, to the extent the Waiver is so judged Affirmer hereby
grants to each affected person a royalty-free, non transferable, non
sublicensable, non exclusive, irrevocable and unconditional license
to exercise Affirmer's Copyright and Related Rights in the Work (i)
in all territories worldwide, (ii) for the maximum duration provided
by applicable law or treaty (including future time extensions), (iii)
in any current or future medium and for any number of copies, and
(iv) for any purpose whatsoever, including without limitation
commercial, advertising or promotional purposes (the "License"). The
License shall be deemed effective as of the date CC0 was applied by
Affirmer to the Work. Should any part of the License for any reason
be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the
remainder of the License, and in such case Affirmer hereby affirms
that he or she will not (i) exercise any of his or her remaining
Copyright and Related Rights in the Work or (ii) assert any
associated claims and causes of action with respect to the Work, in
either case contrary to Affirmer's express Statement of Purpose.
4. **Limitations and Disclaimers.**
- No trademark or patent rights held by Affirmer are waived,
abandoned, surrendered, licensed or otherwise affected by this
document.
- Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy,
or the present or absence of errors, whether or not discoverable,
all to the greatest extent permissible under applicable law.
- Affirmer disclaims responsibility for clearing rights of other
persons that may apply to the Work or any use thereof, including
without limitation any person's Copyright and Related Rights in the
Work. Further, Affirmer disclaims responsibility for obtaining any
necessary consents, permissions or other rights required for any
use of the Work.
- Affirmer understands and acknowledges that Creative Commons is not
a party to this document and has no duty or obligation with respect
to this CC0 or use of the Work.
+13
View File
@@ -0,0 +1,13 @@
# Monocypher 4.0.2 — vendored
Source: https://monocypher.org/download/monocypher-4.0.2.tar.gz
Files: `monocypher.c/.h`, `monocypher-ed25519.c/.h`, `LICENCE.md`. Unmodified.
Licence: dual 2-clause BSD / CC-0.
Here for the model package format (`core/ANSModelPackage`): X25519 to unwrap a
package's content key, and RFC 8032 Ed25519 to prove where the package came
from. The `-ed25519` pair is not optional — Monocypher's own EdDSA signs with
BLAKE2b, which would not interoperate with a package signed anywhere else.
ANSMINDCORE vendors the same release for the same reason. The two readers must
stay byte-compatible, so update them together and run both suites.
+500
View File
@@ -0,0 +1,500 @@
// Monocypher version 4.0.2
//
// This file is dual-licensed. Choose whichever licence you want from
// the two licences listed below.
//
// The first licence is a regular 2-clause BSD licence. The second licence
// is the CC-0 from Creative Commons. It is intended to release Monocypher
// to the public domain. The BSD licence serves as a fallback option.
//
// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
//
// ------------------------------------------------------------------------
//
// Copyright (c) 2017-2019, Loup Vaillant
// All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ------------------------------------------------------------------------
//
// Written in 2017-2019 by Loup Vaillant
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <https://creativecommons.org/publicdomain/zero/1.0/>
#include "monocypher-ed25519.h"
#ifdef MONOCYPHER_CPP_NAMESPACE
namespace MONOCYPHER_CPP_NAMESPACE {
#endif
/////////////////
/// Utilities ///
/////////////////
#define FOR(i, min, max) for (size_t i = min; i < max; i++)
#define COPY(dst, src, size) FOR(_i_, 0, size) (dst)[_i_] = (src)[_i_]
#define ZERO(buf, size) FOR(_i_, 0, size) (buf)[_i_] = 0
#define WIPE_CTX(ctx) crypto_wipe(ctx , sizeof(*(ctx)))
#define WIPE_BUFFER(buffer) crypto_wipe(buffer, sizeof(buffer))
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
typedef uint8_t u8;
typedef uint64_t u64;
// Returns the smallest positive integer y such that
// (x + y) % pow_2 == 0
// Basically, it's how many bytes we need to add to "align" x.
// Only works when pow_2 is a power of 2.
// Note: we use ~x+1 instead of -x to avoid compiler warnings
static size_t align(size_t x, size_t pow_2)
{
return (~x + 1) & (pow_2 - 1);
}
static u64 load64_be(const u8 s[8])
{
return((u64)s[0] << 56)
| ((u64)s[1] << 48)
| ((u64)s[2] << 40)
| ((u64)s[3] << 32)
| ((u64)s[4] << 24)
| ((u64)s[5] << 16)
| ((u64)s[6] << 8)
| (u64)s[7];
}
static void store64_be(u8 out[8], u64 in)
{
out[0] = (in >> 56) & 0xff;
out[1] = (in >> 48) & 0xff;
out[2] = (in >> 40) & 0xff;
out[3] = (in >> 32) & 0xff;
out[4] = (in >> 24) & 0xff;
out[5] = (in >> 16) & 0xff;
out[6] = (in >> 8) & 0xff;
out[7] = in & 0xff;
}
static void load64_be_buf (u64 *dst, const u8 *src, size_t size) {
FOR(i, 0, size) { dst[i] = load64_be(src + i*8); }
}
///////////////
/// SHA 512 ///
///////////////
static u64 rot(u64 x, int c ) { return (x >> c) | (x << (64 - c)); }
static u64 ch (u64 x, u64 y, u64 z) { return (x & y) ^ (~x & z); }
static u64 maj(u64 x, u64 y, u64 z) { return (x & y) ^ ( x & z) ^ (y & z); }
static u64 big_sigma0(u64 x) { return rot(x, 28) ^ rot(x, 34) ^ rot(x, 39); }
static u64 big_sigma1(u64 x) { return rot(x, 14) ^ rot(x, 18) ^ rot(x, 41); }
static u64 lit_sigma0(u64 x) { return rot(x, 1) ^ rot(x, 8) ^ (x >> 7); }
static u64 lit_sigma1(u64 x) { return rot(x, 19) ^ rot(x, 61) ^ (x >> 6); }
static const u64 K[80] = {
0x428a2f98d728ae22,0x7137449123ef65cd,0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc,
0x3956c25bf348b538,0x59f111f1b605d019,0x923f82a4af194f9b,0xab1c5ed5da6d8118,
0xd807aa98a3030242,0x12835b0145706fbe,0x243185be4ee4b28c,0x550c7dc3d5ffb4e2,
0x72be5d74f27b896f,0x80deb1fe3b1696b1,0x9bdc06a725c71235,0xc19bf174cf692694,
0xe49b69c19ef14ad2,0xefbe4786384f25e3,0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65,
0x2de92c6f592b0275,0x4a7484aa6ea6e483,0x5cb0a9dcbd41fbd4,0x76f988da831153b5,
0x983e5152ee66dfab,0xa831c66d2db43210,0xb00327c898fb213f,0xbf597fc7beef0ee4,
0xc6e00bf33da88fc2,0xd5a79147930aa725,0x06ca6351e003826f,0x142929670a0e6e70,
0x27b70a8546d22ffc,0x2e1b21385c26c926,0x4d2c6dfc5ac42aed,0x53380d139d95b3df,
0x650a73548baf63de,0x766a0abb3c77b2a8,0x81c2c92e47edaee6,0x92722c851482353b,
0xa2bfe8a14cf10364,0xa81a664bbc423001,0xc24b8b70d0f89791,0xc76c51a30654be30,
0xd192e819d6ef5218,0xd69906245565a910,0xf40e35855771202a,0x106aa07032bbd1b8,
0x19a4c116b8d2d0c8,0x1e376c085141ab53,0x2748774cdf8eeb99,0x34b0bcb5e19b48a8,
0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb,0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3,
0x748f82ee5defb2fc,0x78a5636f43172f60,0x84c87814a1f0ab72,0x8cc702081a6439ec,
0x90befffa23631e28,0xa4506cebde82bde9,0xbef9a3f7b2c67915,0xc67178f2e372532b,
0xca273eceea26619c,0xd186b8c721c0c207,0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178,
0x06f067aa72176fba,0x0a637dc5a2c898a6,0x113f9804bef90dae,0x1b710b35131c471b,
0x28db77f523047d84,0x32caab7b40c72493,0x3c9ebe0a15c9bebc,0x431d67c49c100d4c,
0x4cc5d4becb3e42b6,0x597f299cfc657e2a,0x5fcb6fab3ad6faec,0x6c44198c4a475817
};
static void sha512_compress(crypto_sha512_ctx *ctx)
{
u64 a = ctx->hash[0]; u64 b = ctx->hash[1];
u64 c = ctx->hash[2]; u64 d = ctx->hash[3];
u64 e = ctx->hash[4]; u64 f = ctx->hash[5];
u64 g = ctx->hash[6]; u64 h = ctx->hash[7];
FOR (j, 0, 16) {
u64 in = K[j] + ctx->input[j];
u64 t1 = big_sigma1(e) + ch (e, f, g) + h + in;
u64 t2 = big_sigma0(a) + maj(a, b, c);
h = g; g = f; f = e; e = d + t1;
d = c; c = b; b = a; a = t1 + t2;
}
size_t i16 = 0;
FOR(i, 1, 5) {
i16 += 16;
FOR (j, 0, 16) {
ctx->input[j] += lit_sigma1(ctx->input[(j- 2) & 15]);
ctx->input[j] += lit_sigma0(ctx->input[(j-15) & 15]);
ctx->input[j] += ctx->input[(j- 7) & 15];
u64 in = K[i16 + j] + ctx->input[j];
u64 t1 = big_sigma1(e) + ch (e, f, g) + h + in;
u64 t2 = big_sigma0(a) + maj(a, b, c);
h = g; g = f; f = e; e = d + t1;
d = c; c = b; b = a; a = t1 + t2;
}
}
ctx->hash[0] += a; ctx->hash[1] += b;
ctx->hash[2] += c; ctx->hash[3] += d;
ctx->hash[4] += e; ctx->hash[5] += f;
ctx->hash[6] += g; ctx->hash[7] += h;
}
// Write 1 input byte
static void sha512_set_input(crypto_sha512_ctx *ctx, u8 input)
{
size_t word = ctx->input_idx >> 3;
size_t byte = ctx->input_idx & 7;
ctx->input[word] |= (u64)input << (8 * (7 - byte));
}
// Increment a 128-bit "word".
static void sha512_incr(u64 x[2], u64 y)
{
x[1] += y;
if (x[1] < y) {
x[0]++;
}
}
void crypto_sha512_init(crypto_sha512_ctx *ctx)
{
ctx->hash[0] = 0x6a09e667f3bcc908;
ctx->hash[1] = 0xbb67ae8584caa73b;
ctx->hash[2] = 0x3c6ef372fe94f82b;
ctx->hash[3] = 0xa54ff53a5f1d36f1;
ctx->hash[4] = 0x510e527fade682d1;
ctx->hash[5] = 0x9b05688c2b3e6c1f;
ctx->hash[6] = 0x1f83d9abfb41bd6b;
ctx->hash[7] = 0x5be0cd19137e2179;
ctx->input_size[0] = 0;
ctx->input_size[1] = 0;
ctx->input_idx = 0;
ZERO(ctx->input, 16);
}
void crypto_sha512_update(crypto_sha512_ctx *ctx,
const u8 *message, size_t message_size)
{
// Avoid undefined NULL pointer increments with empty messages
if (message_size == 0) {
return;
}
// Align ourselves with word boundaries
if ((ctx->input_idx & 7) != 0) {
size_t nb_bytes = MIN(align(ctx->input_idx, 8), message_size);
FOR (i, 0, nb_bytes) {
sha512_set_input(ctx, message[i]);
ctx->input_idx++;
}
message += nb_bytes;
message_size -= nb_bytes;
}
// Align ourselves with block boundaries
if ((ctx->input_idx & 127) != 0) {
size_t nb_words = MIN(align(ctx->input_idx, 128), message_size) >> 3;
load64_be_buf(ctx->input + (ctx->input_idx >> 3), message, nb_words);
ctx->input_idx += nb_words << 3;
message += nb_words << 3;
message_size -= nb_words << 3;
}
// Compress block if needed
if (ctx->input_idx == 128) {
sha512_incr(ctx->input_size, 1024); // size is in bits
sha512_compress(ctx);
ctx->input_idx = 0;
ZERO(ctx->input, 16);
}
// Process the message block by block
FOR (i, 0, message_size >> 7) { // number of blocks
load64_be_buf(ctx->input, message, 16);
sha512_incr(ctx->input_size, 1024); // size is in bits
sha512_compress(ctx);
ctx->input_idx = 0;
ZERO(ctx->input, 16);
message += 128;
}
message_size &= 127;
if (message_size != 0) {
// Remaining words
size_t nb_words = message_size >> 3;
load64_be_buf(ctx->input, message, nb_words);
ctx->input_idx += nb_words << 3;
message += nb_words << 3;
message_size -= nb_words << 3;
// Remaining bytes
FOR (i, 0, message_size) {
sha512_set_input(ctx, message[i]);
ctx->input_idx++;
}
}
}
void crypto_sha512_final(crypto_sha512_ctx *ctx, u8 hash[64])
{
// Add padding bit
if (ctx->input_idx == 0) {
ZERO(ctx->input, 16);
}
sha512_set_input(ctx, 128);
// Update size
sha512_incr(ctx->input_size, ctx->input_idx * 8);
// Compress penultimate block (if any)
if (ctx->input_idx > 111) {
sha512_compress(ctx);
ZERO(ctx->input, 14);
}
// Compress last block
ctx->input[14] = ctx->input_size[0];
ctx->input[15] = ctx->input_size[1];
sha512_compress(ctx);
// Copy hash to output (big endian)
FOR (i, 0, 8) {
store64_be(hash + i*8, ctx->hash[i]);
}
WIPE_CTX(ctx);
}
void crypto_sha512(u8 hash[64], const u8 *message, size_t message_size)
{
crypto_sha512_ctx ctx;
crypto_sha512_init (&ctx);
crypto_sha512_update(&ctx, message, message_size);
crypto_sha512_final (&ctx, hash);
}
////////////////////
/// HMAC SHA 512 ///
////////////////////
void crypto_sha512_hmac_init(crypto_sha512_hmac_ctx *ctx,
const u8 *key, size_t key_size)
{
// hash key if it is too long
if (key_size > 128) {
crypto_sha512(ctx->key, key, key_size);
key = ctx->key;
key_size = 64;
}
// Compute inner key: padded key XOR 0x36
FOR (i, 0, key_size) { ctx->key[i] = key[i] ^ 0x36; }
FOR (i, key_size, 128) { ctx->key[i] = 0x36; }
// Start computing inner hash
crypto_sha512_init (&ctx->ctx);
crypto_sha512_update(&ctx->ctx, ctx->key, 128);
}
void crypto_sha512_hmac_update(crypto_sha512_hmac_ctx *ctx,
const u8 *message, size_t message_size)
{
crypto_sha512_update(&ctx->ctx, message, message_size);
}
void crypto_sha512_hmac_final(crypto_sha512_hmac_ctx *ctx, u8 hmac[64])
{
// Finish computing inner hash
crypto_sha512_final(&ctx->ctx, hmac);
// Compute outer key: padded key XOR 0x5c
FOR (i, 0, 128) {
ctx->key[i] ^= 0x36 ^ 0x5c;
}
// Compute outer hash
crypto_sha512_init (&ctx->ctx);
crypto_sha512_update(&ctx->ctx, ctx->key , 128);
crypto_sha512_update(&ctx->ctx, hmac, 64);
crypto_sha512_final (&ctx->ctx, hmac); // outer hash
WIPE_CTX(ctx);
}
void crypto_sha512_hmac(u8 hmac[64], const u8 *key, size_t key_size,
const u8 *message, size_t message_size)
{
crypto_sha512_hmac_ctx ctx;
crypto_sha512_hmac_init (&ctx, key, key_size);
crypto_sha512_hmac_update(&ctx, message, message_size);
crypto_sha512_hmac_final (&ctx, hmac);
}
////////////////////
/// HKDF SHA 512 ///
////////////////////
void crypto_sha512_hkdf_expand(u8 *okm, size_t okm_size,
const u8 *prk, size_t prk_size,
const u8 *info, size_t info_size)
{
int not_first = 0;
u8 ctr = 1;
u8 blk[64];
while (okm_size > 0) {
size_t out_size = MIN(okm_size, sizeof(blk));
crypto_sha512_hmac_ctx ctx;
crypto_sha512_hmac_init(&ctx, prk , prk_size);
if (not_first) {
// For some reason HKDF uses some kind of CBC mode.
// For some reason CTR mode alone wasn't enough.
// Like what, they didn't trust HMAC in 2010? Really??
crypto_sha512_hmac_update(&ctx, blk , sizeof(blk));
}
crypto_sha512_hmac_update(&ctx, info, info_size);
crypto_sha512_hmac_update(&ctx, &ctr, 1);
crypto_sha512_hmac_final(&ctx, blk);
COPY(okm, blk, out_size);
not_first = 1;
okm += out_size;
okm_size -= out_size;
ctr++;
}
}
void crypto_sha512_hkdf(u8 *okm , size_t okm_size,
const u8 *ikm , size_t ikm_size,
const u8 *salt, size_t salt_size,
const u8 *info, size_t info_size)
{
// Extract
u8 prk[64];
crypto_sha512_hmac(prk, salt, salt_size, ikm, ikm_size);
// Expand
crypto_sha512_hkdf_expand(okm, okm_size, prk, sizeof(prk), info, info_size);
}
///////////////
/// Ed25519 ///
///////////////
void crypto_ed25519_key_pair(u8 secret_key[64], u8 public_key[32], u8 seed[32])
{
u8 a[64];
COPY(a, seed, 32); // a[ 0..31] = seed
crypto_wipe(seed, 32);
COPY(secret_key, a, 32); // secret key = seed
crypto_sha512(a, a, 32); // a[ 0..31] = scalar
crypto_eddsa_trim_scalar(a, a); // a[ 0..31] = trimmed scalar
crypto_eddsa_scalarbase(public_key, a); // public key = [trimmed scalar]B
COPY(secret_key + 32, public_key, 32); // secret key includes public half
WIPE_BUFFER(a);
}
static void hash_reduce(u8 h[32],
const u8 *a, size_t a_size,
const u8 *b, size_t b_size,
const u8 *c, size_t c_size,
const u8 *d, size_t d_size)
{
u8 hash[64];
crypto_sha512_ctx ctx;
crypto_sha512_init (&ctx);
crypto_sha512_update(&ctx, a, a_size);
crypto_sha512_update(&ctx, b, b_size);
crypto_sha512_update(&ctx, c, c_size);
crypto_sha512_update(&ctx, d, d_size);
crypto_sha512_final (&ctx, hash);
crypto_eddsa_reduce(h, hash);
}
static void ed25519_dom_sign(u8 signature [64], const u8 secret_key[32],
const u8 *dom, size_t dom_size,
const u8 *message, size_t message_size)
{
u8 a[64]; // secret scalar and prefix
u8 r[32]; // secret deterministic "random" nonce
u8 h[32]; // publically verifiable hash of the message (not wiped)
u8 R[32]; // first half of the signature (allows overlapping inputs)
const u8 *pk = secret_key + 32;
crypto_sha512(a, secret_key, 32);
crypto_eddsa_trim_scalar(a, a);
hash_reduce(r, dom, dom_size, a + 32, 32, message, message_size, 0, 0);
crypto_eddsa_scalarbase(R, r);
hash_reduce(h, dom, dom_size, R, 32, pk, 32, message, message_size);
COPY(signature, R, 32);
crypto_eddsa_mul_add(signature + 32, h, a, r);
WIPE_BUFFER(a);
WIPE_BUFFER(r);
}
void crypto_ed25519_sign(u8 signature [64], const u8 secret_key[64],
const u8 *message, size_t message_size)
{
ed25519_dom_sign(signature, secret_key, 0, 0, message, message_size);
}
int crypto_ed25519_check(const u8 signature[64], const u8 public_key[32],
const u8 *msg, size_t msg_size)
{
u8 h_ram[32];
hash_reduce(h_ram, signature, 32, public_key, 32, msg, msg_size, 0, 0);
return crypto_eddsa_check_equation(signature, public_key, h_ram);
}
static const u8 domain[34] = "SigEd25519 no Ed25519 collisions\1";
void crypto_ed25519_ph_sign(uint8_t signature[64], const uint8_t secret_key[64],
const uint8_t message_hash[64])
{
ed25519_dom_sign(signature, secret_key, domain, sizeof(domain),
message_hash, 64);
}
int crypto_ed25519_ph_check(const uint8_t sig[64], const uint8_t pk[32],
const uint8_t msg_hash[64])
{
u8 h_ram[32];
hash_reduce(h_ram, domain, sizeof(domain), sig, 32, pk, 32, msg_hash, 64);
return crypto_eddsa_check_equation(sig, pk, h_ram);
}
#ifdef MONOCYPHER_CPP_NAMESPACE
}
#endif
+140
View File
@@ -0,0 +1,140 @@
// Monocypher version 4.0.2
//
// This file is dual-licensed. Choose whichever licence you want from
// the two licences listed below.
//
// The first licence is a regular 2-clause BSD licence. The second licence
// is the CC-0 from Creative Commons. It is intended to release Monocypher
// to the public domain. The BSD licence serves as a fallback option.
//
// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
//
// ------------------------------------------------------------------------
//
// Copyright (c) 2017-2019, Loup Vaillant
// All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ------------------------------------------------------------------------
//
// Written in 2017-2019 by Loup Vaillant
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <https://creativecommons.org/publicdomain/zero/1.0/>
#ifndef ED25519_H
#define ED25519_H
#include "monocypher.h"
#ifdef MONOCYPHER_CPP_NAMESPACE
namespace MONOCYPHER_CPP_NAMESPACE {
#elif defined(__cplusplus)
extern "C" {
#endif
////////////////////////
/// Type definitions ///
////////////////////////
// Do not rely on the size or content on any of those types,
// they may change without notice.
typedef struct {
uint64_t hash[8];
uint64_t input[16];
uint64_t input_size[2];
size_t input_idx;
} crypto_sha512_ctx;
typedef struct {
uint8_t key[128];
crypto_sha512_ctx ctx;
} crypto_sha512_hmac_ctx;
// SHA 512
// -------
void crypto_sha512_init (crypto_sha512_ctx *ctx);
void crypto_sha512_update(crypto_sha512_ctx *ctx,
const uint8_t *message, size_t message_size);
void crypto_sha512_final (crypto_sha512_ctx *ctx, uint8_t hash[64]);
void crypto_sha512(uint8_t hash[64],
const uint8_t *message, size_t message_size);
// SHA 512 HMAC
// ------------
void crypto_sha512_hmac_init(crypto_sha512_hmac_ctx *ctx,
const uint8_t *key, size_t key_size);
void crypto_sha512_hmac_update(crypto_sha512_hmac_ctx *ctx,
const uint8_t *message, size_t message_size);
void crypto_sha512_hmac_final(crypto_sha512_hmac_ctx *ctx, uint8_t hmac[64]);
void crypto_sha512_hmac(uint8_t hmac[64],
const uint8_t *key , size_t key_size,
const uint8_t *message, size_t message_size);
// SHA 512 HKDF
// ------------
void crypto_sha512_hkdf_expand(uint8_t *okm, size_t okm_size,
const uint8_t *prk, size_t prk_size,
const uint8_t *info, size_t info_size);
void crypto_sha512_hkdf(uint8_t *okm , size_t okm_size,
const uint8_t *ikm , size_t ikm_size,
const uint8_t *salt, size_t salt_size,
const uint8_t *info, size_t info_size);
// Ed25519
// -------
// Signatures (EdDSA with curve25519 + SHA-512)
// --------------------------------------------
void crypto_ed25519_key_pair(uint8_t secret_key[64],
uint8_t public_key[32],
uint8_t seed[32]);
void crypto_ed25519_sign(uint8_t signature [64],
const uint8_t secret_key[64],
const uint8_t *message, size_t message_size);
int crypto_ed25519_check(const uint8_t signature [64],
const uint8_t public_key[32],
const uint8_t *message, size_t message_size);
// Pre-hash variants
void crypto_ed25519_ph_sign(uint8_t signature [64],
const uint8_t secret_key [64],
const uint8_t message_hash[64]);
int crypto_ed25519_ph_check(const uint8_t signature [64],
const uint8_t public_key [32],
const uint8_t message_hash[64]);
#ifdef __cplusplus
}
#endif
#endif // ED25519_H
+2956
View File
File diff suppressed because it is too large Load Diff
+321
View File
@@ -0,0 +1,321 @@
// Monocypher version 4.0.2
//
// This file is dual-licensed. Choose whichever licence you want from
// the two licences listed below.
//
// The first licence is a regular 2-clause BSD licence. The second licence
// is the CC-0 from Creative Commons. It is intended to release Monocypher
// to the public domain. The BSD licence serves as a fallback option.
//
// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
//
// ------------------------------------------------------------------------
//
// Copyright (c) 2017-2019, Loup Vaillant
// All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ------------------------------------------------------------------------
//
// Written in 2017-2019 by Loup Vaillant
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <https://creativecommons.org/publicdomain/zero/1.0/>
#ifndef MONOCYPHER_H
#define MONOCYPHER_H
#include <stddef.h>
#include <stdint.h>
#ifdef MONOCYPHER_CPP_NAMESPACE
namespace MONOCYPHER_CPP_NAMESPACE {
#elif defined(__cplusplus)
extern "C" {
#endif
// Constant time comparisons
// -------------------------
// Return 0 if a and b are equal, -1 otherwise
int crypto_verify16(const uint8_t a[16], const uint8_t b[16]);
int crypto_verify32(const uint8_t a[32], const uint8_t b[32]);
int crypto_verify64(const uint8_t a[64], const uint8_t b[64]);
// Erase sensitive data
// --------------------
void crypto_wipe(void *secret, size_t size);
// Authenticated encryption
// ------------------------
void crypto_aead_lock(uint8_t *cipher_text,
uint8_t mac [16],
const uint8_t key [32],
const uint8_t nonce[24],
const uint8_t *ad, size_t ad_size,
const uint8_t *plain_text, size_t text_size);
int crypto_aead_unlock(uint8_t *plain_text,
const uint8_t mac [16],
const uint8_t key [32],
const uint8_t nonce[24],
const uint8_t *ad, size_t ad_size,
const uint8_t *cipher_text, size_t text_size);
// Authenticated stream
// --------------------
typedef struct {
uint64_t counter;
uint8_t key[32];
uint8_t nonce[8];
} crypto_aead_ctx;
void crypto_aead_init_x(crypto_aead_ctx *ctx,
const uint8_t key[32], const uint8_t nonce[24]);
void crypto_aead_init_djb(crypto_aead_ctx *ctx,
const uint8_t key[32], const uint8_t nonce[8]);
void crypto_aead_init_ietf(crypto_aead_ctx *ctx,
const uint8_t key[32], const uint8_t nonce[12]);
void crypto_aead_write(crypto_aead_ctx *ctx,
uint8_t *cipher_text,
uint8_t mac[16],
const uint8_t *ad , size_t ad_size,
const uint8_t *plain_text, size_t text_size);
int crypto_aead_read(crypto_aead_ctx *ctx,
uint8_t *plain_text,
const uint8_t mac[16],
const uint8_t *ad , size_t ad_size,
const uint8_t *cipher_text, size_t text_size);
// General purpose hash (BLAKE2b)
// ------------------------------
// Direct interface
void crypto_blake2b(uint8_t *hash, size_t hash_size,
const uint8_t *message, size_t message_size);
void crypto_blake2b_keyed(uint8_t *hash, size_t hash_size,
const uint8_t *key, size_t key_size,
const uint8_t *message, size_t message_size);
// Incremental interface
typedef struct {
// Do not rely on the size or contents of this type,
// for they may change without notice.
uint64_t hash[8];
uint64_t input_offset[2];
uint64_t input[16];
size_t input_idx;
size_t hash_size;
} crypto_blake2b_ctx;
void crypto_blake2b_init(crypto_blake2b_ctx *ctx, size_t hash_size);
void crypto_blake2b_keyed_init(crypto_blake2b_ctx *ctx, size_t hash_size,
const uint8_t *key, size_t key_size);
void crypto_blake2b_update(crypto_blake2b_ctx *ctx,
const uint8_t *message, size_t message_size);
void crypto_blake2b_final(crypto_blake2b_ctx *ctx, uint8_t *hash);
// Password key derivation (Argon2)
// --------------------------------
#define CRYPTO_ARGON2_D 0
#define CRYPTO_ARGON2_I 1
#define CRYPTO_ARGON2_ID 2
typedef struct {
uint32_t algorithm; // Argon2d, Argon2i, Argon2id
uint32_t nb_blocks; // memory hardness, >= 8 * nb_lanes
uint32_t nb_passes; // CPU hardness, >= 1 (>= 3 recommended for Argon2i)
uint32_t nb_lanes; // parallelism level (single threaded anyway)
} crypto_argon2_config;
typedef struct {
const uint8_t *pass;
const uint8_t *salt;
uint32_t pass_size;
uint32_t salt_size; // 16 bytes recommended
} crypto_argon2_inputs;
typedef struct {
const uint8_t *key; // may be NULL if no key
const uint8_t *ad; // may be NULL if no additional data
uint32_t key_size; // 0 if no key (32 bytes recommended otherwise)
uint32_t ad_size; // 0 if no additional data
} crypto_argon2_extras;
extern const crypto_argon2_extras crypto_argon2_no_extras;
void crypto_argon2(uint8_t *hash, uint32_t hash_size, void *work_area,
crypto_argon2_config config,
crypto_argon2_inputs inputs,
crypto_argon2_extras extras);
// Key exchange (X-25519)
// ----------------------
// Shared secrets are not quite random.
// Hash them to derive an actual shared key.
void crypto_x25519_public_key(uint8_t public_key[32],
const uint8_t secret_key[32]);
void crypto_x25519(uint8_t raw_shared_secret[32],
const uint8_t your_secret_key [32],
const uint8_t their_public_key [32]);
// Conversion to EdDSA
void crypto_x25519_to_eddsa(uint8_t eddsa[32], const uint8_t x25519[32]);
// scalar "division"
// Used for OPRF. Be aware that exponential blinding is less secure
// than Diffie-Hellman key exchange.
void crypto_x25519_inverse(uint8_t blind_salt [32],
const uint8_t private_key[32],
const uint8_t curve_point[32]);
// "Dirty" versions of x25519_public_key().
// Use with crypto_elligator_rev().
// Leaks 3 bits of the private key.
void crypto_x25519_dirty_small(uint8_t pk[32], const uint8_t sk[32]);
void crypto_x25519_dirty_fast (uint8_t pk[32], const uint8_t sk[32]);
// Signatures
// ----------
// EdDSA with curve25519 + BLAKE2b
void crypto_eddsa_key_pair(uint8_t secret_key[64],
uint8_t public_key[32],
uint8_t seed[32]);
void crypto_eddsa_sign(uint8_t signature [64],
const uint8_t secret_key[64],
const uint8_t *message, size_t message_size);
int crypto_eddsa_check(const uint8_t signature [64],
const uint8_t public_key[32],
const uint8_t *message, size_t message_size);
// Conversion to X25519
void crypto_eddsa_to_x25519(uint8_t x25519[32], const uint8_t eddsa[32]);
// EdDSA building blocks
void crypto_eddsa_trim_scalar(uint8_t out[32], const uint8_t in[32]);
void crypto_eddsa_reduce(uint8_t reduced[32], const uint8_t expanded[64]);
void crypto_eddsa_mul_add(uint8_t r[32],
const uint8_t a[32],
const uint8_t b[32],
const uint8_t c[32]);
void crypto_eddsa_scalarbase(uint8_t point[32], const uint8_t scalar[32]);
int crypto_eddsa_check_equation(const uint8_t signature[64],
const uint8_t public_key[32],
const uint8_t h_ram[32]);
// Chacha20
// --------
// Specialised hash.
// Used to hash X25519 shared secrets.
void crypto_chacha20_h(uint8_t out[32],
const uint8_t key[32],
const uint8_t in [16]);
// Unauthenticated stream cipher.
// Don't forget to add authentication.
uint64_t crypto_chacha20_djb(uint8_t *cipher_text,
const uint8_t *plain_text,
size_t text_size,
const uint8_t key[32],
const uint8_t nonce[8],
uint64_t ctr);
uint32_t crypto_chacha20_ietf(uint8_t *cipher_text,
const uint8_t *plain_text,
size_t text_size,
const uint8_t key[32],
const uint8_t nonce[12],
uint32_t ctr);
uint64_t crypto_chacha20_x(uint8_t *cipher_text,
const uint8_t *plain_text,
size_t text_size,
const uint8_t key[32],
const uint8_t nonce[24],
uint64_t ctr);
// Poly 1305
// ---------
// This is a *one time* authenticator.
// Disclosing the mac reveals the key.
// See crypto_lock() on how to use it properly.
// Direct interface
void crypto_poly1305(uint8_t mac[16],
const uint8_t *message, size_t message_size,
const uint8_t key[32]);
// Incremental interface
typedef struct {
// Do not rely on the size or contents of this type,
// for they may change without notice.
uint8_t c[16]; // chunk of the message
size_t c_idx; // How many bytes are there in the chunk.
uint32_t r [4]; // constant multiplier (from the secret key)
uint32_t pad[4]; // random number added at the end (from the secret key)
uint32_t h [5]; // accumulated hash
} crypto_poly1305_ctx;
void crypto_poly1305_init (crypto_poly1305_ctx *ctx, const uint8_t key[32]);
void crypto_poly1305_update(crypto_poly1305_ctx *ctx,
const uint8_t *message, size_t message_size);
void crypto_poly1305_final (crypto_poly1305_ctx *ctx, uint8_t mac[16]);
// Elligator 2
// -----------
// Elligator mappings proper
void crypto_elligator_map(uint8_t curve [32], const uint8_t hidden[32]);
int crypto_elligator_rev(uint8_t hidden[32], const uint8_t curve [32],
uint8_t tweak);
// Easy to use key pair generation
void crypto_elligator_key_pair(uint8_t hidden[32], uint8_t secret_key[32],
uint8_t seed[32]);
#ifdef __cplusplus
}
#endif
#endif // MONOCYPHER_H
+27 -5
View File
@@ -1,5 +1,11 @@
cmake_minimum_required(VERSION 3.28) cmake_minimum_required(VERSION 3.28)
cmake_policy(SET CMP0167 NEW) # Suppress FindBoost removal warning # CMP0167 (FindBoost removed from CMake) only exists from 3.30, and this
# project asks for 3.28 — so setting it unconditionally makes every older
# CMake fail at line two with "policy is not known to this version". Guarded,
# a newer CMake still gets the quiet FindBoost it was added for.
if(POLICY CMP0167)
cmake_policy(SET CMP0167 NEW)
endif()
# ── CUDA is optional (not available on macOS) ─────────────────── # ── CUDA is optional (not available on macOS) ───────────────────
include(CheckLanguage) include(CheckLanguage)
@@ -17,10 +23,14 @@ if(CMAKE_CUDA_COMPILER)
set(CMAKE_CUDA_ARCHITECTURES set(CMAKE_CUDA_ARCHITECTURES
75-real 80-real 86-real 87-real 88-real 89-real 90-real 100-real 120-real 75-real 80-real 86-real 87-real 88-real 89-real 90-real 100-real 120-real
75-virtual) 75-virtual)
# --use_fast_math, with underscores: that is nvcc's own spelling. Older
# toolkits also accepted the hyphenated form; CUDA 13 does not, and every
# .cu file fails with "Unknown option '--use-fast-math'" before it compiles
# a line.
if(MSVC) if(MSVC)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math -Xcompiler=/utf-8") set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use_fast_math -Xcompiler=/utf-8")
else() else()
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math") set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use_fast_math")
endif() endif()
else() else()
project(ANSCORE LANGUAGES C CXX) project(ANSCORE LANGUAGES C CXX)
@@ -154,7 +164,12 @@ include(cmake/Dependencies.cmake)
# Core — licensing & infrastructure # Core — licensing & infrastructure
add_subdirectory(core/anslicensing) add_subdirectory(core/anslicensing)
add_subdirectory(core/ANSLicensingSystem) add_subdirectory(core/ANSLicensingSystem)
add_subdirectory(core/ANSLibsLoader) # ANSLibsLoader dynamically binds cudart/cuBLAS/nvJPEG at runtime, but it still
# compiles against <cuda_runtime.h> for their types and links CUDA::cudart_static.
if(ANSCORE_HAS_CUDA)
add_subdirectory(core/ANSLibsLoader)
endif()
add_subdirectory(core/ANSModelPackage)
# Modules — feature DLLs (cross-platform) # Modules — feature DLLs (cross-platform)
add_subdirectory(modules/ANSMOT) add_subdirectory(modules/ANSMOT)
@@ -177,11 +192,18 @@ if(ANSCORE_HAS_CUDA)
add_subdirectory(modules/ANSOCR) add_subdirectory(modules/ANSOCR)
add_subdirectory(modules/ANSFR) add_subdirectory(modules/ANSFR)
add_subdirectory(modules/ANSLPR) add_subdirectory(modules/ANSLPR)
# ANSCV hosts NVDEC decode, the NV12 GPU frame pool and the nvJPEG encoder:
# ANSGpuFrameOps.h includes <cuda_runtime.h> unconditionally and calls
# cudaMemcpy2D/cudaMallocPitch directly, so it needs the toolkit like the
# engine modules do.
add_subdirectory(modules/ANSCV)
endif() endif()
add_subdirectory(modules/ANSCV)
# Facade # Facade
add_subdirectory(ANSLIB) add_subdirectory(ANSLIB)
# Tests # Tests
# enable_testing() (not include(CTest), which would add Nightly/Experimental
# targets) so the add_test() calls under tests/ reach ctest.
enable_testing()
add_subdirectory(tests) add_subdirectory(tests)
+259 -47
View File
@@ -98,6 +98,16 @@ namespace {
return s.substr(0, 4) + std::string(s.size() - 8, '*') + s.substr(s.size() - 4); return s.substr(0, 4) + std::string(s.size() - 8, '*') + s.substr(s.size() - 4);
} }
// The HWID emitted when WMI returns no hardware components at all (every
// hash field is the "absent" marker). Every PC with broken WMI produces
// this exact string, so it identifies "WMI broken" in customer logs.
constexpr const char* kEmptyFingerprintHWID = "ESWJJ-LEEFC-CCTBB-JSSWJ-JLEEF";
constexpr const char* kEmptyFingerprintWarning =
"Hardware fingerprint is EMPTY - WMI returned no hardware components on this PC. "
"Licensing proceeds but the license is weakly bound to this machine. "
"Repair WMI: run 'winmgmt /verifyrepository' (then 'winmgmt /salvagerepository' if inconsistent).";
// SDK identifier passed to ANSLSHelper for licensing-service auth. Despite // SDK identifier passed to ANSLSHelper for licensing-service auth. Despite
// the variable name "privateKey" used at call sites, this is an 11-byte // the variable name "privateKey" used at call sites, this is an 11-byte
// SDK token that identifies the ANSCENTER product to the licensing // SDK token that identifies the ANSCENTER product to the licensing
@@ -690,6 +700,7 @@ struct GpuAdapter {
DWORD vendorId{0}; DWORD vendorId{0};
std::string name; // UTF-8 std::string name; // UTF-8
int64_t dedicatedVramMB{0}; int64_t dedicatedVramMB{0};
int64_t sharedSysMemMB{0}; // iGPUs report ~0 dedicated; this is their pool
bool isDiscrete{false}; // heuristic: dedicated VRAM ≥ 1 GiB bool isDiscrete{false}; // heuristic: dedicated VRAM ≥ 1 GiB
// running totals — reset every Sample() // running totals — reset every Sample()
@@ -697,6 +708,19 @@ struct GpuAdapter {
int64_t processMemMB{0}; int64_t processMemMB{0};
}; };
// Vendor name in the spelling OpenCL uses, for adapters OpenCL did not
// report. Callers that string-match the JSON "vendor" field see the same
// text whichever source filled it in.
const char* OpenClStyleVendor(DWORD vendorId) {
switch (vendorId) {
case 0x10DE: return "NVIDIA";
case 0x8086: return "Intel Corporation";
case 0x1002: // fallthrough — AMD and some AMD APUs
case 0x1022: return "Advanced Micro Devices, Inc.";
default: return "";
}
}
const char* VendorString(DWORD vendorId) { const char* VendorString(DWORD vendorId) {
switch (vendorId) { switch (vendorId) {
case 0x10DE: return "NVIDIA"; case 0x10DE: return "NVIDIA";
@@ -737,6 +761,7 @@ std::vector<GpuAdapter> EnumerateGpuAdapters() {
a.vendorId = desc.VendorId; a.vendorId = desc.VendorId;
a.name = WideToUtf8(desc.Description); a.name = WideToUtf8(desc.Description);
a.dedicatedVramMB = static_cast<int64_t>(desc.DedicatedVideoMemory / (1024ULL * 1024ULL)); a.dedicatedVramMB = static_cast<int64_t>(desc.DedicatedVideoMemory / (1024ULL * 1024ULL));
a.sharedSysMemMB = static_cast<int64_t>(desc.SharedSystemMemory / (1024ULL * 1024ULL));
a.isDiscrete = a.dedicatedVramMB >= 1024; // ≥ 1 GiB ⇒ probably dGPU a.isDiscrete = a.dedicatedVramMB >= 1024; // ≥ 1 GiB ⇒ probably dGPU
out.push_back(std::move(a)); out.push_back(std::move(a));
} }
@@ -747,6 +772,66 @@ std::vector<GpuAdapter> EnumerateGpuAdapters() {
return out; return out;
} }
// Driver version for a DXGI adapter.
//
// DXGI does not expose it, and WMI (Win32_VideoController) costs a COM/WMI
// round trip. The display-adapter class key has it: every installed adapter
// gets a numbered subkey holding DriverDesc — the Device Manager name, which
// is the same text DXGI returns as Description — plus DriverVersion.
// Returns "" when the adapter has no match (rare: a driver installed after
// the key was enumerated, or a name mismatch), which is what the JSON then
// reports rather than a fabricated value.
std::string GpuDriverVersion(const std::string& description) {
if (description.empty()) return {};
static const wchar_t* kDisplayClassKey =
L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}";
HKEY classKey = nullptr;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, kDisplayClassKey, 0,
KEY_READ, &classKey) != ERROR_SUCCESS) {
ANS_DBG("HWInfo", "GpuDriverVersion: cannot open display class key");
return {};
}
std::string version;
constexpr DWORD kSubNameChars = 256;
wchar_t subName[kSubNameChars];
for (DWORD i = 0; version.empty(); ++i) {
DWORD subLen = kSubNameChars;
if (RegEnumKeyExW(classKey, i, subName, &subLen,
nullptr, nullptr, nullptr, nullptr) != ERROR_SUCCESS)
break; // ERROR_NO_MORE_ITEMS — done
HKEY adapterKey = nullptr;
if (RegOpenKeyExW(classKey, subName, 0, KEY_READ, &adapterKey) != ERROR_SUCCESS)
continue;
auto readString = [&adapterKey](const wchar_t* value) -> std::string {
constexpr DWORD kBufChars = 512;
wchar_t buf[kBufChars];
DWORD len = sizeof(buf);
DWORD type = 0;
if (RegQueryValueExW(adapterKey, value, nullptr, &type,
reinterpret_cast<LPBYTE>(buf), &len) != ERROR_SUCCESS ||
type != REG_SZ)
return {};
// RegQueryValueExW does not guarantee NUL termination.
const DWORD chars = len / sizeof(wchar_t);
buf[chars < kBufChars ? chars : kBufChars - 1] = L'\0';
return WideToUtf8(buf);
};
if (readString(L"DriverDesc") == description)
version = readString(L"DriverVersion");
RegCloseKey(adapterKey);
}
RegCloseKey(classKey);
return version;
}
bool ParseLuidFromInstance(const wchar_t* inst, LUID& out) { bool ParseLuidFromInstance(const wchar_t* inst, LUID& out) {
if (!inst) return false; if (!inst) return false;
const wchar_t* p = wcsstr(inst, L"luid_0x"); const wchar_t* p = wcsstr(inst, L"luid_0x");
@@ -1361,32 +1446,67 @@ namespace ANSCENTER
} }
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// GPU — classify all adapters by vendor string // GPU — classify all adapters by PCI vendor ID.
//
// Enumerated over DXGI, not OpenCL. hwinfo::getAllGPUs() is an
// OpenCL device list (the prebuilt HWiNFO.lib has no
// Win32_VideoController path), so it reports nothing on a machine
// whose driver did not register an OpenCL ICD — and we would then
// silently drop a perfectly good CUDA card down to the CPU engine.
// DXGI only needs a WDDM display driver, which any usable GPU has.
//
// Vendor IDs are exact, so no substring matching and none of its
// traps (e.g. "ATI" matching inside "CORPOR-ATI-ON").
// ---------------------------------------------------------------- // ----------------------------------------------------------------
auto gpus = hwinfo::getAllGPUs(); auto adapters = EnumerateGpuAdapters();
ANS_DBG("HWInfo", "Found %zu GPU adapter(s)", gpus.size()); ANS_DBG("HWInfo", "DXGI: found %zu GPU adapter(s)", adapters.size());
for (auto& gpu : gpus) { for (const auto& a : adapters) {
std::string vendor = gpu.vendor(); ANS_DBG("HWInfo", "GPU: name=%s vendorId=0x%04X vram=%lldMiB discrete=%d",
ANS_DBG("HWInfo", "GPU: name=%s vendor=%s", gpu.name().c_str(), vendor.c_str()); a.name.c_str(), a.vendorId,
std::cout << "[HWInfo] Detected GPU: " << gpu.name() << " (Vendor: " << vendor << ")" << std::endl; static_cast<long long>(a.dedicatedVramMB), a.isDiscrete ? 1 : 0);
// Normalize to uppercase for reliable substring matching std::cout << "[HWInfo] Detected GPU: " << a.name
std::transform(vendor.begin(), vendor.end(), << " (Vendor: " << VendorString(a.vendorId) << ")" << std::endl;
vendor.begin(), ::toupper);
if (vendor.find("NVIDIA") != std::string::npos) switch (a.vendorId) {
gpuNvidiaAvailable = true; case 0x10DE: gpuNvidiaAvailable = true; break; // NVIDIA
if (vendor.find("INTEL") != std::string::npos) case 0x1002: gpuAMDAvailable = true; break; // AMD / ATI
gpuOpenVINOAvailable = true; case 0x1022: gpuAMDAvailable = true; break; // AMD (some APUs)
// AMD cards report as "AMD", "Advanced Micro Devices", or case 0x8086: gpuOpenVINOAvailable = true; break; // Intel
// legacy "ATI Technologies" branding. default: break;
// NOTE: "ATI" alone is too broad — "INTEL CORPORATION" contains }
// "ATI" inside "CORPOR-ATI-ON", causing a false positive. }
// Use "ATI " (with trailing space) to match "ATI Technologies"
// or "ATI Radeon" without matching "CORPORATION". // DXGI came back empty — no WDDM driver at all, or a session with no
if (vendor.find("AMD") != std::string::npos || // graphics stack. Fall back to the OpenCL list rather than assuming
vendor.find("ADVANCED MICRO") != std::string::npos || // there is no GPU.
vendor.find("ATI ") != std::string::npos) if (adapters.empty()) {
gpuAMDAvailable = true; auto gpus = hwinfo::getAllGPUs();
ANS_DBG("HWInfo", "DXGI empty; OpenCL fallback found %zu device(s)", gpus.size());
for (auto& gpu : gpus) {
std::string vendor = gpu.vendor();
ANS_DBG("HWInfo", "OpenCL GPU: name=%s vendor=%s",
gpu.name().c_str(), vendor.c_str());
std::cout << "[HWInfo] Detected GPU (OpenCL): " << gpu.name()
<< " (Vendor: " << vendor << ")" << std::endl;
// Normalize to uppercase for reliable substring matching
std::transform(vendor.begin(), vendor.end(),
vendor.begin(), ::toupper);
if (vendor.find("NVIDIA") != std::string::npos)
gpuNvidiaAvailable = true;
if (vendor.find("INTEL") != std::string::npos)
gpuOpenVINOAvailable = true;
// AMD cards report as "AMD", "Advanced Micro Devices", or
// legacy "ATI Technologies" branding.
// NOTE: "ATI" alone is too broad — "INTEL CORPORATION" contains
// "ATI" inside "CORPOR-ATI-ON", causing a false positive.
// Use "ATI " (with trailing space) to match "ATI Technologies"
// or "ATI Radeon" without matching "CORPORATION".
if (vendor.find("AMD") != std::string::npos ||
vendor.find("ADVANCED MICRO") != std::string::npos ||
vendor.find("ATI ") != std::string::npos)
gpuAMDAvailable = true;
}
} }
ANS_DBG("HWInfo", "Detection: cpuIntel=%d gpuNvidia=%d gpuAMD=%d gpuOpenVINO=%d", ANS_DBG("HWInfo", "Detection: cpuIntel=%d gpuNvidia=%d gpuAMD=%d gpuOpenVINO=%d",
cpuIntelAvailable, gpuNvidiaAvailable, gpuAMDAvailable, gpuOpenVINOAvailable); cpuIntelAvailable, gpuNvidiaAvailable, gpuAMDAvailable, gpuOpenVINOAvailable);
@@ -1657,10 +1777,19 @@ namespace ANSCENTER
registrationName = "ANSTS"; registrationName = "ANSTS";
productId = 1008; productId = 1008;
} }
else { else if (productName == "ANSMIND") {//1009
registrationName = productName; registrationName = "ANSMIND";
productId = 1009; productId = 1009;
} }
else if (productName == "ANSGW") {//1010
registrationName = "ANSGW";
productId = 1010;
}
else {
// Unknown product: refuse rather than mint a trial under some real product's id.
std::cout << "ActivateTrialLicense: unknown product name '" << productName << "'" << std::endl;
return 0;
}
std::string trialLicenseKey = GetTrialLicenseKey(registrationName); std::string trialLicenseKey = GetTrialLicenseKey(registrationName);
if (trialLicenseKey.empty()) { if (trialLicenseKey.empty()) {
@@ -1729,6 +1858,12 @@ namespace ANSCENTER
case ANSCENTER::ANSPRODUCTS::ANS_TS://1008 case ANSCENTER::ANSPRODUCTS::ANS_TS://1008
_productName = "ANSTS"; _productName = "ANSTS";
break; break;
case ANSCENTER::ANSPRODUCTS::ANSMIND://1009
_productName = "ANSMIND";
break;
case ANSCENTER::ANSPRODUCTS::ANSGW://1010
_productName = "ANSGW";
break;
} }
if (_productName == productName) { if (_productName == productName) {
// Delete the license file // Delete the license file
@@ -1776,6 +1911,12 @@ namespace ANSCENTER
else if (productName == "ANSTS") {//1008 else if (productName == "ANSTS") {//1008
registrationName = "ANSTS"; registrationName = "ANSTS";
} }
else if (productName == "ANSMIND") {//1009
registrationName = "ANSMIND";
}
else if (productName == "ANSGW") {//1010
registrationName = "ANSGW";
}
else { else {
registrationName = productName; registrationName = productName;
} }
@@ -1816,6 +1957,12 @@ namespace ANSCENTER
else if (productName == "ANSTS") {//1008 else if (productName == "ANSTS") {//1008
registrationName = "ANSTS"; registrationName = "ANSTS";
} }
else if (productName == "ANSMIND") {//1009
registrationName = "ANSMIND";
}
else if (productName == "ANSGW") {//1010
registrationName = "ANSGW";
}
else { else {
registrationName = productName; registrationName = productName;
} }
@@ -1882,6 +2029,8 @@ namespace ANSCENTER
ansLicenseCollection.push_back("ANSALPR");//1006 ansLicenseCollection.push_back("ANSALPR");//1006
ansLicenseCollection.push_back("ANSCV");//1007 ansLicenseCollection.push_back("ANSCV");//1007
ansLicenseCollection.push_back("ANSTS");//1008 ansLicenseCollection.push_back("ANSTS");//1008
ansLicenseCollection.push_back("ANSMIND");//1009
ansLicenseCollection.push_back("ANSGW");//1010
for (auto& element : ansLicenseCollection) { for (auto& element : ansLicenseCollection) {
boost::property_tree::ptree dataNode; boost::property_tree::ptree dataNode;
@@ -1944,6 +2093,12 @@ namespace ANSCENTER
case ANSCENTER::ANSPRODUCTS::ANS_TS: case ANSCENTER::ANSPRODUCTS::ANS_TS:
_productName = "ANSTS"; _productName = "ANSTS";
break; break;
case ANSCENTER::ANSPRODUCTS::ANSMIND:
_productName = "ANSMIND";
break;
case ANSCENTER::ANSPRODUCTS::ANSGW:
_productName = "ANSGW";
break;
} }
if (_productName == element) { if (_productName == element) {
_registrationName = ANSUtility::DecodeBase64Utility(_validationData.c_str());// Decode _registrationName = ANSUtility::DecodeBase64Utility(_validationData.c_str());// Decode
@@ -2021,10 +2176,18 @@ namespace ANSCENTER
registrationName = "ANSTS"; registrationName = "ANSTS";
productId = 1008; productId = 1008;
} }
else { else if (ansLicenseInfo.productName == "ANSMIND") {
registrationName = ansLicenseInfo.productName; registrationName = "ANSMIND";
productId = 1009; productId = 1009;
} }
else if (ansLicenseInfo.productName == "ANSGW") {
registrationName = "ANSGW";
productId = 1010;
}
else {
// Unknown product: skip the row rather than report it under some real product's id.
continue;
}
dataNode.put("product_name", ansLicenseInfo.productName); dataNode.put("product_name", ansLicenseInfo.productName);
std::string trialLicenseKey = GetTrialLicenseKey(registrationName); std::string trialLicenseKey = GetTrialLicenseKey(registrationName);
if (trialLicenseKey.empty()) { if (trialLicenseKey.empty()) {
@@ -2166,7 +2329,7 @@ namespace ANSCENTER
} }
void ANSLSHelper::LoadLicense(std::string licenseKey) { void ANSLSHelper::LoadLicense(std::string licenseKey) {
if (!this->_license != NULL) delete this->_license; if (this->_license != NULL) delete this->_license;
this->_license = new License(); this->_license = new License();
std::string licenseFileName = Dash2Underscore(licenseKey)+".json"; std::string licenseFileName = Dash2Underscore(licenseKey)+".json";
std::string licenseFolder = GetLicenseDir(); std::string licenseFolder = GetLicenseDir();
@@ -2812,6 +2975,9 @@ namespace ANSCENTER
std::wstring _registrationName = String2WString(registrationName); std::wstring _registrationName = String2WString(registrationName);
std::wstring hardwareId = KeyHelper::GetCurrentHardwareId(); std::wstring hardwareId = KeyHelper::GetCurrentHardwareId();
std::string _hardwareId = WString2String(hardwareId); std::string _hardwareId = WString2String(hardwareId);
if (_hardwareId == kEmptyFingerprintHWID) {
this->_logger.LogWarn("ANSLSHelper::ValidateLicenseKey", kEmptyFingerprintWarning, __FILE__, __LINE__);
}
bool _overrideLicenseFile = false; bool _overrideLicenseFile = false;
if (activationKey.empty()) { if (activationKey.empty()) {
if (this->_license != NULL) {// get it from there if (this->_license != NULL) {// get it from there
@@ -2888,14 +3054,13 @@ namespace ANSCENTER
case STATUS_INVALID_HARDWARE_ID: case STATUS_INVALID_HARDWARE_ID:
_licenseKeyStatus = INVALID_HARDWARE_ID; _licenseKeyStatus = INVALID_HARDWARE_ID;
activationStatusStr = "Hardware id does not match this computer. current HWID is " + _hardwareId + activationStatusStr = "Local hardware fingerprint check rejected this computer (hardware changed, or WMI returned no components). Current HWID is " + _hardwareId +
"; client HWID is " + WString2String(licensingClient.GetHardwareId()) + "; stored activation key is " + mask_secret(WString2String(_activationKey)) +
"; Current activation key is " + WString2String(_activationKey) + "; server activation key is " + mask_secret(WString2String(licensingClient.GetActivationKey())) +
"; client Activation key is " + WString2String(licensingClient.GetActivationKey()) +
"; URL is " + WString2String(this->_licenseServiceURL.c_str())+ "; URL is " + WString2String(this->_licenseServiceURL.c_str())+
"; ProductId is " + WString2String(wProductId) + "; ProductId is " + WString2String(wProductId) +
"; RegistrationName is " + WString2String(_registrationName) + "; RegistrationName is " + WString2String(_registrationName) +
"; Status is" + std::to_string(activationStatus); "; Status is " + std::to_string(activationStatus);
break; break;
case STATUS_LICENSE_EXPIRED: case STATUS_LICENSE_EXPIRED:
@@ -2969,7 +3134,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) { if (_overrideLicenseFile) {
this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__); this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__);
if (!this->_license != NULL) delete this->_license; if (this->_license != NULL) delete this->_license;
this->_license = new License(); this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str()); this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen); this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -2996,7 +3161,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) { if (_overrideLicenseFile) {
// Save license // Save license
this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__); this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__);
if (!this->_license != NULL) delete this->_license; if (this->_license != NULL) delete this->_license;
this->_license = new License(); this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str()); this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen); this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3026,6 +3191,9 @@ namespace ANSCENTER
std::wstring _registrationName = String2WString(registrationName); std::wstring _registrationName = String2WString(registrationName);
std::string _hardwareId = customHWID; std::string _hardwareId = customHWID;
std::wstring hardwareId = String2WString(customHWID); std::wstring hardwareId = String2WString(customHWID);
if (_hardwareId == kEmptyFingerprintHWID) {
this->_logger.LogWarn("ANSLSHelper::ValidateLicenseWithCustomHWID", kEmptyFingerprintWarning, __FILE__, __LINE__);
}
bool _overrideLicenseFile = false; bool _overrideLicenseFile = false;
if (activationKey.empty()) { if (activationKey.empty()) {
if (this->_license != NULL) {// get it from there if (this->_license != NULL) {// get it from there
@@ -3102,14 +3270,13 @@ namespace ANSCENTER
case STATUS_INVALID_HARDWARE_ID: case STATUS_INVALID_HARDWARE_ID:
_licenseKeyStatus = INVALID_HARDWARE_ID; _licenseKeyStatus = INVALID_HARDWARE_ID;
activationStatusStr = "Hardware id does not match this computer. current HWID is " + _hardwareId + activationStatusStr = "Local hardware fingerprint check rejected this computer (hardware changed, or WMI returned no components). Current HWID is " + _hardwareId +
"; client HWID is " + WString2String(licensingClient.GetHardwareId()) + "; stored activation key is " + mask_secret(WString2String(_activationKey)) +
"; Current activation key is " + WString2String(_activationKey) + "; server activation key is " + mask_secret(WString2String(licensingClient.GetActivationKey())) +
"; client Activation key is " + WString2String(licensingClient.GetActivationKey()) +
"; URL is " + WString2String(this->_licenseServiceURL.c_str()) + "; URL is " + WString2String(this->_licenseServiceURL.c_str()) +
"; ProductId is " + WString2String(wProductId) + "; ProductId is " + WString2String(wProductId) +
"; RegistrationName is " + WString2String(_registrationName) + "; RegistrationName is " + WString2String(_registrationName) +
"; Status is" + std::to_string(activationStatus); "; Status is " + std::to_string(activationStatus);
break; break;
case STATUS_LICENSE_EXPIRED: case STATUS_LICENSE_EXPIRED:
@@ -3183,7 +3350,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) { if (_overrideLicenseFile) {
this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__); this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__);
if (!this->_license != NULL) delete this->_license; if (this->_license != NULL) delete this->_license;
this->_license = new License(); this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str()); this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen); this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3210,7 +3377,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) { if (_overrideLicenseFile) {
// Save license // Save license
this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__); this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__);
if (!this->_license != NULL) delete this->_license; if (this->_license != NULL) delete this->_license;
this->_license = new License(); this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str()); this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen); this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3496,7 +3663,7 @@ namespace ANSCENTER
return 0; // invalid return 0; // invalid
} }
else { else {
if (!this->_license != NULL) delete this->_license; if (this->_license != NULL) delete this->_license;
this->_license = new License(); this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str()); this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen); this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3873,9 +4040,25 @@ namespace ANSCENTER
osNode.put("endianess", os.isLittleEndian() ? "little endian" : "big endian"); osNode.put("endianess", os.isLittleEndian() ? "little endian" : "big endian");
root.add_child("OS", osNode); root.add_child("OS", osNode);
// GPU information // GPU information.
auto gpus = hwinfo::getAllGPUs(); //
for (auto& gpu : gpus) { // The OpenCL loop below is unchanged — same keys, same order,
// same values — so on a machine that already reported its GPUs
// the JSON is byte-identical to before.
//
// But hwinfo::getAllGPUs() *is* an OpenCL device list, and it
// reports nothing when no vendor registered an OpenCL ICD. That
// is how this field ended up as "" on machines that plainly have
// a GPU. So afterwards we append any DXGI adapter OpenCL did not
// already cover: DXGI needs only a WDDM display driver, so it
// sees the adapter either way. Nothing is appended when OpenCL
// works, which is why the identical case stays identical.
auto clDevices = hwinfo::getAllGPUs();
auto adapters = EnumerateGpuAdapters();
ANS_DBG("HWInfo", "GetSystemHardwareInformation: OpenCL %zu device(s), DXGI %zu adapter(s)",
clDevices.size(), adapters.size());
for (auto& gpu : clDevices) {
boost::property_tree::ptree gpuNodeChild; boost::property_tree::ptree gpuNodeChild;
gpuNodeChild.put("GPU", gpu.id()); gpuNodeChild.put("GPU", gpu.id());
gpuNodeChild.put("vendor", gpu.vendor()); gpuNodeChild.put("vendor", gpu.vendor());
@@ -3886,6 +4069,35 @@ namespace ANSCENTER
gpuNode.push_back(std::make_pair("", gpuNodeChild)); gpuNode.push_back(std::make_pair("", gpuNodeChild));
} }
for (const auto& a : adapters) {
bool covered = false;
for (const auto& d : clDevices) {
if (d.name() == a.name) { covered = true; break; }
}
if (covered) continue;
ANS_DBG("HWInfo", "GPU not seen by OpenCL, adding from DXGI: name=%s vendorId=0x%04X",
a.name.c_str(), a.vendorId);
// Dedicated VRAM, or the shared pool for an iGPU. An iGPU
// carves out a token amount of dedicated memory (128 MiB is
// typical) and works out of system RAM, so reporting the
// carve-out would badly understate it.
const double memMiB = static_cast<double>(
a.dedicatedVramMB >= 512 ? a.dedicatedVramMB : a.sharedSysMemMB);
const char* vendor = OpenClStyleVendor(a.vendorId);
boost::property_tree::ptree gpuNodeChild;
gpuNodeChild.put("GPU", 0); // hwinfo never set a per-device id either
gpuNodeChild.put("vendor", std::string(*vendor ? vendor : VendorString(a.vendorId)));
gpuNodeChild.put("model", a.name);
gpuNodeChild.put("diver_version", GpuDriverVersion(a.name));
gpuNodeChild.put("memory[MiB]", memMiB);
// DXGI exposes no clock; 0 rather than a fabricated number.
gpuNodeChild.put("frequency", static_cast<int64_t>(0));
gpuNode.push_back(std::make_pair("", gpuNodeChild));
}
root.add_child("GPU", gpuNode); root.add_child("GPU", gpuNode);
+3 -1
View File
@@ -259,7 +259,9 @@ namespace ANSCENTER
ANS_OCR = 1005, ANS_OCR = 1005,
ANS_ALPR =1006, ANS_ALPR =1006,
ANS_CV = 1007, ANS_CV = 1007,
ANS_TS = 1008 ANS_TS = 1008,
ANSMIND = 1009,
ANSGW = 1010
}; };
enum ANSLICENSESTATUS { enum ANSLICENSESTATUS {
ACTIVE = 0, ACTIVE = 0,
+1 -1
View File
@@ -54,7 +54,7 @@ target_link_libraries(ANSLicensingSystem PRIVATE
shlwapi.lib winhttp.lib shlwapi.lib winhttp.lib
pdh.lib psapi.lib pdh.lib psapi.lib
wbemuuid.lib oleaut32.lib ole32.lib wbemuuid.lib oleaut32.lib ole32.lib
dxgi.lib dxgi.lib advapi32.lib
dnsapi.lib dnsapi.lib
${WIN_COMMON_LIBS} ${WIN_COMMON_LIBS}
) )
+314
View File
@@ -0,0 +1,314 @@
#ifndef ANSAEAD_H
#define ANSAEAD_H
#pragma once
// ANSAead — the AEAD the model package format is sealed with (RFC 8439).
//
// KEPT IN STEP WITH ANSMINDCORE's src/core/chacha20poly1305.hpp — the same
// algorithm, byte for byte, because a package written by one product must open
// in the other. Change them together or not at all.
//
// WHY IN-TREE, and why this cipher. A versioned crypto dependency on every
// platform we ship, for a primitive with published test vectors, is a worse
// trade than the algorithm itself. ChaCha20-Poly1305 is the one modern AEAD
// that is a few
// hundred lines of 32-bit arithmetic with no tables, no S-boxes and no
// timing-sensitive lookups — AES-GCM needs either hardware instructions or a
// GHASH that is easy to get subtly wrong. The suite checks this against the
// RFC's own vectors, so it is verified rather than merely present.
//
// NOT a general crypto library. It seals and opens model packages. The nonce
// rule is the caller's to keep: a key and nonce pair must never encrypt twice,
// which the package format satisfies by deriving the nonce from a random
// per-payload base and the chunk index.
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
namespace ANSCENTER {
namespace aead {
// ── ChaCha20 (RFC 8439 §2.4) ──────────────────────────────────────────────
namespace detail {
inline uint32_t rotl32(uint32_t v, int n) { return (v << n) | (v >> (32 - n)); }
inline uint32_t load32(const uint8_t* p) {
return uint32_t(p[0]) | (uint32_t(p[1]) << 8) | (uint32_t(p[2]) << 16) |
(uint32_t(p[3]) << 24);
}
inline void store32(uint8_t* p, uint32_t v) {
p[0] = uint8_t(v);
p[1] = uint8_t(v >> 8);
p[2] = uint8_t(v >> 16);
p[3] = uint8_t(v >> 24);
}
inline void chachaBlock(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], uint8_t out[64]) {
static const char kSigma[] = "expand 32-byte k";
uint32_t s[16];
s[0] = load32(reinterpret_cast<const uint8_t*>(kSigma));
s[1] = load32(reinterpret_cast<const uint8_t*>(kSigma) + 4);
s[2] = load32(reinterpret_cast<const uint8_t*>(kSigma) + 8);
s[3] = load32(reinterpret_cast<const uint8_t*>(kSigma) + 12);
for (int i = 0; i < 8; ++i) s[4 + i] = load32(key + 4 * i);
s[12] = counter;
for (int i = 0; i < 3; ++i) s[13 + i] = load32(nonce + 4 * i);
uint32_t x[16];
std::memcpy(x, s, sizeof(x));
auto qr = [&x](int a, int b, int c, int d) {
x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 16);
x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 12);
x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 8);
x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 7);
};
for (int i = 0; i < 10; ++i) { // 20 rounds = 10 double rounds
qr(0, 4, 8, 12); qr(1, 5, 9, 13); qr(2, 6, 10, 14); qr(3, 7, 11, 15);
qr(0, 5, 10, 15); qr(1, 6, 11, 12); qr(2, 7, 8, 13); qr(3, 4, 9, 14);
}
for (int i = 0; i < 16; ++i) store32(out + 4 * i, x[i] + s[i]);
}
// XOR a buffer with the keystream, starting at `counter`.
inline void chacha20Xor(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], const uint8_t* in, size_t n,
uint8_t* out) {
uint8_t block[64];
size_t done = 0;
while (done < n) {
chachaBlock(key, counter++, nonce, block);
const size_t take = (n - done < 64) ? (n - done) : 64;
for (size_t i = 0; i < take; ++i) out[done + i] = in[done + i] ^ block[i];
done += take;
}
}
// ── Poly1305 (RFC 8439 §2.5), 130-bit accumulator in 26-bit limbs ─────────
class Poly1305 {
public:
explicit Poly1305(const uint8_t key[32]) {
r_[0] = (load32(key) ) & 0x3ffffff;
r_[1] = (load32(key + 3) >> 2) & 0x3ffff03;
r_[2] = (load32(key + 6) >> 4) & 0x3ffc0ff;
r_[3] = (load32(key + 9) >> 6) & 0x3f03fff;
r_[4] = (load32(key + 12) >> 8) & 0x00fffff;
for (int i = 0; i < 4; ++i) pad_[i] = load32(key + 16 + 4 * i);
}
void update(const uint8_t* m, size_t n) {
while (n > 0) {
const size_t take = (16 - bufLen_ < n) ? (16 - bufLen_) : n;
std::memcpy(buf_ + bufLen_, m, take);
bufLen_ += take;
m += take;
n -= take;
if (bufLen_ == 16) {
block(buf_, false);
bufLen_ = 0;
}
}
}
void finish(uint8_t tag[16]) {
if (bufLen_ > 0) {
buf_[bufLen_++] = 1;
while (bufLen_ < 16) buf_[bufLen_++] = 0;
block(buf_, true);
}
// Carry propagation, then the conditional subtraction of p = 2^130-5.
uint32_t c;
c = h_[1] >> 26; h_[1] &= 0x3ffffff; h_[2] += c;
c = h_[2] >> 26; h_[2] &= 0x3ffffff; h_[3] += c;
c = h_[3] >> 26; h_[3] &= 0x3ffffff; h_[4] += c;
c = h_[4] >> 26; h_[4] &= 0x3ffffff; h_[0] += c * 5;
c = h_[0] >> 26; h_[0] &= 0x3ffffff; h_[1] += c;
uint32_t g[5];
g[0] = h_[0] + 5; c = g[0] >> 26; g[0] &= 0x3ffffff;
g[1] = h_[1] + c; c = g[1] >> 26; g[1] &= 0x3ffffff;
g[2] = h_[2] + c; c = g[2] >> 26; g[2] &= 0x3ffffff;
g[3] = h_[3] + c; c = g[3] >> 26; g[3] &= 0x3ffffff;
g[4] = h_[4] + c - (uint32_t(1) << 26);
uint32_t mask = (g[4] >> 31) - 1; // all ones when g >= p
for (int i = 0; i < 5; ++i) g[i] &= mask;
mask = ~mask;
for (int i = 0; i < 5; ++i) h_[i] = (h_[i] & mask) | g[i];
// h modulo 2^128, as four 32-bit words. The narrowing is deliberate: each
// word takes the low 32 bits of two limbs, and the only carry between
// words is the one the pad addition produces. Keeping the spilled limb
// bits in a 64-bit accumulator instead counts them twice — which is a
// tag that verifies against itself and against nothing else.
const uint32_t w0 = uint32_t((h_[0]) | (h_[1] << 26));
const uint32_t w1 = uint32_t((h_[1] >> 6) | (h_[2] << 20));
const uint32_t w2 = uint32_t((h_[2] >> 12) | (h_[3] << 14));
const uint32_t w3 = uint32_t((h_[3] >> 18) | (h_[4] << 8));
uint64_t f = uint64_t(w0) + pad_[0];
store32(tag, uint32_t(f));
f = uint64_t(w1) + pad_[1] + (f >> 32);
store32(tag + 4, uint32_t(f));
f = uint64_t(w2) + pad_[2] + (f >> 32);
store32(tag + 8, uint32_t(f));
f = uint64_t(w3) + pad_[3] + (f >> 32);
store32(tag + 12, uint32_t(f));
}
private:
void block(const uint8_t m[16], bool last) {
const uint32_t hibit = last ? 0 : (uint32_t(1) << 24);
h_[0] += (load32(m) ) & 0x3ffffff;
h_[1] += (load32(m + 3) >> 2) & 0x3ffffff;
h_[2] += (load32(m + 6) >> 4) & 0x3ffffff;
h_[3] += (load32(m + 9) >> 6) & 0x3ffffff;
h_[4] += (load32(m + 12) >> 8) | hibit;
const uint32_t s1 = r_[1] * 5, s2 = r_[2] * 5, s3 = r_[3] * 5, s4 = r_[4] * 5;
uint64_t d0 = uint64_t(h_[0]) * r_[0] + uint64_t(h_[1]) * s4 +
uint64_t(h_[2]) * s3 + uint64_t(h_[3]) * s2 +
uint64_t(h_[4]) * s1;
uint64_t d1 = uint64_t(h_[0]) * r_[1] + uint64_t(h_[1]) * r_[0] +
uint64_t(h_[2]) * s4 + uint64_t(h_[3]) * s3 +
uint64_t(h_[4]) * s2;
uint64_t d2 = uint64_t(h_[0]) * r_[2] + uint64_t(h_[1]) * r_[1] +
uint64_t(h_[2]) * r_[0] + uint64_t(h_[3]) * s4 +
uint64_t(h_[4]) * s3;
uint64_t d3 = uint64_t(h_[0]) * r_[3] + uint64_t(h_[1]) * r_[2] +
uint64_t(h_[2]) * r_[1] + uint64_t(h_[3]) * r_[0] +
uint64_t(h_[4]) * s4;
uint64_t d4 = uint64_t(h_[0]) * r_[4] + uint64_t(h_[1]) * r_[3] +
uint64_t(h_[2]) * r_[2] + uint64_t(h_[3]) * r_[1] +
uint64_t(h_[4]) * r_[0];
uint32_t c = uint32_t(d0 >> 26); h_[0] = uint32_t(d0) & 0x3ffffff;
d1 += c; c = uint32_t(d1 >> 26); h_[1] = uint32_t(d1) & 0x3ffffff;
d2 += c; c = uint32_t(d2 >> 26); h_[2] = uint32_t(d2) & 0x3ffffff;
d3 += c; c = uint32_t(d3 >> 26); h_[3] = uint32_t(d3) & 0x3ffffff;
d4 += c; c = uint32_t(d4 >> 26); h_[4] = uint32_t(d4) & 0x3ffffff;
h_[0] += c * 5; c = h_[0] >> 26; h_[0] &= 0x3ffffff;
h_[1] += c;
}
uint32_t r_[5]{};
uint32_t h_[5]{};
uint32_t pad_[4]{};
uint8_t buf_[16]{};
size_t bufLen_ = 0;
};
inline void poly1305Pad16(Poly1305& p, size_t n) {
static const uint8_t zeros[16] = {0};
const size_t rem = n % 16;
if (rem) p.update(zeros, 16 - rem);
}
inline void lenLE64(uint64_t v, uint8_t out[8]) {
for (int i = 0; i < 8; ++i) out[i] = uint8_t(v >> (8 * i));
}
// Equal in constant time, so a wrong tag leaks nothing through timing.
inline bool equalCT(const uint8_t* a, const uint8_t* b, size_t n) {
uint8_t diff = 0;
for (size_t i = 0; i < n; ++i) diff = uint8_t(diff | (a[i] ^ b[i]));
return diff == 0;
}
} // namespace detail
// ── AEAD (RFC 8439 §2.8) ──────────────────────────────────────────────────
inline constexpr size_t kKeyBytes = 32;
inline constexpr size_t kNonceBytes = 12;
inline constexpr size_t kTagBytes = 16;
// Seals `plain` in place-independent fashion: `out` receives the ciphertext,
// `tag` the authenticator over ciphertext and associated data.
inline void aeadSeal(const uint8_t key[kKeyBytes], const uint8_t nonce[kNonceBytes],
const uint8_t* aad, size_t aadLen, const uint8_t* plain,
size_t plainLen, uint8_t* out, uint8_t tag[kTagBytes]) {
uint8_t polyKey[64];
const uint8_t zeroIn[64] = {0};
detail::chacha20Xor(key, 0, nonce, zeroIn, 64, polyKey); // block 0
if (plainLen) detail::chacha20Xor(key, 1, nonce, plain, plainLen, out);
detail::Poly1305 mac(polyKey);
if (aadLen) {
mac.update(aad, aadLen);
detail::poly1305Pad16(mac, aadLen);
}
if (plainLen) {
mac.update(out, plainLen);
detail::poly1305Pad16(mac, plainLen);
}
uint8_t lens[16];
detail::lenLE64(aadLen, lens);
detail::lenLE64(plainLen, lens + 8);
mac.update(lens, 16);
mac.finish(tag);
}
// Opens what aeadSeal produced. Returns false — without writing plaintext the
// caller can trust — when the tag does not verify.
inline bool aeadOpen(const uint8_t key[kKeyBytes], const uint8_t nonce[kNonceBytes],
const uint8_t* aad, size_t aadLen, const uint8_t* cipher,
size_t cipherLen, const uint8_t tag[kTagBytes],
uint8_t* out) {
uint8_t polyKey[64];
const uint8_t zeroIn[64] = {0};
detail::chacha20Xor(key, 0, nonce, zeroIn, 64, polyKey);
detail::Poly1305 mac(polyKey);
if (aadLen) {
mac.update(aad, aadLen);
detail::poly1305Pad16(mac, aadLen);
}
if (cipherLen) {
mac.update(cipher, cipherLen);
detail::poly1305Pad16(mac, cipherLen);
}
uint8_t lens[16];
detail::lenLE64(aadLen, lens);
detail::lenLE64(cipherLen, lens + 8);
mac.update(lens, 16);
uint8_t want[kTagBytes];
mac.finish(want);
if (!detail::equalCT(want, tag, kTagBytes)) return false;
if (cipherLen) detail::chacha20Xor(key, 1, nonce, cipher, cipherLen, out);
return true;
}
// Convenience over vectors: ciphertext with the 16-byte tag appended.
inline std::vector<uint8_t> seal(const std::array<uint8_t, kKeyBytes>& key,
const std::array<uint8_t, kNonceBytes>& nonce,
const std::string& aad,
const std::vector<uint8_t>& plain) {
std::vector<uint8_t> out(plain.size() + kTagBytes);
aeadSeal(key.data(), nonce.data(),
reinterpret_cast<const uint8_t*>(aad.data()), aad.size(),
plain.data(), plain.size(), out.data(), out.data() + plain.size());
return out;
}
inline bool open(const std::array<uint8_t, kKeyBytes>& key,
const std::array<uint8_t, kNonceBytes>& nonce,
const std::string& aad, const std::vector<uint8_t>& sealed,
std::vector<uint8_t>& plain) {
if (sealed.size() < kTagBytes) return false;
const size_t n = sealed.size() - kTagBytes;
plain.assign(n, 0);
return aeadOpen(key.data(), nonce.data(),
reinterpret_cast<const uint8_t*>(aad.data()), aad.size(),
sealed.data(), n, sealed.data() + n, plain.data());
}
} // namespace aead
} // namespace ANSCENTER
#endif // ANSAEAD_H
+74
View File
@@ -0,0 +1,74 @@
#ifndef ANSCSPRNG_H
#define ANSCSPRNG_H
#pragma once
// ANSCsprng — random bytes from the operating system, for keys, salts and
// nonces.
//
// TAKEN FROM ANSMINDCORE (src/core/csprng.hpp). NOT std::random_device or a
// seeded generator: fine for an id that only needs to be distinct, and wrong
// for anything a guesser must not predict. The OS generator is the only
// source used for key material here.
//
// Windows BCryptGenRandom with the system-preferred RNG (bcrypt.lib)
// Linux getrandom(2), looping over short reads and EINTR
// macOS arc4random_buf, which cannot fail
//
// Returns false when the OS refused — callers treat that as "cannot seal",
// never as "seal with whatever was in the buffer".
#include <cstddef>
#include <cstdint>
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <bcrypt.h>
#if defined(_MSC_VER)
#pragma comment(lib, "bcrypt.lib")
#endif
#elif defined(__APPLE__)
#include <stdlib.h>
#else
#include <cerrno>
#include <sys/random.h>
#endif
namespace ANSCENTER {
inline bool randomBytes(uint8_t* out, size_t n) {
if (n == 0) return true;
if (!out) return false;
#if defined(_WIN32)
while (n > 0) {
const ULONG chunk = n > 0x7fffffffu ? 0x7fffffffu : ULONG(n);
if (BCryptGenRandom(nullptr, out, chunk, BCRYPT_USE_SYSTEM_PREFERRED_RNG) != 0)
return false;
out += chunk;
n -= chunk;
}
return true;
#elif defined(__APPLE__)
arc4random_buf(out, n);
return true;
#else
while (n > 0) {
const ssize_t got = getrandom(out, n, 0);
if (got < 0) {
if (errno == EINTR) continue;
return false;
}
out += size_t(got);
n -= size_t(got);
}
return true;
#endif
}
} // namespace ANSCENTER
#endif // ANSCSPRNG_H
+45
View File
@@ -0,0 +1,45 @@
#ifndef ANSMODELKEYS_H
#define ANSMODELKEYS_H
#pragma once
// ANSModelKeys — the keys this build was given, if it was given any.
//
// The base private key is not in this file and is not in the repository. It is
// written by the key ceremony (ANSMINDCORE's tools/ansmodel_keys.cpp) into
// ANSModelKeysGenerated.h, which is ignored by git and produced on the machine
// that builds a release. This header is the seam: it uses that file when it is
// there and says "no keys" when it is not.
//
// A BUILD WITHOUT THE KEY IS A VALID BUILD. Every zip still loads, and a
// package is refused with a sentence that says why. That is what keeps the key
// out of a developer's checkout without keeping developers out of the code.
// ANSCORE_HAVE_BUILTIN_MODEL_KEYS is set by CMake when the ceremony's
// generated header is there. It is not tested with __has_include on purpose:
// that test leaves no dependency for the build system, so a key emitted after
// a build would not take effect until someone cleaned — and the failure looks
// exactly like a missing key, which on a release machine means shipping an
// engine that silently cannot open a package.
#if defined(ANSCORE_HAVE_BUILTIN_MODEL_KEYS)
#include "ANSModelKeysGenerated.h"
#endif
#include "ANSModelPackage.h"
namespace ANSCENTER {
namespace modelpkg {
// Add whatever this build ships with to a keyring. False when it ships with
// nothing, which is not an error.
inline bool AddBuiltInKeys(Keyring& ring) {
#if defined(ANSCORE_HAVE_BUILTIN_MODEL_KEYS)
return builtin::addBuiltInKeys(ring);
#else
(void)ring;
return false;
#endif
}
} // namespace modelpkg
} // namespace ANSCENTER
#endif // ANSMODELKEYS_H
+250
View File
@@ -0,0 +1,250 @@
#ifndef ANSMODELOPEN_H
#define ANSMODELOPEN_H
#pragma once
// ANSModelOpen — the one place that knows how a model package reaches an engine.
//
// An engine's "model" has always been a path: a password-protected zip that
// ANSODBase inflates into a folder, after which the engine picks out
// train_last.onnx, classes.names and model_config.json. A .ansmodel package
// replaces all four with one sealed file, and this header is what turns it
// back into the things the engines already understand — the model's bytes, the
// class list, the task and the input size.
//
// THE PLAINTEXT NEVER REACHES THE DISK. That is the whole reason the format
// exists. OpenModelPackage decrypts into a buffer the caller owns; the caller
// hands that buffer to a session and wipes it. Nothing here writes a file.
//
// WHERE THE KEY COMES FROM. Three places, in this order: what the build ships
// with (ANSModelKeys.h, filled in by the key ceremony), what the environment
// offers a developer, and whatever a caller sets with SetProcessModelKeys().
// Nothing here reads a key out of the package or off the disk. With no keys at
// all, a package is refused with a sentence that says so, and every existing
// zip still loads — a build made without the ceremony runs everything except
// packages.
//
// The base private key must never sit in the binary as one contiguous
// constant; assembling it is the job of whatever calls SetProcessModelKeys,
// not of this header, which is why this header has no key material in it.
//
// The development fallback (ANSCORE_MODEL_KEY_ID / ANSCORE_MODEL_BASE_PRIVATE
// / ANSCORE_MODEL_ROOT_ID / ANSCORE_MODEL_ROOT_PUBLIC, all base64) exists so
// the path can be exercised before that ceremony exists. It is read once and
// announced, because a key that came from the environment is not the key a
// shipped product should be using.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <string>
#include <vector>
#include "ANSModelKeys.h"
#include "ANSModelPackage.h"
#include "ANSOnnxShape.h"
namespace ANSCENTER {
namespace modelpkg {
// ── the keys this process opens packages with ─────────────────────────────
namespace detail {
inline std::mutex& keysMutex() {
static std::mutex m;
return m;
}
inline Keyring& keyringStorage() {
static Keyring ring;
return ring;
}
inline bool keyFromB64(const char* b64, Key& out) {
if (!b64 || !*b64) return false;
std::vector<uint8_t> raw;
if (!bits::base64Decode(b64, raw) || raw.size() != out.size()) return false;
std::memcpy(out.data(), raw.data(), out.size());
return true;
}
// True once this process's keyring is settled — either because the build's own
// keys were installed, or because a caller replaced them outright. A caller
// that sets keys means exactly that: what it set is what this process holds.
inline bool& keysSettled() {
static bool settled = false;
return settled;
}
// Development only: keys from the environment, and said out loud.
inline void loadEnvKeys(Keyring& ring) {
const char* id = std::getenv("ANSCORE_MODEL_KEY_ID");
const char* priv = std::getenv("ANSCORE_MODEL_BASE_PRIVATE");
Key k{};
if (id && *id && keyFromB64(priv, k)) {
ring.addX25519(id, k);
std::fprintf(stderr,
"[modelpkg] base key %s taken from the environment - for "
"development, not for a shipped build\n",
id);
}
const char* rootId = std::getenv("ANSCORE_MODEL_ROOT_ID");
const char* rootPub = std::getenv("ANSCORE_MODEL_ROOT_PUBLIC");
Key r{};
if (rootId && *rootId && keyFromB64(rootPub, r)) {
ring.addRoot(rootId, r);
std::fprintf(stderr,
"[modelpkg] root key %s taken from the environment\n",
rootId);
}
}
} // namespace detail
inline void SetProcessModelKeys(Keyring ring) {
std::lock_guard<std::mutex> lk(detail::keysMutex());
detail::keyringStorage() = std::move(ring);
detail::keysSettled() = true;
}
inline Keyring ProcessModelKeys() {
std::lock_guard<std::mutex> lk(detail::keysMutex());
Keyring& ring = detail::keyringStorage();
if (!detail::keysSettled()) {
detail::keysSettled() = true;
AddBuiltInKeys(ring); // what this build was given at the ceremony
detail::loadEnvKeys(ring); // and what a developer's environment adds
}
return ring; // by value: the caller may open on another thread
}
// True once this build can open anything at all. A product that has not had
// keys installed should say "this build cannot open model packages" rather
// than let every load fail one at a time.
inline bool HaveProcessModelKeys() {
const Keyring ring = ProcessModelKeys();
return !ring.x25519.empty() || !ring.keys.empty();
}
// ── recognising one ───────────────────────────────────────────────────────
inline bool IsPackagePath(const std::string& path) {
return path.size() > 9 &&
path.compare(path.size() - 9, 9, ".ansmodel") == 0;
}
// ── what an engine needs out of one ───────────────────────────────────────
// Everything the inflated folder used to carry, in the shape the engines read
// it: the model itself, the classes in order, and the geometry that
// model_config.json held.
struct OpenedModel {
std::vector<uint8_t> model; // decrypted; wipe it once loaded
std::vector<std::string> classes;
std::string task; // detection | classification | segmentation | obb | pose
std::string family; // yolo | yolo10 | rfdetr
std::string runtime; // onnx | openvino-ir
std::string legacyMarker; // what the zip would have carried
std::string name; // what to call this model in a log
int inputWidth = 0; // 0 when the package does not say
int inputHeight = 0;
std::string warning; // show it; the model still loaded
void wipe() {
if (!model.empty()) std::memset(model.data(), 0, model.size());
model.clear();
model.shrink_to_fit();
}
};
// Read what a package says about itself without decrypting the model. Cheap
// enough to call on a folder full of packages.
inline bool DescribeModelPackage(const std::string& path, OpenedModel& out,
std::string& err) {
Package pkg;
if (!open(path, ProcessModelKeys(), pkg, err)) return false;
const Json& m = pkg.manifest;
out.task = m.value("task", std::string("detection"));
out.family = m.value("family", std::string());
out.runtime = m.value("runtime", std::string("onnx"));
out.legacyMarker =
m.value("legacy", Json::object()).value("marker", std::string());
out.warning = pkg.warning;
out.classes.clear();
for (const auto& c : m.value("classes", Json::array()))
out.classes.push_back(c.value("name", std::string()));
const Json in = m.value("input", Json::object());
out.inputWidth = in.value("width", 0);
out.inputHeight = in.value("height", 0);
out.name = m.value("project", std::string());
if (out.name.empty()) out.name = path;
return true;
}
// Open one all the way: the description above, plus the model's bytes.
//
// The caller owns those bytes and should wipe them (OpenedModel::wipe) the
// moment the session has copied what it needs.
inline bool OpenModelPackage(const std::string& path, OpenedModel& out,
std::string& err) {
if (!HaveProcessModelKeys()) {
err = "this build has no model-package key, so it cannot open " + path;
return false;
}
Package pkg;
if (!open(path, ProcessModelKeys(), pkg, err)) return false;
const std::string entry = pkg.entryPayloadId();
if (entry.empty()) {
err = "the package names no model to run";
return false;
}
if (!readPayload(pkg, entry, out.model, err)) return false;
const Json& m = pkg.manifest;
out.task = m.value("task", std::string("detection"));
out.family = m.value("family", std::string());
out.runtime = m.value("runtime", std::string("onnx"));
out.legacyMarker =
m.value("legacy", Json::object()).value("marker", std::string());
out.warning = pkg.warning;
out.classes.clear();
for (const auto& c : m.value("classes", Json::array()))
out.classes.push_back(c.value("name", std::string()));
const Json in = m.value("input", Json::object());
out.inputWidth = in.value("width", 0);
out.inputHeight = in.value("height", 0);
out.name = m.value("project", std::string());
if (out.name.empty()) out.name = path;
// WHAT AN OLDER PACKAGE CANNOT TELL US, and its model can. The input
// size, when the manifest predates the field; and the head, because a
// package whose family is "yolo" may hold an end-to-end export — the box
// used to take the family from the backend marker, and the marker names
// the runtime rather than the head. It costs nothing to ask here: the
// bytes are already decrypted.
{
onnxshape::GraphShape g;
std::string swhy;
if (onnxshape::graphFromMemory(out.model.data(), out.model.size(), g,
swhy)) {
if (out.inputWidth <= 0 || out.inputHeight <= 0) {
out.inputWidth = g.input.width;
out.inputHeight = g.input.height;
}
if (g.endToEnd() && out.family == "yolo") {
out.family = "yolo10";
out.warning = out.warning.empty()
? "this package says family yolo, but its head is "
"end-to-end; reading it as yolo10"
: out.warning;
}
}
}
return true;
}
} // namespace modelpkg
} // namespace ANSCENTER
#endif // ANSMODELOPEN_H
+812
View File
@@ -0,0 +1,812 @@
#ifndef ANSMODELPACKAGE_H
#define ANSMODELPACKAGE_H
#pragma once
// ANSModelPackage — reading (and, for tools and tests, writing) an ANS model
// package: one encrypted file carrying a model, its classes and how to run it.
//
// KEPT IN STEP WITH ANSMINDCORE's src/model/ansmodel.hpp. The two are the same
// format and must stay byte-compatible: the unit test here opens a package
// written by THAT implementation, which is what makes "compatible" a fact
// rather than an intention. Change them together.
//
// WHAT THIS REPLACES. A model shipped as a zip under a password every copy of
// the software knew — a password that has since been recovered — holding
// Categories/<name>.<id>, classes.names, a zero-length backend marker and
// train_last.onnx. Opening one meant inflating it to a directory, so the
// unprotected model sat on disk, was optimised, and was zipped back into a
// cache. This format keeps every fact that bundle carried and changes how it
// is carried: the manifest names the classes and the model family, and the
// bytes go straight from decryption into the runtime (engine/ibackend's
// ModelSource) without touching the filesystem.
//
// LAYOUT (all integers little-endian, offsets absolute):
//
// 0 8 magic "ANSMDL\0\0"
// 8 2 format version (1)
// 10 2 flags, bit 0 = more than one model
// 12 4 header length
// 16 n cleartext header, UTF-8 JSON — readable without any key
// … manifest, sealed — classes, task, family, models
// … payloads, sealed in chunks — the model bytes
//
// WHAT IS IN THE CLEAR, and why so little. The header carries only what a
// reader needs before it can decrypt — which key opens the file, where the
// sealed parts are — plus an optional card of counts for a store listing.
// Class names, metrics and the model's shape are in the manifest, sealed.
//
// SEALING. ChaCha20-Poly1305 (core/chacha20poly1305) with a content key that
// is random per package. Each chunk's nonce is a random per-payload base
// followed by the chunk index, and the associated data binds the key id, the
// payload id and that index — so a chunk cannot be moved between payloads or
// between packages, and a reordered file fails on the first chunk.
//
// WRAPPING. The content key is sealed to the base key with X25519: an
// ephemeral key pair per package, a shared secret against the base public key,
// and a key derived from it with HMAC-SHA256. Packaging tools therefore hold
// only the PUBLIC half and cannot open what they produce — which is the point
// of doing it asymmetrically when a symmetric key would have been simpler. A
// package may carry several wraps; a reader takes the first one it holds a key
// for. The older symmetric wrap is still read, so packages written before this
// keep opening.
//
// SIGNING. Ed25519 (RFC 8032, via the vendored monocypher-ed25519) over the
// header with its own `sig` removed. The signature is made by a RELEASE key
// whose public half travels in the package inside a certificate the ROOT key
// signed — so an engine that has never seen that release still verifies it,
// from the one root key it was built with. That is what lets signing keys
// rotate without stranding an engine that cannot be updated.
//
// EXPIRY AND REVOCATION. An expired certificate WARNS and loads: the date says
// when a key was in service, not whether the package is genuine, and an
// air-gapped box's clock drifts. A revoked key refuses outright. Strict mode
// turns expiry into a refusal for sites that keep accurate time.
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include <nlohmann/json.hpp>
#include "ANSAead.h"
#include "ANSCsprng.h"
#include "ANSPackageBits.h"
extern "C" {
#include "monocypher-ed25519.h"
#include "monocypher.h"
}
namespace ANSCENTER {
namespace modelpkg {
using Json = nlohmann::json;
inline constexpr char kMagic[8] = {'A', 'N', 'S', 'M', 'D', 'L', '\0', '\0'};
inline constexpr uint16_t kVersion = 1;
inline constexpr uint16_t kFlagMultiModel = 1u << 0;
inline constexpr const char* kWrapSymmetric = "chacha20poly1305-key";
inline constexpr const char* kWrapX25519 = "x25519-hkdf-sha256-chacha20poly1305";
inline constexpr const char* kSigEd25519 = "ed25519";
inline constexpr uint32_t kDefaultChunk = 4u * 1024 * 1024;
using Key = std::array<uint8_t, aead::kKeyBytes>;
// What this build can open and what it trusts.
//
// A reader holds every base key it has ever shipped, because the base key does
// not rotate: a package names the key it was sealed to, and the answer to an
// unknown id is "this package needs a newer engine", not a decryption failure.
// It holds root public keys, which is how a release signing key it has never
// seen is still verified. And it holds the list of signing keys that have been
// burned, which is the only thing that refuses a validly signed package.
struct Keyring {
std::map<std::string, Key> keys; // symmetric base keys (legacy wrap)
std::map<std::string, Key> x25519; // base PRIVATE keys, by id
std::map<std::string, Key> roots; // root PUBLIC keys, by id
std::vector<std::string> revokedSigners;
bool strictExpiry = false; // off: an expired cert warns and loads
int64_t nowSec = 0; // 0 = ask the clock
void add(std::string id, const Key& k) { keys[std::move(id)] = k; }
void addX25519(std::string id, const Key& priv) { x25519[std::move(id)] = priv; }
void addRoot(std::string id, const Key& pub) { roots[std::move(id)] = pub; }
bool revoked(const std::string& signerId) const {
for (const auto& r : revokedSigners)
if (r == signerId) return true;
return false;
}
const Key* find(const std::string& id) const {
const auto it = keys.find(id);
return it == keys.end() ? nullptr : &it->second;
}
};
// One model inside a package: where its bytes are and how they are sealed.
struct PayloadRef {
std::string id;
uint64_t off = 0;
uint64_t len = 0; // sealed length, tags included
uint32_t chunk = kDefaultChunk;
std::array<uint8_t, 4> nonceBase{};
};
// An opened package: everything but the model bytes, which are read on demand
// so a two-gigabyte model is never decrypted to answer "what is this?".
struct Package {
std::string path;
uint16_t version = 0;
uint16_t flags = 0;
std::string keyId;
Json header; // the cleartext header, as read
Json manifest; // decrypted
std::vector<PayloadRef> payloads;
Key contentKey{};
std::string signerId; // the release key that signed it
bool certExpired = false; // loaded anyway; see the header comment
std::string warning; // what a caller should show, if anything
bool multiModel() const { return (flags & kFlagMultiModel) != 0; }
// The model a reader should run when it is given no choice: the manifest's
// entry model, or the first one.
std::string entryPayloadId() const {
const auto models = manifest.value("models", Json::array());
for (const auto& m : models)
if (m.value("entry", false)) return m.value("payload", std::string());
if (!models.empty()) return models[0].value("payload", std::string());
return payloads.empty() ? std::string() : payloads.front().id;
}
};
namespace detail {
inline uint16_t rd16(const uint8_t* p) { return uint16_t(p[0] | (p[1] << 8)); }
inline uint32_t rd32(const uint8_t* p) {
return uint32_t(p[0]) | (uint32_t(p[1]) << 8) | (uint32_t(p[2]) << 16) |
(uint32_t(p[3]) << 24);
}
inline void wr16(std::string& out, uint16_t v) {
out.push_back(char(v & 0xff));
out.push_back(char(v >> 8));
}
inline void wr32(std::string& out, uint32_t v) {
for (int i = 0; i < 4; ++i) out.push_back(char((v >> (8 * i)) & 0xff));
}
inline std::string b64(const std::vector<uint8_t>& v) {
return bits::base64Encode(std::string(v.begin(), v.end()));
}
inline std::vector<uint8_t> unb64(const std::string& s) {
std::vector<uint8_t> out;
if (!bits::base64Decode(s, out)) out.clear();
return out;
}
// The nonce for one chunk: a random per-payload base, then the index. Unique
// per (payload, chunk) under a content key that is unique per package.
inline std::array<uint8_t, aead::kNonceBytes> chunkNonce(
const std::array<uint8_t, 4>& base, uint64_t index) {
std::array<uint8_t, aead::kNonceBytes> n{};
std::memcpy(n.data(), base.data(), 4);
for (int i = 0; i < 8; ++i) n[4 + i] = uint8_t(index >> (8 * i));
return n;
}
// What each chunk is bound to. Moving a chunk to another payload, another
// index or another package changes this, and the tag then fails.
inline std::string chunkAad(const std::string& keyId, const std::string& payloadId,
uint64_t index, uint64_t chunkCount) {
return "ansmodel/v1|" + keyId + "|" + payloadId + "|" + std::to_string(index) +
"|" + std::to_string(chunkCount);
}
// The wrapping key from an X25519 shared secret. HMAC-SHA256 over the secret
// with a fixed label, which is HKDF-Extract: the raw secret is not uniform and
// must never be used as a key directly.
inline Key deriveWrapKey(const uint8_t shared[32], const std::string& keyId) {
const std::string salt = "ansmodel/v1|wrap|" + keyId;
const auto mac = bits::hmacSha256(
reinterpret_cast<const uint8_t*>(salt.data()), salt.size(), shared, 32);
Key k{};
std::memcpy(k.data(), mac.data(), k.size());
return k;
}
// The bytes a signature covers: the header with its own `sig` taken out, as
// canonical JSON. nlohmann orders object keys, so a reader that removes the
// same field and re-serialises reproduces exactly what the packer signed.
inline std::string signedBytes(Json header) {
header.erase("sig");
return header.dump();
}
// What the root vouches for: this release key, this id, until this date.
inline std::string certBytes(const Json& cert) {
return "ansmodel/v1|cert|" + cert.value("keyId", std::string()) + "|" +
cert.value("pub", std::string()) + "|" +
cert.value("notAfter", std::string());
}
inline int64_t nowSeconds() {
return int64_t(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
}
// "2027-09-01T00:00:00Z" → seconds. Returns 0 when it cannot be read, which
// is treated as "no expiry stated" rather than "expired".
inline int64_t parseIso8601(const std::string& t) {
std::tm tm{};
if (t.size() < 19) return 0;
tm.tm_year = std::atoi(t.substr(0, 4).c_str()) - 1900;
tm.tm_mon = std::atoi(t.substr(5, 2).c_str()) - 1;
tm.tm_mday = std::atoi(t.substr(8, 2).c_str());
tm.tm_hour = std::atoi(t.substr(11, 2).c_str());
tm.tm_min = std::atoi(t.substr(14, 2).c_str());
tm.tm_sec = std::atoi(t.substr(17, 2).c_str());
if (tm.tm_year <= 0 || tm.tm_mon < 0 || tm.tm_mon > 11) return 0;
#if defined(_WIN32)
return int64_t(_mkgmtime(&tm));
#else
return int64_t(timegm(&tm));
#endif
}
inline uint64_t chunkCountFor(uint64_t sealedLen, uint32_t chunk) {
const uint64_t perChunk = chunk + aead::kTagBytes;
return (sealedLen + perChunk - 1) / perChunk;
}
} // namespace detail
// ── reading ───────────────────────────────────────────────────────────────
// Open the header and manifest. Does not read a single model byte.
inline bool open(const std::string& path, const Keyring& ring, Package& out,
std::string& err) {
out = Package{};
out.path = path;
std::ifstream f(path, std::ios::binary);
if (!f) {
err = "cannot open " + path;
return false;
}
uint8_t fixed[16];
f.read(reinterpret_cast<char*>(fixed), sizeof(fixed));
if (f.gcount() != std::streamsize(sizeof(fixed))) {
err = "not a model package: file is too short";
return false;
}
if (std::memcmp(fixed, kMagic, sizeof(kMagic)) != 0) {
err = "not a model package: wrong magic";
return false;
}
out.version = detail::rd16(fixed + 8);
out.flags = detail::rd16(fixed + 10);
if (out.version > kVersion) {
err = "this package is format version " + std::to_string(out.version) +
", which needs a newer ANS engine";
return false;
}
const uint32_t headerLen = detail::rd32(fixed + 12);
if (headerLen == 0 || headerLen > 8u * 1024 * 1024) {
err = "header length is not plausible";
return false;
}
std::string headerText(headerLen, '\0');
f.read(headerText.data(), headerLen);
if (f.gcount() != std::streamsize(headerLen)) {
err = "truncated: the header is not all there";
return false;
}
out.header = Json::parse(headerText, nullptr, false);
if (out.header.is_discarded()) {
err = "the header is not readable JSON";
return false;
}
// ── the signature, before anything is decrypted ────────────────────────
const Json sig = out.header.value("sig", Json::object());
if (sig.is_null() || sig.empty()) {
err = "the package is not signed";
return false;
}
const std::string sigAlg = sig.value("alg", std::string());
if (sigAlg == kSigEd25519) {
const Json cert = sig.value("cert", Json::object());
const std::string signerId = cert.value("keyId", std::string());
const std::string rootId = cert.value("rootKeyId", std::string());
const auto signerPub = detail::unb64(cert.value("pub", std::string()));
const auto rootSig = detail::unb64(cert.value("rootSig", std::string()));
const auto headerSig = detail::unb64(sig.value("value", std::string()));
if (signerPub.size() != 32 || rootSig.size() != 64 || headerSig.size() != 64) {
err = "the signature block is malformed";
return false;
}
if (ring.revoked(signerId)) {
err = "this package was signed with a key that has been revoked (" +
signerId + ")";
return false;
}
const auto rootIt = ring.roots.find(rootId);
if (rootIt == ring.roots.end()) {
err = "the package is vouched for by an unknown root (" + rootId + ")";
return false;
}
// The root vouches for the release key; the release key signs the header.
const std::string cb = detail::certBytes(cert);
if (crypto_ed25519_check(rootSig.data(), rootIt->second.data(),
reinterpret_cast<const uint8_t*>(cb.data()),
cb.size()) != 0) {
err = "the signing key is not vouched for by the root it names";
return false;
}
const std::string sb = detail::signedBytes(out.header);
if (crypto_ed25519_check(headerSig.data(), signerPub.data(),
reinterpret_cast<const uint8_t*>(sb.data()),
sb.size()) != 0) {
err = "the package signature does not verify — it has been altered";
return false;
}
out.signerId = signerId;
// Expiry is a fact about the key's service, not about this package's
// authenticity, and an air-gapped clock drifts. Warn and load; strict
// mode is for sites that would rather fail closed.
const int64_t notAfter = detail::parseIso8601(cert.value("notAfter", std::string()));
const int64_t now = ring.nowSec > 0 ? ring.nowSec : detail::nowSeconds();
if (notAfter > 0 && now > notAfter) {
out.certExpired = true;
out.warning = "signed with a certificate that expired on " +
cert.value("notAfter", std::string()) + " (key " + signerId + ")";
if (ring.strictExpiry) {
err = out.warning + " — refused because strict expiry is on";
return false;
}
}
} else if (sigAlg != "none") {
err = "the package is signed in a way this build does not know (" + sigAlg + ")";
return false;
}
// Which key opens it. Every wrap is tried, so a package sealed to several
// keys opens on any engine holding one of them.
const auto wraps = out.header.value("wraps", Json::array());
if (wraps.empty()) {
err = "the package names no key";
return false;
}
std::vector<std::string> unknown;
bool unwrapped = false;
for (const auto& w : wraps) {
const std::string keyId = w.value("keyId", std::string());
const std::string alg = w.value("alg", std::string());
const auto nonceBytes = detail::unb64(w.value("nonce", std::string()));
const auto ct = detail::unb64(w.value("ct", std::string()));
if (nonceBytes.size() != aead::kNonceBytes ||
ct.size() != aead::kKeyBytes + aead::kTagBytes) {
err = "the wrapped key is malformed";
return false;
}
std::array<uint8_t, aead::kNonceBytes> nonce{};
std::memcpy(nonce.data(), nonceBytes.data(), nonce.size());
// The key that opens THIS wrap: either an X25519 shared secret against the
// ephemeral public key the packer left here, or — for packages written
// before the curve was vendored — the symmetric base key.
Key unwrapKey{};
if (alg == kWrapX25519) {
const auto it = ring.x25519.find(keyId);
if (it == ring.x25519.end()) {
unknown.push_back(keyId);
continue;
}
const auto epk = detail::unb64(w.value("epk", std::string()));
if (epk.size() != 32) {
err = "the wrap carries no usable ephemeral key";
return false;
}
uint8_t shared[32];
crypto_x25519(shared, it->second.data(), epk.data());
unwrapKey = detail::deriveWrapKey(shared, keyId);
crypto_wipe(shared, sizeof(shared));
} else if (alg == kWrapSymmetric) {
const Key* k = ring.find(keyId);
if (!k) {
unknown.push_back(keyId);
continue;
}
unwrapKey = *k;
} else {
err = "the package uses an unsupported wrap (" + alg + ")";
return false;
}
std::vector<uint8_t> plain;
if (!aead::open(unwrapKey, nonce, "ansmodel/v1|wrap|" + keyId, ct, plain)) {
err = "the wrapped key does not open with " + keyId +
" — the file is damaged or was sealed to a different key";
return false;
}
std::memcpy(out.contentKey.data(), plain.data(), out.contentKey.size());
out.keyId = keyId;
unwrapped = true;
break;
}
if (!unwrapped) {
err = "this package needs a newer ANS engine: it is sealed to key " +
(unknown.empty() ? std::string("(unnamed)") : unknown.front());
return false;
}
// Where the sealed parts are.
for (const auto& p : out.header.value("payloads", Json::array())) {
PayloadRef r;
r.id = p.value("id", std::string());
r.off = p.value("off", uint64_t(0));
r.len = p.value("len", uint64_t(0));
r.chunk = p.value("chunk", kDefaultChunk);
const auto nb = detail::unb64(p.value("nonceBase", std::string()));
if (r.id.empty() || r.len == 0 || r.chunk == 0 || nb.size() != 4) {
err = "a payload entry is malformed";
return false;
}
std::memcpy(r.nonceBase.data(), nb.data(), 4);
out.payloads.push_back(r);
}
if (out.payloads.empty()) {
err = "the package carries no model";
return false;
}
// The manifest, sealed as a single chunk under the payload id "manifest".
const Json m = out.header.value("manifest", Json::object());
const uint64_t moff = m.value("off", uint64_t(0));
const uint64_t mlen = m.value("len", uint64_t(0));
const auto mnb = detail::unb64(m.value("nonceBase", std::string()));
if (mlen == 0 || mnb.size() != 4) {
err = "the manifest entry is malformed";
return false;
}
std::vector<uint8_t> sealed(static_cast<size_t>(mlen), 0);
f.seekg(std::streamoff(moff));
f.read(reinterpret_cast<char*>(sealed.data()), std::streamsize(mlen));
if (f.gcount() != std::streamsize(mlen)) {
err = "truncated: the manifest is not all there";
return false;
}
std::array<uint8_t, 4> mbase{};
std::memcpy(mbase.data(), mnb.data(), 4);
std::vector<uint8_t> manifestText;
if (!aead::open(out.contentKey, detail::chunkNonce(mbase, 0),
detail::chunkAad(out.keyId, "manifest", 0, 1), sealed,
manifestText)) {
err = "the manifest does not verify — the package has been altered";
return false;
}
out.manifest =
Json::parse(std::string(manifestText.begin(), manifestText.end()), nullptr,
false);
if (out.manifest.is_discarded()) {
err = "the manifest is not readable JSON";
return false;
}
return true;
}
// Decrypt one model into memory. Chunk by chunk, each verified as it is read,
// so a damaged file fails on the chunk that is wrong rather than after the
// whole thing has been decrypted.
inline bool readPayload(const Package& pkg, const std::string& payloadId,
std::vector<uint8_t>& out, std::string& err) {
const PayloadRef* ref = nullptr;
for (const auto& p : pkg.payloads)
if (p.id == payloadId) ref = &p;
if (!ref) {
err = "no model called " + payloadId + " in this package";
return false;
}
std::ifstream f(pkg.path, std::ios::binary);
if (!f) {
err = "cannot reopen " + pkg.path;
return false;
}
f.seekg(std::streamoff(ref->off));
const uint64_t chunks = detail::chunkCountFor(ref->len, ref->chunk);
out.clear();
out.reserve(size_t(ref->len));
std::vector<uint8_t> sealed;
std::vector<uint8_t> plain;
uint64_t remaining = ref->len;
for (uint64_t i = 0; i < chunks; ++i) {
const uint64_t take =
remaining < uint64_t(ref->chunk) + aead::kTagBytes
? remaining
: uint64_t(ref->chunk) + aead::kTagBytes;
sealed.assign(size_t(take), 0);
f.read(reinterpret_cast<char*>(sealed.data()), std::streamsize(take));
if (f.gcount() != std::streamsize(take)) {
err = "truncated: model " + payloadId + " stops at chunk " +
std::to_string(i);
return false;
}
if (!aead::open(pkg.contentKey, detail::chunkNonce(ref->nonceBase, i),
detail::chunkAad(pkg.keyId, ref->id, i, chunks), sealed,
plain)) {
err = "chunk " + std::to_string(i) + " of " + payloadId +
" does not verify — the package has been altered";
out.clear();
return false;
}
out.insert(out.end(), plain.begin(), plain.end());
remaining -= take;
}
return true;
}
// ── writing (tools and tests) ─────────────────────────────────────────────
//
// The product's packages are written by the training box. This exists so the
// reader can be tested against files built by something other than itself,
// and so a converter can turn today's zip bundles into packages.
struct PackModel {
std::string id; // payload id, e.g. "p0"
std::vector<uint8_t> bytes;
};
struct PackRequest {
std::string keyId;
Key key{}; // symmetric base key (legacy wrap)
// Wrap to a base PUBLIC key instead, which is what a real packer does: it
// can then produce packages it cannot itself open.
bool useX25519 = false;
Key basePublic{};
// Sign as a release key the root vouches for. Empty signerSecret writes an
// unsigned package, which only tests should ever want.
std::string signerId;
std::vector<uint8_t> signerSecret; // 64 bytes, RFC 8032
std::vector<uint8_t> signerPublic; // 32 bytes
std::string rootKeyId;
std::vector<uint8_t> rootSig; // 64 bytes over the certificate
std::string notAfter; // ISO-8601, may be empty
Json manifest;
Json card; // optional, counts only
std::vector<PackModel> models;
uint32_t chunk = kDefaultChunk; // smaller in tests, to exercise chunking
// TESTS ONLY. pack() draws the content key, the ephemeral X25519 key and
// every nonce from the operating system's CSPRNG. Setting this derives them
// from `testSeed` instead, so a fixture can be written byte-for-byte again —
// and so anyone holding the package can recover its content key by trying
// the 256 seeds, without the base key. It is honoured only in a build that
// defines ANSMODEL_DETERMINISTIC_PACK_FOR_TESTS; anywhere else pack() refuses
// rather than write a package that only looks sealed.
bool insecureDeterministicForTests = false;
uint8_t testSeed = 0;
};
inline bool pack(const std::string& path, const PackRequest& req,
std::string& err) {
if (req.models.empty()) {
err = "a package with no model describes nothing";
return false;
}
// Everything the package's secrecy rests on, drawn up front: the content
// key, the ephemeral private key it is wrapped with, the wrap nonce and one
// nonce base per sealed part. From the OS CSPRNG — anything a holder of the
// file could re-derive would open it without the base key.
Key contentKey{};
Key esk{};
std::array<uint8_t, aead::kNonceBytes> wrapNonce{};
std::array<uint8_t, 4> manifestBase{};
std::vector<std::array<uint8_t, 4>> bases(req.models.size());
struct Wipe {
Key& a;
Key& b;
~Wipe() {
crypto_wipe(a.data(), a.size());
crypto_wipe(b.data(), b.size());
}
} wipe{contentKey, esk};
if (req.insecureDeterministicForTests) {
#if defined(ANSMODEL_DETERMINISTIC_PACK_FOR_TESTS)
for (size_t i = 0; i < contentKey.size(); ++i)
contentKey[i] = uint8_t(req.testSeed * 31 + i * 7 + 11);
for (size_t i = 0; i < esk.size(); ++i)
esk[i] = uint8_t(req.testSeed * 13 + i * 5 + 3);
for (size_t i = 0; i < wrapNonce.size(); ++i)
wrapNonce[i] = uint8_t(req.testSeed + 100 + i);
manifestBase = {uint8_t(req.testSeed + 1), 2, 3, 4};
for (size_t i = 0; i < bases.size(); ++i)
bases[i] = {uint8_t(req.testSeed + 10 + i), 5, 6, 7};
#else
err = "deterministic packing is for tests only, and this build is not one";
return false;
#endif
} else {
bool ok = ANSCENTER::randomBytes(contentKey.data(), contentKey.size()) &&
ANSCENTER::randomBytes(esk.data(), esk.size()) &&
ANSCENTER::randomBytes(wrapNonce.data(), wrapNonce.size()) &&
ANSCENTER::randomBytes(manifestBase.data(), manifestBase.size());
// The parts share the content key, so their bases must differ: two parts
// on one base would reuse every chunk nonce they have in common.
for (size_t i = 0; ok && i < bases.size(); ++i) {
bool clash = true;
while (ok && clash) {
ok = ANSCENTER::randomBytes(bases[i].data(), bases[i].size());
clash = bases[i] == manifestBase;
for (size_t j = 0; j < i; ++j) clash = clash || bases[i] == bases[j];
}
}
if (!ok) {
err = "the operating system gave no random bytes, so nothing was sealed";
return false;
}
}
auto sealChunked = [&](const std::vector<uint8_t>& plain,
const std::string& payloadId,
const std::array<uint8_t, 4>& base,
std::string& blob) {
const uint64_t chunks =
plain.empty() ? 1 : (plain.size() + req.chunk - 1) / req.chunk;
for (uint64_t i = 0; i < chunks; ++i) {
const size_t from = size_t(i * req.chunk);
const size_t take = std::min<size_t>(req.chunk, plain.size() - from);
std::vector<uint8_t> part(plain.begin() + from, plain.begin() + from + take);
const auto sealed =
aead::seal(contentKey, detail::chunkNonce(base, i),
detail::chunkAad(req.keyId, payloadId, i, chunks), part);
blob.append(reinterpret_cast<const char*>(sealed.data()), sealed.size());
}
};
// The manifest first, then each model, so the header can name their offsets.
const std::string manifestText = req.manifest.dump();
const std::vector<uint8_t> manifestBytes(manifestText.begin(),
manifestText.end());
std::string manifestBlob;
{
const auto sealed =
aead::seal(contentKey, detail::chunkNonce(manifestBase, 0),
detail::chunkAad(req.keyId, "manifest", 0, 1),
manifestBytes);
manifestBlob.assign(reinterpret_cast<const char*>(sealed.data()),
sealed.size());
}
std::vector<std::string> blobs;
for (size_t i = 0; i < req.models.size(); ++i) {
std::string blob;
sealChunked(req.models[i].bytes, req.models[i].id, bases[i], blob);
blobs.push_back(std::move(blob));
}
// Wrap the content key to the base key. One wrap here; the header's shape
// takes several, which is how a package opens on more than one key.
const std::vector<uint8_t> keyPlain(contentKey.begin(), contentKey.end());
Key wrapKey = req.key;
std::vector<uint8_t> epk;
if (req.useX25519) {
// An ephemeral key pair per package: its public half travels in the
// header, its private half dies here, and the packer never holds anything
// that opens the result.
epk.assign(32, 0);
crypto_x25519_public_key(epk.data(), esk.data());
uint8_t shared[32];
crypto_x25519(shared, esk.data(), req.basePublic.data());
wrapKey = detail::deriveWrapKey(shared, req.keyId);
crypto_wipe(shared, sizeof(shared));
crypto_wipe(esk.data(), esk.size());
}
const auto wrapped = aead::seal(wrapKey, wrapNonce,
"ansmodel/v1|wrap|" + req.keyId, keyPlain);
// Header offsets are known once the sizes are: the header is written first,
// so its own length has to be settled before the offsets inside it. Build it
// with placeholder offsets, measure, then fill them in — the size cannot
// change, because the numbers are written with a fixed width.
auto buildHeader = [&](uint64_t headerLen) {
Json h;
h["format"] = "ansmodel";
h["version"] = int(kVersion);
Json wrap{{"keyId", req.keyId},
{"alg", req.useX25519 ? kWrapX25519 : kWrapSymmetric},
{"nonce", detail::b64(std::vector<uint8_t>(wrapNonce.begin(),
wrapNonce.end()))},
{"ct", detail::b64(wrapped)}};
if (req.useX25519) wrap["epk"] = detail::b64(epk);
h["wraps"] = Json::array({wrap});
uint64_t at = 16 + headerLen;
h["manifest"] = Json{{"off", at},
{"len", uint64_t(manifestBlob.size())},
{"nonceBase", detail::b64(std::vector<uint8_t>(
manifestBase.begin(), manifestBase.end()))}};
at += manifestBlob.size();
Json payloads = Json::array();
for (size_t i = 0; i < blobs.size(); ++i) {
payloads.push_back(Json{
{"id", req.models[i].id},
{"off", at},
{"len", uint64_t(blobs[i].size())},
{"chunk", req.chunk},
{"nonceBase", detail::b64(std::vector<uint8_t>(bases[i].begin(),
bases[i].end()))},
{"sha256", bits::sha256Hex(std::string(blobs[i].begin(), blobs[i].end()))}});
at += blobs[i].size();
}
h["payloads"] = payloads;
if (!req.card.is_null()) h["card"] = req.card;
// The signature is written last, over everything above: `sig` is added
// with a placeholder of the exact length the real one takes, so signing
// cannot change the header's size and the offsets stay true.
if (req.signerSecret.size() == 64 && req.signerPublic.size() == 32) {
h["sig"] = Json{{"alg", kSigEd25519},
{"value", std::string(88, 'A')},
{"cert", Json{{"keyId", req.signerId},
{"pub", detail::b64(req.signerPublic)},
{"notAfter", req.notAfter},
{"rootKeyId", req.rootKeyId},
{"rootSig", detail::b64(req.rootSig)}}}};
} else {
h["sig"] = Json{{"alg", "none"}, {"value", ""}};
}
return h;
};
// Two passes: the first measures, the second is written. Offsets are plain
// JSON numbers, so a longer offset could lengthen the header — hence the
// loop, which settles in one extra step.
std::string headerText = buildHeader(0).dump();
for (int i = 0; i < 4; ++i) {
const std::string next = buildHeader(headerText.size()).dump();
if (next.size() == headerText.size()) {
headerText = next;
break;
}
headerText = next;
}
Json header = buildHeader(headerText.size());
if (req.signerSecret.size() == 64) {
// Sign the header as the reader will read it: without `sig`, canonically
// serialised. The placeholder above keeps the length identical, so putting
// the real signature in changes no offset.
const std::string sb = detail::signedBytes(header);
std::vector<uint8_t> value(64, 0);
crypto_ed25519_sign(value.data(), req.signerSecret.data(),
reinterpret_cast<const uint8_t*>(sb.data()), sb.size());
header["sig"]["value"] = detail::b64(value);
}
headerText = header.dump();
std::string file(kMagic, sizeof(kMagic));
detail::wr16(file, kVersion);
detail::wr16(file, req.models.size() > 1 ? kFlagMultiModel : uint16_t(0));
detail::wr32(file, uint32_t(headerText.size()));
file += headerText;
file += manifestBlob;
for (const auto& b : blobs) file += b;
std::ofstream o(path, std::ios::binary | std::ios::trunc);
if (!o) {
err = "cannot write " + path;
return false;
}
o.write(file.data(), std::streamsize(file.size()));
if (!o) {
err = "failed while writing " + path;
return false;
}
return true;
}
} // namespace modelpkg
} // namespace ANSCENTER
#endif // ANSMODELPACKAGE_H
+354
View File
@@ -0,0 +1,354 @@
#ifndef ANSONNXSHAPE_H
#define ANSONNXSHAPE_H
#pragma once
// ANSOnnxShape — the input geometry a model declares, read off the graph.
//
// WHY A READER NEEDS THIS. A node has to know what its model expects before it
// can preprocess a single frame, and until now that answer came from whatever
// the bundle happened to carry: model_config.json's input_size in a folder, an
// `input` object in a package manifest. Neither is present on every model. The
// graph always is, and the graph is the thing the session is built from — so
// where the metadata is silent, ask the model itself rather than assume 640.
//
// TWO SOURCES, ONE PARSER. A package's model is already in memory, decrypted,
// and must never reach the disk; an imported folder's model is a file that may
// be hundreds of megabytes. The template below reads either, and the file case
// SEEKS past the nodes and the initializers rather than reading them — the
// fields this wants sit after all of the weight on the wire.
//
// The path through the schema, for anyone checking this against onnx.proto:
//
// ModelProto.graph field 7, length-delimited
// GraphProto.input field 11, repeated (after node and
// initializer on the wire)
// GraphProto.output field 12, repeated
// ValueInfoProto.type field 2
// TypeProto.tensor_type field 1
// TypeProto.Tensor.shape field 2
// TensorShapeProto.dim field 1, repeated
// Dimension.dim_value field 1, varint (dim_param means the
// axis is dynamic: reported as 0)
// ModelProto.metadata_props field 14, repeated StringStringEntryProto
// StringStringEntryProto.key field 1, string
// StringStringEntryProto.value field 2, string
//
// An axis the graph leaves open is reported as zero, and a file that cannot be
// parsed is reported as a failure. Both mean the same thing to a caller: this
// model does not say, keep whatever default you had. Being wrong about a
// model's input size is worse than being silent about it.
#include <cstdint>
#include <cctype>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
namespace ANSCENTER {
namespace onnxshape {
struct InputShape {
int channels = 0; // 0 where the graph does not fix that axis
int height = 0;
int width = 0;
bool known() const { return height > 0 && width > 0; }
};
// What the whole graph says about itself: what goes in, and what shape comes
// out — which is the difference between a head that needs decoding and one
// that has already done it.
struct GraphShape {
InputShape input;
std::vector<int64_t> output; // the FIRST output's dims; empty when unknown
bool exportSaysEndToEnd = false; // the model's own metadata, when it says
// AN END-TO-END HEAD, [1, N, 6]: rows of (x1 y1 x2 y2 score class) with the
// NMS already inside the graph. Telling one apart from an ordinary YOLO head
// matters more than it looks: feeding [1,N,6] to a v8 decoder yields boxes
// everywhere with coordinates read as confidences — plausible-looking
// garbage rather than an error.
//
// The trap is a two-class v8 export, whose [1, 4+nc, N] is [1, 6, 8400] —
// a 6 in the shape, and not end-to-end. So the 6 must be the LAST axis, and
// the axis before it must be a detection count rather than an anchor count;
// an end-to-end head emits max_det rows, which is hundreds, never thousands.
// When the export's own metadata says end2end, that settles it outright.
bool endToEnd() const {
if (exportSaysEndToEnd) return true;
return output.size() == 3 && output[0] == 1 && output[2] == 6 &&
output[1] > 0 && output[1] <= 1000;
}
};
namespace detail {
// A model already in memory: the decrypted contents of a package.
struct MemSource {
const uint8_t* p = nullptr;
int64_t size = 0;
int64_t at = 0;
int64_t tell() const { return at; }
void seek(int64_t to) { at = to < 0 ? 0 : (to > size ? size : to); }
int get() { return at < size ? int(p[at++]) : -1; }
bool ok() const { return true; }
};
// A model on disk, walked without being read.
struct FileSource {
std::ifstream f;
int64_t size = 0;
int64_t tell() { return int64_t(f.tellg()); }
void seek(int64_t to) { f.clear(); f.seekg(std::streamoff(to), std::ios::beg); }
int get() { return f.get(); }
bool ok() { return bool(f); }
};
template <class Src>
bool varint(Src& s, uint64_t& out) {
out = 0;
for (int shift = 0; shift < 64; shift += 7) {
const int c = s.get();
if (c < 0) return false;
out |= uint64_t(c & 0x7f) << shift;
if ((c & 0x80) == 0) return true;
}
return false;
}
template <class Src>
bool tag(Src& s, uint32_t& field, uint32_t& wire) {
uint64_t key = 0;
if (!varint(s, key)) return false;
field = uint32_t(key >> 3);
wire = uint32_t(key & 0x7);
return field != 0;
}
// Step over a field whose contents are of no interest.
template <class Src>
bool skip(Src& s, uint32_t wire) {
uint64_t v = 0;
switch (wire) {
case 0: return varint(s, v);
case 1: s.seek(s.tell() + 8); return s.ok();
case 5: s.seek(s.tell() + 4); return s.ok();
case 2:
if (!varint(s, v)) return false;
s.seek(s.tell() + int64_t(v));
return s.ok();
default: return false; // groups: not used anywhere in onnx.proto
}
}
// Find one length-delimited field inside [here, end) and stop on its payload.
template <class Src>
bool seekField(Src& s, int64_t end, uint32_t want, uint64_t& len) {
while (s.ok() && s.tell() >= 0 && s.tell() < end) {
uint32_t field = 0, wire = 0;
if (!tag(s, field, wire)) return false;
if (field == want && wire == 2) return varint(s, len);
if (!skip(s, wire)) return false;
}
return false;
}
// TensorShapeProto: the dimensions in order, a dynamic axis as 0.
template <class Src>
std::vector<int64_t> dims(Src& s, int64_t end) {
std::vector<int64_t> out;
while (s.ok() && s.tell() >= 0 && s.tell() < end) {
uint32_t field = 0, wire = 0;
if (!tag(s, field, wire)) break;
if (field != 1 || wire != 2) { // only `dim` matters
if (!skip(s, wire)) break;
continue;
}
uint64_t len = 0;
if (!varint(s, len)) break;
const int64_t dimEnd = s.tell() + int64_t(len);
int64_t value = 0; // stays 0 for dim_param, i.e. a dynamic axis
while (s.ok() && s.tell() >= 0 && s.tell() < dimEnd) {
uint32_t df = 0, dw = 0;
if (!tag(s, df, dw)) break;
if (df == 1 && dw == 0) {
uint64_t v = 0;
if (!varint(s, v)) break;
value = int64_t(v);
} else if (!skip(s, dw)) {
break;
}
}
s.seek(dimEnd);
out.push_back(value);
}
return out;
}
// One ValueInfoProto (a graph input or output), given a stream on its payload:
// its dims, or empty when it is not a shaped tensor.
template <class Src>
std::vector<int64_t> valueDims(Src& s, int64_t valueEnd) {
uint64_t len = 0;
if (!seekField(s, valueEnd, 2, len)) return {}; // ValueInfoProto.type
const int64_t typeEnd = s.tell() + int64_t(len);
if (!seekField(s, typeEnd, 1, len)) return {}; // TypeProto.tensor_type
const int64_t tensorEnd = s.tell() + int64_t(len);
if (!seekField(s, tensorEnd, 2, len)) return {}; // Tensor.shape
return dims(s, s.tell() + int64_t(len));
}
// The graph's first input and first output, and whether the export declared
// itself end-to-end. `end` is the whole file; `graphStart` lets the output be
// found after the input, since both hang off the same graph.
template <class Src>
bool readGraph(Src& s, int64_t end, GraphShape& out, std::string& why) {
uint64_t len = 0;
if (!seekField(s, end, 7, len)) { // ModelProto.graph
why = "this does not look like an ONNX model";
return false;
}
const int64_t graphStart = s.tell();
const int64_t graphEnd = graphStart + int64_t(len);
if (!seekField(s, graphEnd, 11, len)) { // GraphProto.input
why = "the graph declares no input";
return false;
}
const std::vector<int64_t> in = valueDims(s, s.tell() + int64_t(len));
// The output, from the graph's start again: it sits after the input on the
// wire, but seeking from the start costs nothing and does not depend on it.
s.seek(graphStart);
if (seekField(s, graphEnd, 12, len)) // GraphProto.output
out.output = valueDims(s, s.tell() + int64_t(len));
// The export's own word on its head, which beats any shape rule.
s.seek(0);
while (s.ok() && s.tell() >= 0 && s.tell() < end) {
uint32_t field = 0, wire = 0;
if (!tag(s, field, wire)) break;
if (field != 14 || wire != 2) { // ModelProto.metadata_props
if (!skip(s, wire)) break;
continue;
}
uint64_t entryLen = 0;
if (!varint(s, entryLen)) break;
const int64_t entryEnd = s.tell() + int64_t(entryLen);
std::string key, value;
while (s.ok() && s.tell() >= 0 && s.tell() < entryEnd) {
uint32_t ef = 0, ew = 0;
if (!tag(s, ef, ew)) break;
if ((ef == 1 || ef == 2) && ew == 2) {
uint64_t n = 0;
if (!varint(s, n)) break;
std::string text;
for (uint64_t i = 0; i < n; ++i) {
const int c = s.get();
if (c < 0) break;
text.push_back(char(c));
}
(ef == 1 ? key : value) = text;
} else if (!skip(s, ew)) {
break;
}
}
s.seek(entryEnd);
if (key == "end2end") {
for (char& c : value) c = char(::tolower(static_cast<unsigned char>(c)));
out.exportSaysEndToEnd = (value == "true" || value == "1");
}
}
// NCHW is the only layout these engines run.
if (in.size() != 4) {
why = "the input is not a four-dimensional image tensor";
return false;
}
auto axis = [](int64_t v) { return (v > 0 && v < 1000000) ? int(v) : 0; };
out.input.channels = axis(in[1]);
out.input.height = axis(in[2]);
out.input.width = axis(in[3]);
if (!out.input.known()) {
why = "the model takes a dynamic input size";
return false;
}
return true;
}
template <class Src>
bool read(Src& s, int64_t end, InputShape& out, std::string& why) {
GraphShape g;
if (!readGraph(s, end, g, why)) return false;
out = g.input;
return true;
}
} // namespace detail
// What a model already in memory declares.
inline bool fromMemory(const void* data, size_t size, InputShape& out,
std::string& why) {
if (!data || size == 0) {
why = "there is no model to read";
return false;
}
detail::MemSource s{static_cast<const uint8_t*>(data), int64_t(size), 0};
return detail::read(s, int64_t(size), out, why);
}
// The whole graph, from a model already in memory.
inline bool graphFromMemory(const void* data, size_t size, GraphShape& out,
std::string& why) {
if (!data || size == 0) {
why = "there is no model to read";
return false;
}
detail::MemSource s{static_cast<const uint8_t*>(data), int64_t(size), 0};
return detail::readGraph(s, int64_t(size), out, why);
}
// What a model on disk declares. The file is walked, not loaded.
inline bool fromFile(const std::string& path, InputShape& out,
std::string& why) {
std::error_code ec;
const auto size = std::filesystem::file_size(path, ec);
if (ec || size == 0) {
why = "the model is not there to read";
return false;
}
detail::FileSource s;
s.f.open(path, std::ios::binary);
s.size = int64_t(size);
if (!s.f) {
why = "the model could not be opened";
return false;
}
return detail::read(s, int64_t(size), out, why);
}
// The whole graph, from a model on disk. Walked, not loaded.
inline bool graphFromFile(const std::string& path, GraphShape& out,
std::string& why) {
std::error_code ec;
const auto size = std::filesystem::file_size(path, ec);
if (ec || size == 0) {
why = "the model is not there to read";
return false;
}
detail::FileSource s;
s.f.open(path, std::ios::binary);
s.size = int64_t(size);
if (!s.f) {
why = "the model could not be opened";
return false;
}
return detail::readGraph(s, int64_t(size), out, why);
}
} // namespace onnxshape
} // namespace ANSCENTER
#endif // ANSONNXSHAPE_H
+326
View File
@@ -0,0 +1,326 @@
#ifndef ANSPACKAGEBITS_H
#define ANSPACKAGEBITS_H
#pragma once
// ANSPackageBits — base64, SHA-256 and HMAC-SHA256 for the model package.
//
// TAKEN FROM ANSMINDCORE (src/core/base64.hpp, src/core/sha256.hpp) unchanged,
// for the same reason the AEAD is: a package written by one product must open
// in the other, and two implementations of the same transform are two chances
// to disagree. The suite checks these against the published vectors.
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
namespace ANSCENTER {
namespace bits {
// core/base64 — RFC 4648 base64, encode and decode.
//
// Needed by ONVIF's WS-Security UsernameToken (the nonce and the password
// digest both travel base64-encoded) and useful anywhere bytes must cross a
// text channel. Decode is strict about alphabet and padding: this parses
// values that arrive from a camera on the network, and quietly accepting
// malformed input is how a length assumption downstream turns into a bug.
#include <array>
#include <cstdint>
#include <string>
#include <vector>
// ── base64 ───────────────────────────────────────────────────────────────
inline std::string base64Encode(const uint8_t* p, size_t n) {
static const char* kAlpha =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string out;
out.reserve(((n + 2) / 3) * 4);
size_t i = 0;
for (; i + 2 < n; i += 3) {
const uint32_t v = uint32_t(p[i]) << 16 | uint32_t(p[i + 1]) << 8 | p[i + 2];
out += kAlpha[(v >> 18) & 0x3F];
out += kAlpha[(v >> 12) & 0x3F];
out += kAlpha[(v >> 6) & 0x3F];
out += kAlpha[v & 0x3F];
}
if (i + 1 == n) {
const uint32_t v = uint32_t(p[i]) << 16;
out += kAlpha[(v >> 18) & 0x3F];
out += kAlpha[(v >> 12) & 0x3F];
out += "==";
} else if (i + 2 == n) {
const uint32_t v = uint32_t(p[i]) << 16 | uint32_t(p[i + 1]) << 8;
out += kAlpha[(v >> 18) & 0x3F];
out += kAlpha[(v >> 12) & 0x3F];
out += kAlpha[(v >> 6) & 0x3F];
out += '=';
}
return out;
}
inline std::string base64Encode(const std::string& s) {
return base64Encode(reinterpret_cast<const uint8_t*>(s.data()), s.size());
}
template <size_t N>
inline std::string base64Encode(const std::array<uint8_t, N>& a) {
return base64Encode(a.data(), a.size());
}
// Returns false on any character outside the alphabet, bad padding, or a
// length that is not a multiple of 4. Whitespace is skipped: XML puts
// newlines inside base64 elements.
inline bool base64Decode(const std::string& in, std::vector<uint8_t>& out) {
auto val = [](char c) -> int {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
};
constexpr int kPad = -2;
out.clear();
int quad[4] = {0, 0, 0, 0};
int have = 0;
bool padded = false; // once padding starts, only more padding may follow
for (char c : in) {
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') continue;
int v;
if (c == '=') {
v = kPad;
padded = true;
} else {
if (padded) return false; // data after padding
v = val(c);
if (v < 0) return false;
}
quad[have++] = v;
if (have < 4) continue;
// Padding is only legal in the last one or two positions.
if (quad[0] == kPad || quad[1] == kPad) return false;
if (quad[2] == kPad && quad[3] != kPad) return false;
const int pad = (quad[2] == kPad ? 2 : 0) + (quad[2] != kPad &&
quad[3] == kPad ? 1 : 0);
const uint32_t acc = uint32_t(quad[0]) << 18 | uint32_t(quad[1]) << 12 |
uint32_t(pad >= 2 ? 0 : quad[2]) << 6 |
uint32_t(pad >= 1 ? 0 : quad[3]);
out.push_back(uint8_t(acc >> 16));
if (pad < 2) out.push_back(uint8_t(acc >> 8));
if (pad < 1) out.push_back(uint8_t(acc));
have = 0;
}
return have == 0; // a trailing partial group is malformed
}
// core/sha256 — SHA-256 and HMAC-SHA256, in-tree.
//
// WHY IN-TREE. The only consumer is AWS Signature Version 4 request signing
// for the S3 evidence store. Linking OpenSSL for two hash functions would put
// a versioned crypto dependency into the worker's Depends on every platform
// we ship — the same trap the bundled ONNX Runtime exists to avoid — in
// exchange for ~150 lines of algorithm that has fixed, published test
// vectors. The suite checks this against the FIPS 180-4 and RFC 4231 vectors,
// so it is verified rather than merely present.
//
// NOT a general crypto library. It exists to sign requests: there is no
// constant-time comparison here, and nothing in this file should ever be
// used for password hashing or key derivation from a low-entropy secret.
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
// ── SHA-256 and HMAC-SHA256 ──────────────────────────────────────────────
class Sha256 {
public:
static constexpr size_t kDigestLen = 32;
Sha256() { reset(); }
void reset() {
len_ = 0;
bufLen_ = 0;
static const uint32_t kInit[8] = {0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u,
0xa54ff53au, 0x510e527fu, 0x9b05688cu,
0x1f83d9abu, 0x5be0cd19u};
std::memcpy(h_, kInit, sizeof h_);
}
void update(const uint8_t* p, size_t n) {
len_ += n;
while (n) {
const size_t take = (n < 64 - bufLen_) ? n : (64 - bufLen_);
std::memcpy(buf_ + bufLen_, p, take);
bufLen_ += take;
p += take;
n -= take;
if (bufLen_ == 64) {
block(buf_);
bufLen_ = 0;
}
}
}
void update(const std::string& s) {
update(reinterpret_cast<const uint8_t*>(s.data()), s.size());
}
// Consumes the state; call reset() to reuse.
void finish(uint8_t out[kDigestLen]) {
const uint64_t bits = uint64_t(len_) * 8; // captured BEFORE padding
uint8_t b = 0x80;
update(&b, 1);
b = 0;
while (bufLen_ != 56) update(&b, 1);
uint8_t lenBe[8];
for (int i = 0; i < 8; ++i) lenBe[i] = uint8_t(bits >> (56 - 8 * i));
update(lenBe, 8);
for (int i = 0; i < 8; ++i) {
out[i * 4 + 0] = uint8_t(h_[i] >> 24);
out[i * 4 + 1] = uint8_t(h_[i] >> 16);
out[i * 4 + 2] = uint8_t(h_[i] >> 8);
out[i * 4 + 3] = uint8_t(h_[i]);
}
}
private:
static uint32_t rotr(uint32_t x, int n) {
return (x >> n) | (x << (32 - n));
}
void block(const uint8_t* p) {
// First 32 bits of the fractional parts of the cube roots of the first
// 64 primes (FIPS 180-4 §4.2.2).
static const uint32_t k[64] = {
0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu,
0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u, 0xd807aa98u, 0x12835b01u,
0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u,
0xc19bf174u, 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu,
0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, 0x983e5152u,
0xa831c66du, 0xb00327c8u, 0xbf597fc7u, 0xc6e00bf3u, 0xd5a79147u,
0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu,
0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u,
0xd6990624u, 0xf40e3585u, 0x106aa070u, 0x19a4c116u, 0x1e376c08u,
0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu,
0x682e6ff3u, 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u,
0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u};
uint32_t w[64];
for (int i = 0; i < 16; ++i)
w[i] = uint32_t(p[i * 4]) << 24 | uint32_t(p[i * 4 + 1]) << 16 |
uint32_t(p[i * 4 + 2]) << 8 | uint32_t(p[i * 4 + 3]);
for (int i = 16; i < 64; ++i) {
const uint32_t s0 =
rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
const uint32_t s1 =
rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
uint32_t a = h_[0], b = h_[1], c = h_[2], d = h_[3];
uint32_t e = h_[4], f = h_[5], g = h_[6], hh = h_[7];
for (int i = 0; i < 64; ++i) {
const uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
const uint32_t ch = (e & f) ^ (~e & g);
const uint32_t t1 = hh + S1 + ch + k[i] + w[i];
const uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
const uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
const uint32_t t2 = S0 + maj;
hh = g; g = f; f = e; e = d + t1;
d = c; c = b; b = a; a = t1 + t2;
}
h_[0] += a; h_[1] += b; h_[2] += c; h_[3] += d;
h_[4] += e; h_[5] += f; h_[6] += g; h_[7] += hh;
}
uint32_t h_[8];
uint8_t buf_[64];
size_t bufLen_ = 0;
uint64_t len_ = 0;
};
using Digest256 = std::array<uint8_t, 32>;
inline Digest256 sha256(const uint8_t* p, size_t n) {
Sha256 s;
s.update(p, n);
Digest256 out{};
s.finish(out.data());
return out;
}
inline Digest256 sha256(const std::string& s) {
return sha256(reinterpret_cast<const uint8_t*>(s.data()), s.size());
}
inline std::string toHex(const uint8_t* p, size_t n) {
static const char* kHex = "0123456789abcdef";
std::string out;
out.reserve(n * 2);
for (size_t i = 0; i < n; ++i) {
out += kHex[p[i] >> 4];
out += kHex[p[i] & 0xF];
}
return out;
}
inline std::string toHex(const Digest256& d) {
return toHex(d.data(), d.size());
}
inline std::string sha256Hex(const uint8_t* p, size_t n) {
return toHex(sha256(p, n));
}
inline std::string sha256Hex(const std::string& s) {
return toHex(sha256(s));
}
// RFC 2104 HMAC with SHA-256.
inline Digest256 hmacSha256(const uint8_t* key, size_t keyLen,
const uint8_t* msg, size_t msgLen) {
uint8_t k[64] = {0};
if (keyLen > 64) {
Sha256 s;
s.update(key, keyLen);
s.finish(k); // remaining 32 bytes stay zero-padded
} else {
std::memcpy(k, key, keyLen);
}
uint8_t ipad[64], opad[64];
for (int i = 0; i < 64; ++i) {
ipad[i] = uint8_t(k[i] ^ 0x36);
opad[i] = uint8_t(k[i] ^ 0x5c);
}
uint8_t inner[32];
{
Sha256 s;
s.update(ipad, 64);
s.update(msg, msgLen);
s.finish(inner);
}
Digest256 out{};
{
Sha256 s;
s.update(opad, 64);
s.update(inner, 32);
s.finish(out.data());
}
return out;
}
inline Digest256 hmacSha256(const std::string& key, const std::string& msg) {
return hmacSha256(reinterpret_cast<const uint8_t*>(key.data()), key.size(),
reinterpret_cast<const uint8_t*>(msg.data()), msg.size());
}
inline Digest256 hmacSha256(const Digest256& key, const std::string& msg) {
return hmacSha256(key.data(), key.size(),
reinterpret_cast<const uint8_t*>(msg.data()), msg.size());
}
} // namespace bits
} // namespace ANSCENTER
#endif // ANSPACKAGEBITS_H
+48
View File
@@ -0,0 +1,48 @@
# ANSModelPackage — reading an ANS model package (.ansmodel).
#
# Header-only apart from the vendored curves, and deliberately free of the
# heavy dependencies around it: no CUDA, no OpenCV, no LabVIEW. That is what
# lets it build — and be tested — on every platform, including the ones where
# the engine layer does not build at all.
add_library(ANSModelPackage STATIC
${CMAKE_SOURCE_DIR}/3rdparty/monocypher/monocypher.c
${CMAKE_SOURCE_DIR}/3rdparty/monocypher/monocypher-ed25519.c
)
target_include_directories(ANSModelPackage PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/3rdparty/monocypher
)
# nlohmann/json, wherever this machine keeps it: the ANSLibs tree on Windows,
# vcpkg, or the system elsewhere. Asked for by name rather than assumed — the
# Windows branch used to point at vcpkg alone, and on a real build machine the
# header lives under ANSLibs, so every target that linked this one failed to
# find it while the engines, which add ANSLibs themselves, compiled fine.
find_path(ANSCORE_NLOHMANN_INCLUDE nlohmann/json.hpp
HINTS ${ANSLIBS_DIR} ${VCPKG_INCLUDE_DIR}
/opt/homebrew/include /usr/local/include /usr/include)
if(ANSCORE_NLOHMANN_INCLUDE)
target_include_directories(ANSModelPackage PUBLIC ${ANSCORE_NLOHMANN_INCLUDE})
message(STATUS "ANSModelPackage: nlohmann/json at ${ANSCORE_NLOHMANN_INCLUDE}")
else()
message(WARNING "nlohmann/json.hpp not found — ANSModelPackage will not build")
endif()
# The model-package key, decided at configure time: see ANSModelKeys.h for why
# this is not an __has_include test. PUBLIC so every engine that opens a
# package is compiled with the same answer.
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/ANSModelKeysGenerated.h")
target_compile_definitions(ANSModelPackage PUBLIC ANSCORE_HAVE_BUILTIN_MODEL_KEYS=1)
message(STATUS "ANSModelPackage: key present — packages can be opened")
else()
message(STATUS "ANSModelPackage: no key — packages cannot be opened "
"(run ansmodel-keys emit --product anscore, then re-run cmake)")
endif()
# BCryptGenRandom, behind the content key and nonces pack() draws (ANSCsprng.h).
if(WIN32)
target_link_libraries(ANSModelPackage PUBLIC bcrypt)
endif()
set_target_properties(ANSModelPackage PROPERTIES POSITION_INDEPENDENT_CODE ON)
+56 -4
View File
@@ -337,6 +337,8 @@ const char* HardwareId::GetCurrentHardwareId()
bits.WriteUInt16(hash1); bits.WriteUInt16(hash1);
bool uuidFound = (hash1 != h0);
// CPU IDs - get up to 4 for better resilience // CPU IDs - get up to 4 for better resilience
propList.clear(); propList.clear();
@@ -351,6 +353,8 @@ const char* HardwareId::GetCurrentHardwareId()
cpu = NormalizeHardwareString(cpu); cpu = NormalizeHardwareString(cpu);
} }
int cpuCount = (int)propList.size();
hash1 = (propList.size() > 0) ? HashString(propList.front().c_str()) : h0; hash1 = (propList.size() > 0) ? HashString(propList.front().c_str()) : h0;
hash2 = (propList.size() > 1) ? HashString(propList.back().c_str()) : h0; hash2 = (propList.size() > 1) ? HashString(propList.back().c_str()) : h0;
@@ -379,6 +383,8 @@ const char* HardwareId::GetCurrentHardwareId()
} }
propList.sort(); propList.sort();
int macCount = (int)propList.size();
// Store up to 4 MAC hashes // Store up to 4 MAC hashes
auto macIter = propList.begin(); auto macIter = propList.begin();
hash1 = (propList.size() > 0 && macIter != propList.end()) ? HashString((macIter++)->c_str()) : h0; hash1 = (propList.size() > 0 && macIter != propList.end()) ? HashString((macIter++)->c_str()) : h0;
@@ -406,6 +412,8 @@ const char* HardwareId::GetCurrentHardwareId()
} }
propList.sort(); propList.sort();
int hddCount = (int)propList.size();
auto hddIter = propList.begin(); auto hddIter = propList.begin();
hash1 = (propList.size() > 0 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0; hash1 = (propList.size() > 0 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0;
hash2 = (propList.size() > 1 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0; hash2 = (propList.size() > 1 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0;
@@ -432,11 +440,34 @@ const char* HardwareId::GetCurrentHardwareId()
insertPos += 6; insertPos += 6;
} }
// Field-diagnosability: log the component availability once per distinct
// result so support can tell a weak fingerprint from a healthy one
static string lastLoggedHwid;
if (hardwareId != lastLoggedHwid)
{
lastLoggedHwid = hardwareId;
fprintf(stderr, "[anslicensing] hardware id %s (components: uuid=%s cpu=%d mac=%d hdd=%d)\n",
hardwareId.c_str(), uuidFound ? "ok" : "none", cpuCount, macCount, hddCount);
if (!uuidFound && cpuCount == 0 && macCount == 0)
fprintf(stderr, "[anslicensing] WARNING: no hardware components readable via WMI - hardware id is NOT machine-specific. "
"Check WMI health: winmgmt /verifyrepository (then winmgmt /salvagerepository if inconsistent)\n");
}
return hardwareId.c_str(); return hardwareId.c_str();
} }
bool HardwareId::MatchCurrentHardwareId(const char* hwid) bool HardwareId::MatchCurrentHardwareId(const char* hwid)
{ {
// An HWID identical to the one this machine generates right now is by
// definition this computer. The points-based matching below can score
// under REQUIRED_POINTS on machines with an invalid SMBIOS UUID and few
// fingerprintable components (no PCI NIC, no readable disk serial), which
// would reject even a freshly generated local HWID.
const char* currentHwid = GetCurrentHardwareId();
if (currentHwid != NULL && strcmp(hwid, currentHwid) == 0)
return true;
#ifdef _WIN32 #ifdef _WIN32
WmiHelper wmi; WmiHelper wmi;
#endif #endif
@@ -464,8 +495,14 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
auto buf = base32.decode(hardwareId.c_str(), hardwareId.length(), &bufLen); auto buf = base32.decode(hardwareId.c_str(), hardwareId.length(), &bufLen);
// The field layout is 147 bits but the HWID only encodes 16 bytes; zero-pad
// the buffer so reading the trailing (truncated) HDD fields stays in bounds
// and yields deterministic values instead of an out-of-bounds read.
if (buf.size() < 20)
buf.resize(20, 0);
BitStream3 bits; BitStream3 bits;
bits.Attach(buf.data(), 125); bits.Attach(buf.data(), 160);
unsigned char version = 0; unsigned char version = 0;
bits.Read(&version, 3); bits.Read(&version, 3);
@@ -479,6 +516,7 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
// Use a points-based matching system for resilience // Use a points-based matching system for resilience
int matchPoints = 0; int matchPoints = 0;
bool uuidMatched = false;
const int REQUIRED_POINTS = 2; // Need at least 2 components to match const int REQUIRED_POINTS = 2; // Need at least 2 components to match
// Read UUID hash // Read UUID hash
@@ -511,6 +549,7 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
} }
} }
matchPoints += 2; // UUID/Memory match gives 2 points matchPoints += 2; // UUID/Memory match gives 2 points
uuidMatched = true;
} }
else else
{ {
@@ -518,9 +557,11 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
#ifdef _WIN32 #ifdef _WIN32
wmi.GetPropertyList("SELECT * FROM Win32_ComputerSystemProduct", "UUID", &propList); wmi.GetPropertyList("SELECT * FROM Win32_ComputerSystemProduct", "UUID", &propList);
#endif #endif
if (propList.size() > 0) // The generator hashes the last valid UUID entry, so accept a match
// against any entry rather than only the first one
for (list<string>::iterator iter = propList.begin(); iter != propList.end(); iter++)
{ {
string s = NormalizeHardwareString(propList.front()); string s = NormalizeHardwareString(*iter);
if ((s.length() > 1) if ((s.length() > 1)
&& (s != "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") && (s != "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
@@ -529,6 +570,8 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
if (hash1 != h0 && hash1 == HashString(s.c_str())) if (hash1 != h0 && hash1 == HashString(s.c_str()))
{ {
matchPoints += 2; // UUID match is worth 2 points (most stable) matchPoints += 2; // UUID match is worth 2 points (most stable)
uuidMatched = true;
break;
} }
} }
} }
@@ -639,5 +682,14 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
// - UUID (2) + MAC (1) = 3 points ✓ // - UUID (2) + MAC (1) = 3 points ✓
// - UUID (2) + HDD (1) = 3 points ✓ // - UUID (2) + HDD (1) = 3 points ✓
// - CPU (1) + MAC (1) + HDD (1) = 3 points ✓ (if UUID changed but hardware same) // - CPU (1) + MAC (1) + HDD (1) = 3 points ✓ (if UUID changed but hardware same)
return matchPoints >= REQUIRED_POINTS; bool accepted = (matchPoints >= REQUIRED_POINTS);
// Only reached when the exact-string precheck missed, i.e. the stored HWID
// differs from the one this machine generates now. Log the breakdown so a
// status-12 rejection in the field is diagnosable from the console output.
fprintf(stderr, "[anslicensing] hardware id fuzzy match for %s: uuid=%s cpuMatches=%d macMatches=%d hddMatches=%d -> %d point(s), %d required -> %s\n",
hwid, uuidMatched ? "match" : "no-match", cpuMatches, macMatches, hddMatches,
matchPoints, REQUIRED_POINTS, accepted ? "accepted" : "REJECTED");
return accepted;
} }
+27 -2
View File
@@ -2,6 +2,7 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <cstdio>
#include "except.h" #include "except.h"
#include "template.h" #include "template.h"
@@ -179,6 +180,8 @@ public:
catch (Exception * ex) catch (Exception * ex)
{ {
ex->Destroy(); ex->Destroy();
fprintf(stderr, "[anslicensing] activation key is %s (SetKey failed) - product needs (re)activation\n",
activationKey.empty() ? "empty" : "malformed");
activationStatus = STATUS_INVALID_ACTIVATION_KEY; activationStatus = STATUS_INVALID_ACTIVATION_KEY;
if (ownActivationTemplate) delete activationTemplate; if (ownActivationTemplate) delete activationTemplate;
return false; return false;
@@ -186,6 +189,7 @@ public:
if (!validator.IsKeyValid()) if (!validator.IsKeyValid())
{ {
fprintf(stderr, "[anslicensing] activation key does not validate against this license key + hardware id\n");
activationStatus = STATUS_INVALID_ACTIVATION_KEY; activationStatus = STATUS_INVALID_ACTIVATION_KEY;
if (ownActivationTemplate) delete activationTemplate; if (ownActivationTemplate) delete activationTemplate;
return false; return false;
@@ -222,6 +226,7 @@ public:
{ {
if (timeValidationMethod == USE_INTERNET_TIME) // internet time was mandatory if (timeValidationMethod == USE_INTERNET_TIME) // internet time was mandatory
{ {
fprintf(stderr, "[anslicensing] could not obtain internet time (NTP) and internet time is mandatory - check network/firewall\n");
activationStatus = STATUS_NET_ERROR; activationStatus = STATUS_NET_ERROR;
if (ownActivationTemplate) delete activationTemplate; if (ownActivationTemplate) delete activationTemplate;
return false; return false;
@@ -243,6 +248,10 @@ public:
if (m_licenseValidityDays < 0) if (m_licenseValidityDays < 0)
m_licenseValidityDays = 0; m_licenseValidityDays = 0;
fprintf(stderr, "[anslicensing] license expired on %04d-%02d-%02d (grace days remaining: %d)\n",
1900 + licenseExpirationTime.tm_year, 1 + licenseExpirationTime.tm_mon, licenseExpirationTime.tm_mday,
m_licenseValidityDays);
activationStatus = STATUS_LICENSE_EXPIRED; activationStatus = STATUS_LICENSE_EXPIRED;
if (ownActivationTemplate) delete activationTemplate; if (ownActivationTemplate) delete activationTemplate;
@@ -465,6 +474,9 @@ public:
generator.SetValidationData("LicenseKeyValidationData", licenseKeyValidationData.data(), licenseKeyValidationDataLen * 8); generator.SetValidationData("LicenseKeyValidationData", licenseKeyValidationData.data(), licenseKeyValidationDataLen * 8);
} }
fprintf(stderr, "[anslicensing] requesting activation key from licensing service: %s (hwid %s)\n",
activationTemplate->GetLicensingServiceUrl(), hardwareId.c_str());
try { try {
activationKey = generator.GenerateKey(); activationKey = generator.GenerateKey();
} }
@@ -472,19 +484,29 @@ public:
{ {
if ((ex->GetCode() == STATUS_NET_ERROR) && (generator.GetSigningServiceStatusCode() == 406)) if ((ex->GetCode() == STATUS_NET_ERROR) && (generator.GetSigningServiceStatusCode() == 406))
{ {
fprintf(stderr, "[anslicensing] licensing service returned HTTP 406 - payment required\n");
m_licenseValidationResult.SetPaymentRequired(true); m_licenseValidationResult.SetPaymentRequired(true);
ex->Destroy(); ex->Destroy();
} }
else else
{
fprintf(stderr, "[anslicensing] activation request failed (service HTTP status %d) - check network access to the licensing service\n",
generator.GetSigningServiceStatusCode());
throw; throw;
}
} }
if (ValidateActivationKey(activationTemplate.get(), false)) if (ValidateActivationKey(activationTemplate.get(), false))
{ {
fprintf(stderr, "[anslicensing] activation key acquired and validated (service HTTP status %d)\n",
generator.GetSigningServiceStatusCode());
activationStatus = STATUS_SUCCESS; activationStatus = STATUS_SUCCESS;
} }
else else
{
fprintf(stderr, "[anslicensing] licensing service returned an activation key that does not validate for this hardware id\n");
throw new LicensingException(STATUS_GENERIC_ERROR, "internal error - invalid returned activation key"); throw new LicensingException(STATUS_GENERIC_ERROR, "internal error - invalid returned activation key");
}
m_licenseValidationResult.m_license = std::make_unique<LicenseImpl>(); m_licenseValidationResult.m_license = std::make_unique<LicenseImpl>();
@@ -533,8 +555,10 @@ public:
{ {
if (parentUsesWideChar) if (parentUsesWideChar)
{ {
static string hwid = w2s(GetCurrentHardwareIdW(parentLicensingClient)); // per-instance storage: a function-local static would freeze the
return hwid.c_str(); // first client's HWID for the lifetime of the process
currentHwidCache = w2s(GetCurrentHardwareIdW(parentLicensingClient));
return currentHwidCache.c_str();
} }
else else
{ {
@@ -587,6 +611,7 @@ private:
string licenseKey; string licenseKey;
string activationKey; string activationKey;
string hardwareId; string hardwareId;
string currentHwidCache;
string licensingServiceUrl; string licensingServiceUrl;
vector<unsigned char> licenseKeyValidationData; vector<unsigned char> licenseKeyValidationData;
+34 -12
View File
@@ -2,6 +2,7 @@
#define _WIN32_DCOM #define _WIN32_DCOM
#include <iostream> #include <iostream>
#include <cstdio>
#include <comdef.h> #include <comdef.h>
#include <Wbemidl.h> #include <Wbemidl.h>
#include <list> #include <list>
@@ -52,10 +53,12 @@ public:
if (FAILED(hres)) if (FAILED(hres))
{ {
if (ownCoInitialize) // Fail soft: leave wbemServices NULL so queries return empty results.
CoUninitialize(); // Throwing here would escape callers (e.g. hardware id generation)
// that have no exception handling and crash the host application.
throw new exception("failed to create IWbemLocator", hres); ReportFailure("CoCreateInstance(IWbemLocator)", hres);
wbemLocator = NULL;
return;
} }
// Connect to the root\cimv2 namespace with the // Connect to the root\cimv2 namespace with the
@@ -76,12 +79,11 @@ public:
if (FAILED(hres)) if (FAILED(hres))
{ {
ReportFailure("ConnectServer(ROOT\\CIMV2)", hres);
wbemLocator->Release(); wbemLocator->Release();
wbemLocator = NULL;
if (ownCoInitialize) wbemServices = NULL;
CoUninitialize(); return;
throw new exception("could not connect to WMI", hres);
} }
// Set the IWbemServices proxy so that impersonation // Set the IWbemServices proxy so that impersonation
@@ -99,10 +101,12 @@ public:
if (FAILED(hres)) if (FAILED(hres))
{ {
ReportFailure("CoSetProxyBlanket", hres);
wbemServices->Release(); wbemServices->Release();
wbemLocator->Release(); wbemLocator->Release();
if (ownCoInitialize) CoUninitialize(); wbemServices = NULL;
throw new exception("could not connecto to WMI", hres); wbemLocator = NULL;
return;
} }
} }
@@ -115,6 +119,9 @@ public:
void GetPropertyList(const char * wqlQuery, const char * propertyName, list<string> * propList, int maxElements = 0x7FFFFFFF) void GetPropertyList(const char * wqlQuery, const char * propertyName, list<string> * propList, int maxElements = 0x7FFFFFFF)
{ {
if (wbemServices == NULL)
return; // WMI unavailable; already reported by the constructor
HRESULT hres; HRESULT hres;
// For example, query for all the running processes // For example, query for all the running processes
@@ -128,7 +135,8 @@ public:
if (FAILED(hres)) if (FAILED(hres))
{ {
throw new exception("WMI query failed"); ReportFailure(wqlQuery, hres);
return;
} }
else else
{ {
@@ -141,6 +149,14 @@ public:
hres = pEnumerator->Next(WBEM_INFINITE, 1, hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn); &pclsObj, &uReturn);
if (FAILED(hres))
{
// An enumeration error is not "no more rows" — report it,
// otherwise a broken WMI silently yields an empty fingerprint
ReportFailure(wqlQuery, hres);
break;
}
if(0 == uReturn) if(0 == uReturn)
{ {
break; break;
@@ -203,6 +219,12 @@ public:
} }
private: private:
static void ReportFailure(const char * what, HRESULT hres)
{
fprintf(stderr, "[anslicensing] WMI failure (hr=0x%08lX): %s — hardware fingerprint will be degraded\n",
(unsigned long)hres, what);
}
bool ownCoInitialize; bool ownCoInitialize;
IWbemServices * wbemServices; IWbemServices * wbemServices;
IWbemLocator * wbemLocator; IWbemLocator * wbemLocator;
+113 -9
View File
@@ -221,8 +221,45 @@ namespace ANSCENTER {
initialize_handler(); initialize_handler();
} }
// ────────────────────────────────────────────────────────────────────
// A model that is already in memory — the decrypted contents of an
// .ansmodel package. There is no path, so onnx_path stays null and
// every file-shaped step in initialize_handler is skipped: no CWD
// switch, no external-data lookup, no file for ORT to open.
// ────────────────────────────────────────────────────────────────────
BasicOrtHandler::BasicOrtHandler(const void* modelData, size_t modelSize,
const std::string& logName,
unsigned int _num_threads)
: num_threads(_num_threads),
m_engineType(static_cast<EngineType>(-1))
{
log_id_storage = logName;
log_id = log_id_storage.c_str();
model_data = modelData;
model_size = modelSize;
initialize_handler();
}
BasicOrtHandler::BasicOrtHandler(const void* modelData, size_t modelSize,
const std::string& logName,
EngineType engineType,
unsigned int _num_threads)
: num_threads(_num_threads),
m_engineType(engineType)
{
log_id_storage = logName;
log_id = log_id_storage.c_str();
model_data = modelData;
model_size = modelSize;
initialize_handler();
}
BasicOrtHandler::~BasicOrtHandler() BasicOrtHandler::~BasicOrtHandler()
{ {
// The model bytes, when there are any, belong to whoever handed
// them over; that owner wipes them. Only the view goes here.
model_data = nullptr;
model_size = 0;
if (ort_session) { if (ort_session) {
delete ort_session; delete ort_session;
ort_session = nullptr; ort_session = nullptr;
@@ -727,20 +764,26 @@ namespace ANSCENTER {
// AddExternalInitializersFromFilesInMemory() so ORT never // AddExternalInitializersFromFilesInMemory() so ORT never
// memory-maps the file itself. // memory-maps the file itself.
// ---------------------------------------------------------------- // ----------------------------------------------------------------
std::filesystem::path modelFsPath(onnx_path); // wchar_t* // A model in memory has no directory, no sibling .onnx_data and
std::filesystem::path modelDir = modelFsPath.parent_path(); // nothing for ORT to open, so all of this is skipped for one.
std::filesystem::path modelFsPath;
std::filesystem::path modelDir;
std::filesystem::path prevCwd = std::filesystem::current_path(); std::filesystem::path prevCwd = std::filesystem::current_path();
if (!modelDir.empty() && std::filesystem::is_directory(modelDir)) { if (!fromMemory()) {
std::filesystem::current_path(modelDir); modelFsPath = std::filesystem::path(onnx_path); // wchar_t*
std::cout << "[ORT] CWD -> " << modelDir.string() << std::endl; modelDir = modelFsPath.parent_path();
if (!modelDir.empty() && std::filesystem::is_directory(modelDir)) {
std::filesystem::current_path(modelDir);
std::cout << "[ORT] CWD -> " << modelDir.string() << std::endl;
}
} }
// --- Pre-load external data files if they exist ----------------- // --- Pre-load external data files if they exist -----------------
// Keep the buffer alive across session creation (must outlive the // Keep the buffer alive across session creation (must outlive the
// Ort::Session constructor call). // Ort::Session constructor call).
std::vector<char> extDataBuffer; std::vector<char> extDataBuffer;
{ if (!fromMemory()) {
// Build the expected external-data filename: // Build the expected external-data filename:
// <model_stem>.onnx_data (e.g. anssam3.onnx_data) // <model_stem>.onnx_data (e.g. anssam3.onnx_data)
std::filesystem::path extDataPath = std::filesystem::path extDataPath =
@@ -806,7 +849,7 @@ namespace ANSCENTER {
// This avoids ORT opening/mapping ANY files during CreateSession. // This avoids ORT opening/mapping ANY files during CreateSession.
std::vector<char> modelBuffer; std::vector<char> modelBuffer;
bool useModelBuffer = false; bool useModelBuffer = false;
if (!extDataBuffer.empty()) { if (!extDataBuffer.empty() && !fromMemory()) {
// External data was pre-loaded, so also load the .onnx itself // External data was pre-loaded, so also load the .onnx itself
try { try {
auto modelFileSize = std::filesystem::file_size(modelFsPath); auto modelFileSize = std::filesystem::file_size(modelFsPath);
@@ -829,7 +872,10 @@ namespace ANSCENTER {
// --- Attempt session creation (with CUDA → CPU fallback) -------- // --- Attempt session creation (with CUDA → CPU fallback) --------
auto createSession = [&](Ort::SessionOptions& opts, const char* label) { auto createSession = [&](Ort::SessionOptions& opts, const char* label) {
std::cout << "[ORT] Creating session (" << label << ")..." << std::endl; std::cout << "[ORT] Creating session (" << label << ")..." << std::endl;
if (useModelBuffer) { if (fromMemory()) {
ort_session = new Ort::Session(*ort_env,
model_data, model_size, opts);
} else if (useModelBuffer) {
ort_session = new Ort::Session(*ort_env, ort_session = new Ort::Session(*ort_env,
modelBuffer.data(), modelBuffer.size(), opts); modelBuffer.data(), modelBuffer.size(), opts);
} else { } else {
@@ -838,7 +884,11 @@ namespace ANSCENTER {
std::cout << "[ORT] Session created OK (" << label << ")." << std::endl; std::cout << "[ORT] Session created OK (" << label << ")." << std::endl;
}; };
ANS_DBG("OrtHandler", "Creating session for model: %ls", onnx_path); if (fromMemory())
ANS_DBG("OrtHandler", "Creating session for model in memory: %s (%zu bytes)",
log_id ? log_id : "<memory>", model_size);
else
ANS_DBG("OrtHandler", "Creating session for model: %ls", onnx_path);
try { try {
createSession(session_options, "primary EP"); createSession(session_options, "primary EP");
ANS_DBG("OrtHandler", "Session created OK with primary EP"); ANS_DBG("OrtHandler", "Session created OK with primary EP");
@@ -1044,6 +1094,60 @@ namespace ANSCENTER {
} }
} }
// ====================================================================
// AdaFace
// ====================================================================
Ort::Value AdaFace::transform(const cv::Mat& mat)
{
if (mat.empty())
throw std::runtime_error("AdaFace::transform — input is empty.");
// AdaFace consumes BGR — OpenCV Mats are already BGR, no channel swap.
cv::Mat canvas;
cv::resize(mat, canvas, cv::Size(input_node_dims.at(3), input_node_dims.at(2)));
if (canvas.type() != CV_32FC3)
canvas.convertTo(canvas, CV_32FC3);
utils::transform::normalize_inplace(canvas, mean_val, scale_val);
std::vector<int64_t> shape = input_node_dims;
if (shape[0] == -1) shape[0] = 1;
return utils::transform::create_tensor(
canvas, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
Ort::Value AdaFace::transformBatch(const std::vector<cv::Mat>& images)
{
if (images.empty())
throw std::runtime_error("AdaFace::transformBatch — batch is empty.");
const int width = input_node_dims.at(3);
const int height = input_node_dims.at(2);
std::vector<cv::Mat> batch;
batch.reserve(images.size());
for (const auto& mat : images) {
if (mat.empty())
throw std::runtime_error("AdaFace::transformBatch — empty image in batch.");
cv::Mat canvas;
cv::resize(mat, canvas, cv::Size(width, height));
if (canvas.type() != CV_32FC3)
canvas.convertTo(canvas, CV_32FC3);
utils::transform::normalize_inplace(canvas, mean_val, scale_val);
batch.push_back(std::move(canvas));
}
std::vector<int64_t> shape = input_node_dims;
shape[0] = static_cast<int64_t>(images.size());
return utils::transform::create_tensor_batch(
batch, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
// ==================================================================== // ====================================================================
// GlintCosFace // GlintCosFace
// ==================================================================== // ====================================================================
+70
View File
@@ -468,6 +468,25 @@ namespace ANSCENTER {
const LITEORT_CHAR* onnx_path = nullptr; // ← points into onnx_path_w const LITEORT_CHAR* onnx_path = nullptr; // ← points into onnx_path_w
const char* log_id = nullptr; const char* log_id = nullptr;
// ────────────────────────────────────────────────────────────────
// A model handed over as bytes rather than as a path.
//
// An .ansmodel package is decrypted into memory and never written
// out, so there is no file for ORT to open and no directory to
// resolve external data against.
//
// This is a VIEW, not a copy: the owner of the plaintext keeps it
// alive for at least as long as this handler, because
// Reinitialize() rebuilds the session after a driver fault and has
// nothing else to rebuild it from. A copy here would double the
// resident size of every loaded model and protect nothing extra —
// the weights are in RAM either way once a session exists.
// ────────────────────────────────────────────────────────────────
const void* model_data = nullptr; // null for a file model
size_t model_size = 0;
std::string log_id_storage; // owns what log_id points at
bool fromMemory() const { return model_data != nullptr && model_size > 0; }
protected: protected:
const unsigned int num_threads; const unsigned int num_threads;
@@ -499,6 +518,23 @@ namespace ANSCENTER {
const OrtHandlerOptions& options, const OrtHandlerOptions& options,
unsigned int _num_threads = 1); unsigned int _num_threads = 1);
// A model already in memory — the decrypted contents of an
// .ansmodel package. `logName` is what logs call it, since there
// is no file name to fall back on.
//
// The caller KEEPS THE BYTES ALIVE until this handler is destroyed,
// and wipes them then; see model_data above for why a copy is not
// taken here.
explicit BasicOrtHandler(const void* modelData, size_t modelSize,
const std::string& logName,
unsigned int _num_threads = 1);
// The same, with an explicit engine override.
explicit BasicOrtHandler(const void* modelData, size_t modelSize,
const std::string& logName,
EngineType engineType,
unsigned int _num_threads = 1);
virtual ~BasicOrtHandler(); virtual ~BasicOrtHandler();
BasicOrtHandler(const BasicOrtHandler&) = delete; BasicOrtHandler(const BasicOrtHandler&) = delete;
@@ -674,6 +710,40 @@ namespace ANSCENTER {
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override; Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
}; };
// ====================================================================
// AdaFace — face recognition
//
// Same session/inference path as GlintArcFace (detect/detectBatch are
// inherited and dispatch through the virtual transform), but AdaFace
// was trained on BGR input — the official repo flips RGB→BGR before
// normalize — so the BGR2RGB conversion is omitted: OpenCV Mats are
// already BGR. Normalization is identical: (x - 127.5) / 127.5.
// ====================================================================
class AdaFace : public GlintArcFace
{
public:
explicit AdaFace(const std::string& _onnx_path,
unsigned int _num_threads = 1)
: GlintArcFace(_onnx_path, _num_threads)
{
}
explicit AdaFace(const std::string& _onnx_path,
EngineType engineType,
unsigned int _num_threads = 1)
: GlintArcFace(_onnx_path, engineType, _num_threads) {
}
~AdaFace() override = default;
private:
static constexpr float mean_val = 127.5f;
static constexpr float scale_val = 1.f / 127.5f;
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
};
// ==================================================================== // ====================================================================
// MOVINET — action recognition // MOVINET — action recognition
// ==================================================================== // ====================================================================
@@ -265,6 +265,8 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
// It is full path // It is full path
std::string engineName = serializeEngineOptions(m_options, onnxModelPath); std::string engineName = serializeEngineOptions(m_options, onnxModelPath);
std::string engineDir = m_options.engineFileDir; std::string engineDir = m_options.engineFileDir;
m_engineFilePath = engineName;
m_rebuiltFromOnnx = false;
if (FileExist(engineName)) { if (FileExist(engineName)) {
if (m_verbose) { std::cout << "Engine file found: " << engineName << std::endl; } if (m_verbose) { std::cout << "Engine file found: " << engineName << std::endl; }
@@ -314,7 +316,7 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
return false; return false;
} }
} }
if (!FileExist(onnxModelPath)) { if (!hasOnnxBytes() && !FileExist(onnxModelPath)) {
// ONNX model does not exist, try to find alternative precision engine // ONNX model does not exist, try to find alternative precision engine
std::cout << "Searching for alternative precision engine..." << std::endl; std::cout << "Searching for alternative precision engine..." << std::endl;
@@ -335,6 +337,7 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
if (FileExist(alternativeEngineName)) { if (FileExist(alternativeEngineName)) {
std::cout << "Found alternative precision engine: " << alternativeEngineName << std::endl; std::cout << "Found alternative precision engine: " << alternativeEngineName << std::endl;
m_engineFilePath = alternativeEngineName;
return loadNetwork(alternativeEngineName, subVals, divVals, normalize); return loadNetwork(alternativeEngineName, subVals, divVals, normalize);
} }
else { else {
@@ -358,11 +361,13 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
std::cout << "FP16 engine not found, but FP32 engine exists: " << fp32EngineName << std::endl; std::cout << "FP16 engine not found, but FP32 engine exists: " << fp32EngineName << std::endl;
std::cout << "Loading existing FP32 engine..." << std::endl; std::cout << "Loading existing FP32 engine..." << std::endl;
m_options.precision = ANSCENTER::Precision::FP32; m_options.precision = ANSCENTER::Precision::FP32;
m_engineFilePath = fp32EngineName;
return loadNetwork(fp32EngineName, subVals, divVals, normalize); return loadNetwork(fp32EngineName, subVals, divVals, normalize);
} }
} }
// ONNX model exists, generate engine // ONNX model exists, generate engine
m_rebuiltFromOnnx = true;
std::cout << "========================================" << std::endl; std::cout << "========================================" << std::endl;
std::cout << "Engine not found, generating from ONNX model" << std::endl; std::cout << "Engine not found, generating from ONNX model" << std::endl;
std::cout << "========================================" << std::endl; std::cout << "========================================" << std::endl;
@@ -385,12 +390,9 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
auto tempNetwork = std::unique_ptr<nvinfer1::INetworkDefinition>(TRT_CREATE_NETWORK(tempBuilder)); auto tempNetwork = std::unique_ptr<nvinfer1::INetworkDefinition>(TRT_CREATE_NETWORK(tempBuilder));
auto tempParser = std::unique_ptr<nvonnxparser::IParser>(nvonnxparser::createParser(*tempNetwork, m_logger)); auto tempParser = std::unique_ptr<nvonnxparser::IParser>(nvonnxparser::createParser(*tempNetwork, m_logger));
std::ifstream onnxFile(onnxModelPath, std::ios::binary | std::ios::ate); std::vector<char> onnxBuffer;
std::streamsize onnxSize = onnxFile.tellg(); if (!readOnnxBytes(onnxModelPath, onnxBuffer)) {
onnxFile.seekg(0, std::ios::beg); std::cout << "Error: Failed to read ONNX model" << std::endl;
std::vector<char> onnxBuffer(onnxSize);
if (!onnxFile.read(onnxBuffer.data(), onnxSize)) {
std::cout << "Error: Failed to read ONNX file" << std::endl;
return false; return false;
} }
@@ -818,9 +820,10 @@ bool Engine<T>::loadNetwork(std::string trtModelPath, const std::array<float, 3>
std::cerr << "[GpuMemGuard] REFUSING engine load on GPU " std::cerr << "[GpuMemGuard] REFUSING engine load on GPU "
<< m_options.deviceIndex << ": need ~" << m_options.deviceIndex << ": need ~"
<< (buffer.size() / (1024 * 1024)) << " MiB + " << (buffer.size() / (1024 * 1024)) << " MiB + "
<< (ANSCENTER::GpuMemoryGuard::Margin() / (1024 * 1024)) << (ANSCENTER::GpuMemoryGuard::MarginFor(totalVRAM) / (1024 * 1024))
<< " MiB margin, only " << (freeVRAM / (1024 * 1024)) << " MiB margin, only " << (freeVRAM / (1024 * 1024))
<< " MiB free. Reduce concurrent engines/cameras or batch size." << " MiB free of " << (totalVRAM / (1024 * 1024))
<< " MiB total. Reduce concurrent engines/cameras or batch size."
<< std::endl; << std::endl;
return false; return false;
} }
@@ -1459,7 +1462,7 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
std::cout << "Engine file already exists: " << engineName << std::endl; std::cout << "Engine file already exists: " << engineName << std::endl;
return true; return true;
} }
if (!FileExist(onnxModelPath)) { if (!hasOnnxBytes() && !FileExist(onnxModelPath)) {
std::cout << "Error: ONNX model file does not exist: " << onnxModelPath << std::endl; std::cout << "Error: ONNX model file does not exist: " << onnxModelPath << std::endl;
return false; return false;
} }
@@ -1496,16 +1499,14 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
return false; return false;
} }
// Read the onnx file into memory // Get the onnx bytes: from the in-memory buffer of a sealed package, or
std::ifstream file(onnxModelPath, std::ios::binary | std::ios::ate); // from the file on disk.
std::streamsize size = file.tellg(); std::vector<char> buffer;
file.seekg(0, std::ios::beg); if (!readOnnxBytes(onnxModelPath, buffer)) {
std::cout << "Error: Unable to read ONNX model" << std::endl;
std::vector<char> buffer(size);
if (!file.read(buffer.data(), size)) {
std::cout << "Error: Unable to read ONNX file" << std::endl;
return false; return false;
} }
const std::streamsize size = static_cast<std::streamsize>(buffer.size());
std::cout << "ONNX model size: " << size / (1024 * 1024) << " MiB" << std::endl; std::cout << "ONNX model size: " << size / (1024 * 1024) << " MiB" << std::endl;
@@ -2465,16 +2466,14 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
auto tempParser = std::unique_ptr<nvonnxparser::IParser>( auto tempParser = std::unique_ptr<nvonnxparser::IParser>(
nvonnxparser::createParser(*tempNetwork, m_logger)); nvonnxparser::createParser(*tempNetwork, m_logger));
std::ifstream onnxFile(onnxModelPath, std::ios::binary | std::ios::ate); std::vector<char> onnxBuffer;
if (!onnxFile.good()) { const bool onnxReadable = readOnnxBytes(onnxModelPath, onnxBuffer);
if (!onnxReadable) {
ANS_DBG("TRT_Build", "buildWithRetry WARN cannot open ONNX for pre-parse path=%s", ANS_DBG("TRT_Build", "buildWithRetry WARN cannot open ONNX for pre-parse path=%s",
onnxModelPath.c_str()); onnxModelPath.c_str());
} }
if (onnxFile.good()) { if (onnxReadable) {
std::streamsize onnxSize = onnxFile.tellg(); {
onnxFile.seekg(0, std::ios::beg);
std::vector<char> onnxBuffer(onnxSize);
if (onnxFile.read(onnxBuffer.data(), onnxSize)) {
unsigned long sehRetryParse = 0; unsigned long sehRetryParse = 0;
bool retryParsed = parseOnnxModelSafe(tempParser.get(), bool retryParsed = parseOnnxModelSafe(tempParser.get(),
onnxBuffer.data(), onnxBuffer.size(), &sehRetryParse); onnxBuffer.data(), onnxBuffer.size(), &sehRetryParse);
@@ -2502,9 +2501,6 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
(int)retryParsed, (int)retryParsed,
retryParsed ? tempNetwork->getNbInputs() : -1); retryParsed ? tempNetwork->getNbInputs() : -1);
} }
} else {
ANS_DBG("TRT_Build", "buildWithRetry WARN ONNX read failed bytes=%zd",
(long long)onnxSize);
} }
} }
} else { } else {
@@ -88,8 +88,20 @@ public:
const std::array<float, 3>& subVals, const std::array<float, 3>& subVals,
const std::array<float, 3>& divVals, const std::array<float, 3>& divVals,
bool normalize, bool normalize,
int maxSlotsPerGpu) int maxSlotsPerGpu,
const void* onnxData = nullptr,
size_t onnxSize = 0)
{ {
// A sealed .ansmodel is decrypted in memory and never written out, so
// the caller hands the bytes down here: every Engine this call builds
// parses them instead of opening modelPath, which only names the
// TensorRT plan that gets cached.
auto newEngine = [&]() {
auto e = std::make_shared<Engine<T>>(options);
if (onnxData != nullptr && onnxSize > 0) e->setOnnxBytes(onnxData, onnxSize);
return e;
};
// Optimizer / temporary engines: maxSlotsPerGpu==0 means the caller // Optimizer / temporary engines: maxSlotsPerGpu==0 means the caller
// only needs a lightweight, non-shared engine (e.g., OptimizeModelStr). // only needs a lightweight, non-shared engine (e.g., OptimizeModelStr).
// Bypass the pool cache entirely: // Bypass the pool cache entirely:
@@ -99,7 +111,7 @@ public:
// Note: maxSlotsPerGpu==1 is now the normal "1 slot per GPU" multi-GPU // Note: maxSlotsPerGpu==1 is now the normal "1 slot per GPU" multi-GPU
// round-robin mode, so it goes through the pool path below. // round-robin mode, so it goes through the pool path below.
if (maxSlotsPerGpu == 0) { if (maxSlotsPerGpu == 0) {
auto engine = std::make_shared<Engine<T>>(options); auto engine = newEngine();
bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize); bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
return ok ? engine : nullptr; return ok ? engine : nullptr;
} }
@@ -141,13 +153,13 @@ public:
} }
// Cache miss — create new Engine pool // Cache miss — create new Engine pool
auto engine = std::make_shared<Engine<T>>(options); auto engine = newEngine();
bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu); bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu);
if (!ok) { if (!ok) {
// Step 1: Force-evict all pools with refcount=0 to reclaim VRAM // Step 1: Force-evict all pools with refcount=0 to reclaim VRAM
int evicted = forceEvictPending(); int evicted = forceEvictPending();
if (evicted > 0) { if (evicted > 0) {
engine = std::make_shared<Engine<T>>(options); engine = newEngine();
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu); ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu);
} }
@@ -156,7 +168,7 @@ public:
// consumes ~300-500 MB vs ~50-100 MB for a simple loadNetwork. // consumes ~300-500 MB vs ~50-100 MB for a simple loadNetwork.
// Lightweight mode: tasks queue for a single shared slot — slower but works. // Lightweight mode: tasks queue for a single shared slot — slower but works.
if (!ok) { if (!ok) {
engine = std::make_shared<Engine<T>>(options); engine = newEngine();
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize); ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
} }
@@ -183,7 +195,7 @@ public:
} }
// Final retry — try lightweight again after delay // Final retry — try lightweight again after delay
engine = std::make_shared<Engine<T>>(options); engine = newEngine();
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize); ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
} }
@@ -4,6 +4,48 @@
#include <NvInferVersion.h> // also defines TRT version macros #include <NvInferVersion.h> // also defines TRT version macros
#include <cudnn_version.h> // CUDNN_MAJOR/MINOR/PATCHLEVEL #include <cudnn_version.h> // CUDNN_MAJOR/MINOR/PATCHLEVEL
#include <cuda_runtime.h> // cudaRuntimeGetVersion #include <cuda_runtime.h> // cudaRuntimeGetVersion
#include <cstring> // memset for wiping decrypted model bytes
#include <fstream>
// ============================================================================
// ONNX bytes in memory
//
// A sealed .ansmodel is decrypted into RAM and stays there: handing the
// parser a buffer is what keeps the plaintext model off the disk. The build
// path still takes a path, because the path names the TensorRT plan that is
// cached beside it — a plan is a built artefact and carries no model.
// ============================================================================
template <typename T>
void Engine<T>::setOnnxBytes(const void* data, size_t size) {
clearOnnxBytes();
if (data == nullptr || size == 0) return;
const char* p = static_cast<const char*>(data);
m_onnxBytes.assign(p, p + size);
}
template <typename T>
void Engine<T>::clearOnnxBytes() {
if (m_onnxBytes.empty()) return;
std::memset(m_onnxBytes.data(), 0, m_onnxBytes.size());
m_onnxBytes.clear();
m_onnxBytes.shrink_to_fit();
}
template <typename T>
bool Engine<T>::readOnnxBytes(const std::string& onnxModelPath,
std::vector<char>& buffer) const {
if (!m_onnxBytes.empty()) {
buffer = m_onnxBytes;
return true;
}
std::ifstream file(onnxModelPath, std::ios::binary | std::ios::ate);
if (!file.good()) return false;
const std::streamsize size = file.tellg();
if (size <= 0) return false;
file.seekg(0, std::ios::beg);
buffer.resize(static_cast<size_t>(size));
return static_cast<bool>(file.read(buffer.data(), size));
}
template <typename T> template <typename T>
void Engine<T>::transformOutput(std::vector<std::vector<std::vector<T>>> &input, std::vector<std::vector<T>> &output) { void Engine<T>::transformOutput(std::vector<std::vector<std::vector<T>>> &input, std::vector<std::vector<T>> &output) {
+37 -10
View File
@@ -17,8 +17,15 @@
// cooldown so callers back off instead of hammering // cooldown so callers back off instead of hammering
// the fragile context (which is what corrupts it). // the fragile context (which is what corrupts it).
// //
// Margin default = 768 MiB; override at runtime with env ANSCENTER_GPU_MARGIN_MB. // The margin scales with the device: total/8, clamped to [192 MiB, 768 MiB].
// All queries operate on the CURRENT CUDA device (callers cudaSetDevice first). // A fixed margin cannot work across the fleet — 768 MiB is 3% of a 24 GiB
// compute card but 19% of a 4 GiB display card (RTX A400), where the desktop
// plus the ORT CUDA arena never leave that much free and admission control can
// therefore never pass. The 768 MiB ceiling keeps every card >= 6 GiB on
// exactly the previous margin, so only small cards change behaviour.
// Override at runtime with env ANSCENTER_GPU_MARGIN_MB (or SetMargin); an
// explicit override wins over scaling. All queries operate on the CURRENT CUDA
// device (callers cudaSetDevice first).
// ============================================================================ // ============================================================================
#include <cuda_runtime.h> #include <cuda_runtime.h>
#include <atomic> #include <atomic>
@@ -32,8 +39,24 @@ namespace ANSCENTER {
class GpuMemoryGuard { class GpuMemoryGuard {
public: public:
// ---- configuration ---------------------------------------------------- // ---- configuration ----------------------------------------------------
static size_t Margin() { return s_marginBytes.load(std::memory_order_relaxed); } // Effective margin for a device with 'totalB' VRAM. An explicit override
static void SetMargin(size_t b) { s_marginBytes.store(b, std::memory_order_relaxed); } // (env or SetMargin) wins; otherwise scale to the card.
static size_t MarginFor(size_t totalB) {
if (size_t ov = s_marginOverride.load(std::memory_order_relaxed)) return ov;
size_t m = totalB / kMarginDivisor;
if (m < kMarginMin) m = kMarginMin;
if (m > kMarginMax) m = kMarginMax;
return m;
}
// Effective margin for the CURRENT device. Prefer MarginFor() where the
// caller already has a total from Query() — this re-queries.
static size_t Margin() {
size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return kMarginMax;
return MarginFor(totalB);
}
// b == 0 restores device-scaled margins.
static void SetMargin(size_t b) { s_marginOverride.store(b, std::memory_order_relaxed); }
// ---- (1) admission control at load ------------------------------------ // ---- (1) admission control at load ------------------------------------
// True if 'needBytes' can be committed on the current device while keeping // True if 'needBytes' can be committed on the current device while keeping
@@ -42,7 +65,7 @@ public:
static bool CanLoad(size_t needBytes) { static bool CanLoad(size_t needBytes) {
size_t freeB = 0, totalB = 0; size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return true; if (!Query(freeB, totalB)) return true;
return freeB >= needBytes + Margin(); return freeB >= needBytes + MarginFor(totalB);
} }
// ---- (2) pressure guard at inference ---------------------------------- // ---- (2) pressure guard at inference ----------------------------------
@@ -52,7 +75,7 @@ public:
if (BreakerOpen()) return true; if (BreakerOpen()) return true;
size_t freeB = 0, totalB = 0; size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return false; if (!Query(freeB, totalB)) return false;
return freeB < Margin(); return freeB < MarginFor(totalB);
} }
// ---- (3) OOM-safe alloc + circuit breaker ----------------------------- // ---- (3) OOM-safe alloc + circuit breaker -----------------------------
@@ -80,16 +103,20 @@ private:
return std::chrono::duration_cast<std::chrono::milliseconds>( return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count(); std::chrono::steady_clock::now().time_since_epoch()).count();
} }
static size_t InitMargin() { // 0 = no override, use device-scaled margin.
static size_t InitMarginOverride() {
if (const char* e = std::getenv("ANSCENTER_GPU_MARGIN_MB")) { if (const char* e = std::getenv("ANSCENTER_GPU_MARGIN_MB")) {
long mb = std::atol(e); long mb = std::atol(e);
if (mb > 0) return static_cast<size_t>(mb) * 1024ULL * 1024ULL; if (mb > 0) return static_cast<size_t>(mb) * 1024ULL * 1024ULL;
} }
return 768ULL * 1024 * 1024; // 768 MiB return 0;
} }
static constexpr int64_t kCooldownMs = 1500; // breaker cooldown after OOM static constexpr int64_t kCooldownMs = 1500; // breaker cooldown after OOM
inline static std::atomic<size_t> s_marginBytes{ InitMargin() }; static constexpr size_t kMarginDivisor = 8; // margin = total/8 ...
static constexpr size_t kMarginMin = 192ULL * 1024 * 1024; // ... clamped
static constexpr size_t kMarginMax = 768ULL * 1024 * 1024; // (prev. fixed value)
inline static std::atomic<size_t> s_marginOverride{ InitMarginOverride() };
inline static std::atomic<int64_t> s_breakerUntilMs{ 0 }; inline static std::atomic<int64_t> s_breakerUntilMs{ 0 };
}; };
+371 -18
View File
@@ -4,6 +4,7 @@
#include "ANSOVFaceDetector.h" #include "ANSOVFaceDetector.h"
#include "SCRFDFaceDetector.h" #include "SCRFDFaceDetector.h"
#include "ANSFaceRecognizer.h" #include "ANSFaceRecognizer.h"
#include "ONNXFaceAttributes.h"
#include <faiss/IndexIDMap.h> #include <faiss/IndexIDMap.h>
#include <faiss/IndexFlat.h> #include <faiss/IndexFlat.h>
#include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid.hpp>
@@ -374,6 +375,27 @@ namespace ANSCENTER {
bool ANSFacialRecognition::LoadEngine() { bool ANSFacialRecognition::LoadEngine() {
try { try {
ANS_DBG("ANSFR", "LoadEngine: starting"); ANS_DBG("ANSFR", "LoadEngine: starting");
// Restore the last explicitly chosen recognizer model so a
// runtime SetRecognizerModelType survives restarts. A choice
// made this session (before LoadEngine) takes precedence.
if (!_recModelExplicit && _db) {
std::string pref;
{
std::lock_guard<std::mutex> lock(_databaseMutex);
pref = _db->GetSetting("RECOGNIZER_MODEL_PREF");
}
if (pref == "arcface")
_requestedRecModel = FaceRecognizerModel::ARCFACE;
else if (pref == "adaface")
_requestedRecModel = FaceRecognizerModel::ADAFACE;
if (!pref.empty()) {
LogThreadSafe("ANSFacialRecognition::LoadEngine",
"Restored persisted recognizer model preference: " + pref,
LogLevel::Info);
}
}
// Unload existing engine (handles its own locking) // Unload existing engine (handles its own locking)
UnloadEngine(); UnloadEngine();
@@ -463,6 +485,16 @@ namespace ANSCENTER {
attributeModelsSuccess = false; attributeModelsSuccess = false;
} }
// Embedding/model consistency: stored embeddings must come from
// the recognizer model that just loaded. A v1.4 upgrade flips the
// default from ArcFace to AdaFace, which silently invalidates a
// pre-existing database — regenerate before the index builds.
if (!SyncEmbeddingsWithActiveModel()) {
LogThreadSafe("ANSFacialRecognition::LoadEngine",
"Warning: embedding/model sync failed — recognition may return "
"Unknown until faces are re-enrolled");
}
// Reload database/embeddings // Reload database/embeddings
if (!Reload()) { if (!Reload()) {
LogThreadSafe("ANSFacialRecognition::LoadEngine", LogThreadSafe("ANSFacialRecognition::LoadEngine",
@@ -634,6 +666,19 @@ namespace ANSCENTER {
"Built new index with " + std::to_string(newFaissIndex->ntotal) + "Built new index with " + std::to_string(newFaissIndex->ntotal) +
" embeddings, " + std::to_string(newUserDict.size()) + " users", LogLevel::Info); " embeddings, " + std::to_string(newUserDict.size()) + " users", LogLevel::Info);
// Users without a single embedding cannot ever match — every face
// comes back Unknown/2.0 and nothing else in the log says why. The
// usual cause: faces were enrolled while the recognizer engine
// could not load (InsertFace stores the user but no embedding).
if (newFaissIndex->ntotal == 0 && !newUserDict.empty()) {
LogThreadSafe("ANSFacialRecognition::Reload",
"Face database has " + std::to_string(newUserDict.size()) +
" users but ZERO embeddings — all matches will return Unknown. "
"Faces were likely enrolled while the recognizer engine was "
"failing to load (check earlier engine errors). Re-enroll the "
"users' faces once the engine loads.", LogLevel::Warn);
}
// ============================================================ // ============================================================
// PHASE 2: Atomic swap (brief exclusive lock ~microseconds) // PHASE 2: Atomic swap (brief exclusive lock ~microseconds)
// Inference is blocked only during this swap. // Inference is blocked only during this swap.
@@ -805,9 +850,12 @@ namespace ANSCENTER {
} }
bool ANSFacialRecognition::InitializeRecognizer() { bool ANSFacialRecognition::InitializeRecognizer() {
try { try {
ANS_DBG("ANSFR", "InitializeRecognizer: starting, modelPath=%s", _recognizerFilePath.c_str()); ANS_DBG("ANSFR", "InitializeRecognizer: starting, modelPath=%s model=%s",
_recognizerFilePath.c_str(), FaceRecognizerModelName(_requestedRecModel));
// Create recognizer instance // Create recognizer instance
_recognizer = std::make_unique<ANSFaceRecognizer>(); auto recognizer = std::make_unique<ANSFaceRecognizer>();
recognizer->SetPreferredModel(_requestedRecModel);
_recognizer = std::move(recognizer);
_recognizer->SetMaxSlotsPerGpu(m_maxSlotsPerGpu); _recognizer->SetMaxSlotsPerGpu(m_maxSlotsPerGpu);
// Configure model // Configure model
@@ -848,7 +896,13 @@ namespace ANSCENTER {
return false; return false;
} }
ANS_DBG("ANSFR", "InitializeRecognizer: SUCCESS"); // Record what actually loaded — ADAFACE silently falls back to
// ARCFACE when the model pack has no adafacerecognizer.onnx.
if (auto* concrete = dynamic_cast<ANSFaceRecognizer*>(_recognizer.get())) {
_activeRecModel = concrete->GetActiveModel();
}
ANS_DBG("ANSFR", "InitializeRecognizer: SUCCESS, active model=%s",
FaceRecognizerModelName(_activeRecModel));
return true; return true;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
@@ -911,8 +965,50 @@ namespace ANSCENTER {
return false; return false;
} }
} }
namespace {
// EP options for the ONNX attribute models. On NVIDIA the models are
// built into TensorRT engines through the TRT EP and cached on disk
// next to the model pack (first load pays the build, every later load
// is instant). AMD/Intel/CPU resolve to DirectML/OpenVINO/CPU inside
// BasicOrtHandler and ignore every TRT field.
OrtHandlerOptions BuildAttributeOrtOptions(EngineType engineType,
const std::string& modelFolder) {
OrtHandlerOptions opts;
if (engineType == EngineType::NVIDIA_GPU) {
opts.preferTensorRT = true;
opts.trtFP16 = true;
opts.trtEngineCacheDir = CreateFilePath(modelFolder, "TRTCache");
std::error_code ec;
std::filesystem::create_directories(opts.trtEngineCacheDir, ec);
}
return opts;
}
}
bool ANSFacialRecognition::InitializeAgeGenderModel(const std::string& deviceName) { bool ANSFacialRecognition::InitializeAgeGenderModel(const std::string& deviceName) {
try { try {
// Prefer the ONNX model shipped since v1.4; fall back to the
// legacy OpenVINO IR when it is absent (or fails to load).
std::string onnxModelPath = CreateFilePath(_recognizerModelFolder, "agegender.onnx");
if (FileExist(onnxModelPath)) {
try {
auto detector = std::make_unique<OnnxAgeGenderEstimator>(
onnxModelPath, engineType,
BuildAttributeOrtOptions(engineType, _recognizerModelFolder));
std::lock_guard<std::mutex> lock(_ageGenderMutex);
_ageGenderDetector = std::move(detector);
LogThreadSafe("ANSFacialRecognition::InitializeAgeGenderModel",
"Loaded ONNX age-gender model: " + onnxModelPath, LogLevel::Info);
return true;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::InitializeAgeGenderModel",
"ONNX age-gender model failed (" + std::string(e.what()) +
") — falling back to OpenVINO model");
}
}
std::string ageGenderModelPath = CreateFilePath(_recognizerModelFolder, "agegender.xml"); std::string ageGenderModelPath = CreateFilePath(_recognizerModelFolder, "agegender.xml");
if (!FileExist(ageGenderModelPath)) { if (!FileExist(ageGenderModelPath)) {
@@ -936,18 +1032,20 @@ namespace ANSCENTER {
_ageGenderDetector.reset(); _ageGenderDetector.reset();
// Create new detector // Create new detector
_ageGenderDetector = std::make_unique<AgeGenderDetection>(ageGenderModelPath, true); auto detector = std::make_unique<AgeGenderDetection>(ageGenderModelPath, true);
// Compile model for target device // Compile model for target device
ov::CompiledModel compiledModel = core->compile_model( ov::CompiledModel compiledModel = core->compile_model(
_ageGenderDetector->read(*core), detector->read(*core),
deviceName deviceName
); );
// Create inference request // Create inference request
_ageGenderDetector->request = compiledModel.create_infer_request(); detector->request = compiledModel.create_infer_request();
_ageGenderDetector->inTensor = _ageGenderDetector->request.get_input_tensor(); detector->inTensor = detector->request.get_input_tensor();
_ageGenderDetector->inTensor.set_shape(_ageGenderDetector->inShape); detector->inTensor.set_shape(detector->inShape);
_ageGenderDetector = std::move(detector);
} }
return true; return true;
@@ -966,6 +1064,30 @@ namespace ANSCENTER {
} }
bool ANSFacialRecognition::InitializeEmotionModel(const std::string& deviceName) { bool ANSFacialRecognition::InitializeEmotionModel(const std::string& deviceName) {
try { try {
// Prefer the YOLOv11 classifier shipped since v1.4 (class list in
// emotionsrecognition.names); fall back to the OpenVINO IR.
std::string onnxModelPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.onnx");
if (FileExist(onnxModelPath)) {
try {
std::string namesPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.names");
auto classNames = OnnxEmotionsClassifier::LoadClassNames(namesPath);
auto detector = std::make_unique<OnnxEmotionsClassifier>(
onnxModelPath, std::move(classNames), engineType,
BuildAttributeOrtOptions(engineType, _recognizerModelFolder));
std::lock_guard<std::mutex> lock(_emotionsMutex);
_emotionsDetector = std::move(detector);
LogThreadSafe("ANSFacialRecognition::InitializeEmotionModel",
"Loaded ONNX emotion model: " + onnxModelPath, LogLevel::Info);
return true;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::InitializeEmotionModel",
"ONNX emotion model failed (" + std::string(e.what()) +
") — falling back to OpenVINO model");
}
}
std::string emotionModelPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.xml"); std::string emotionModelPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.xml");
if (!FileExist(emotionModelPath)) { if (!FileExist(emotionModelPath)) {
@@ -989,18 +1111,20 @@ namespace ANSCENTER {
_emotionsDetector.reset(); _emotionsDetector.reset();
// Create new detector // Create new detector
_emotionsDetector = std::make_unique<EmotionsDetection>(emotionModelPath, true); auto detector = std::make_unique<EmotionsDetection>(emotionModelPath, true);
// Compile model for target device // Compile model for target device
ov::CompiledModel compiledModel = core->compile_model( ov::CompiledModel compiledModel = core->compile_model(
_emotionsDetector->read(*core), detector->read(*core),
deviceName deviceName
); );
// Create inference request // Create inference request
_emotionsDetector->request = compiledModel.create_infer_request(); detector->request = compiledModel.create_infer_request();
_emotionsDetector->inTensor = _emotionsDetector->request.get_input_tensor(); detector->inTensor = detector->request.get_input_tensor();
_emotionsDetector->inTensor.set_shape(_emotionsDetector->inShape); detector->inTensor.set_shape(detector->inShape);
_emotionsDetector = std::move(detector);
} }
return true; return true;
@@ -1072,6 +1196,208 @@ namespace ANSCENTER {
} }
} }
bool ANSFacialRecognition::SyncEmbeddingsWithActiveModel() {
try {
if (!_db || !_recognizer) return false;
const std::string activeName = FaceRecognizerModelName(_activeRecModel);
std::string storedName;
std::vector<FaceRecord> faces;
{
std::lock_guard<std::mutex> lock(_databaseMutex);
storedName = _db->GetSetting("EMBEDDING_MODEL");
if (_db->GetAllFaces(faces) != 0) return false;
}
if (storedName.empty()) {
// Databases created before v1.4 never recorded the model —
// every embedding in them came from ArcFace.
storedName = faces.empty() ? activeName : "arcface";
}
if (storedName != activeName && !faces.empty()) {
LogThreadSafe("ANSFacialRecognition::SyncEmbeddingsWithActiveModel",
"Stored embeddings were generated by '" + storedName +
"' but the active recognizer is '" + activeName +
"' — regenerating " + std::to_string(faces.size()) +
" embeddings from the enrolled face images", LogLevel::Info);
if (!RegenerateEmbeddings()) return false;
}
{
std::lock_guard<std::mutex> lock(_databaseMutex);
_db->SetSetting("EMBEDDING_MODEL", activeName);
}
return true;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::SyncEmbeddingsWithActiveModel", e.what());
return false;
}
}
bool ANSFacialRecognition::RegenerateEmbeddings() {
try {
if (!_db || !_recognizer || !_recognizer->IsInitialized()) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Recognizer/database not ready");
return false;
}
std::vector<FaceRecord> faces;
{
std::lock_guard<std::mutex> lock(_databaseMutex);
if (_db->GetAllFaces(faces) != 0) return false;
}
if (faces.empty()) return true;
size_t updated = 0, failed = 0;
for (const auto& face : faces) {
// InsertFace stored the aligned face crop — compute the new
// embedding directly on it, no re-detection needed.
cv::Mat crop = cv::imread(face.ImagePath, cv::IMREAD_COLOR);
if (crop.empty()) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Cannot read enrolled face image (face ID " +
std::to_string(face.FaceId) + "): " + face.ImagePath);
++failed;
continue;
}
Object faceObject;
faceObject.mask = crop;
std::vector<float> embedding;
{
std::lock_guard<std::mutex> lock(_recognitionMutex);
embedding = _recognizer->Feature(crop, faceObject);
}
if (embedding.size() != static_cast<size_t>(_config._recOutputDim)) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Embedding failed for face ID " + std::to_string(face.FaceId));
++failed;
continue;
}
std::lock_guard<std::mutex> lock(_databaseMutex);
if (_db->UpdateFaceEmbedding(face.FaceId, embedding.data()) > 0) ++updated;
else ++failed;
}
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Regenerated " + std::to_string(updated) + "/" +
std::to_string(faces.size()) + " embeddings (" +
std::to_string(failed) + " failed)", LogLevel::Info);
_reloadNeeded.store(true);
return updated > 0 || failed == 0;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings", e.what());
return false;
}
}
bool ANSFacialRecognition::SetRecognizerModelType(int modelType) {
try {
if (modelType < 0 || modelType > 1) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Invalid model type: " + std::to_string(modelType) +
" (0 = AdaFace, 1 = ArcFace)");
return false;
}
const auto requested = static_cast<FaceRecognizerModel>(modelType);
_requestedRecModel = requested;
_recModelExplicit = true;
// Persist the explicit choice so it survives restarts (restored
// by LoadEngine when no choice is made in that session).
if (_db) {
std::lock_guard<std::mutex> lock(_databaseMutex);
_db->SetSetting("RECOGNIZER_MODEL_PREF", FaceRecognizerModelName(requested));
}
// Engine not loaded yet — the preference is applied by LoadEngine.
if (!_recognizer) return true;
if (requested == _activeRecModel) return true;
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Switching recognizer model to '" +
std::string(FaceRecognizerModelName(requested)) + "'", LogLevel::Info);
if (!InitializeRecognizer()) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Failed to load recognizer for model '" +
std::string(FaceRecognizerModelName(requested)) + "'");
return false;
}
_recognizerModelFolder = _recognizer->GetModelFolder();
if (!SyncEmbeddingsWithActiveModel()) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Warning: embedding regeneration failed after model switch");
}
_reloadNeeded.store(true);
return Reload();
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType", e.what());
return false;
}
}
bool ANSFacialRecognition::UpdateAttributeInterval(int frames) {
if (frames < 1 || frames > 60) {
LogThreadSafe("ANSFacialRecognition::UpdateAttributeInterval",
"Invalid interval " + std::to_string(frames) + " (expected 1..60)");
return false;
}
{
std::lock_guard<std::mutex> lock(_configMutex);
_attributeInterval = frames;
}
LogThreadSafe("ANSFacialRecognition::UpdateAttributeInterval",
"Attribute refresh interval set to " + std::to_string(frames) + " frames",
LogLevel::Info);
return true;
}
int ANSFacialRecognition::GetRecognizerModelType() const {
return static_cast<int>(_recognizer ? _activeRecModel : _requestedRecModel);
}
std::string ANSFacialRecognition::GetActiveModelInfo() {
try {
const char* agegenderBackend = "disabled";
const char* emotionBackend = "disabled";
{
std::lock_guard<std::mutex> lock(_ageGenderMutex);
if (_ageGenderDetector && _ageGenderDetector->enabled())
agegenderBackend = _ageGenderDetector->backendName();
}
{
std::lock_guard<std::mutex> lock(_emotionsMutex);
if (_emotionsDetector && _emotionsDetector->enabled())
emotionBackend = _emotionsDetector->backendName();
}
nlohmann::json info;
info["recognizer_model"] = FaceRecognizerModelName(
_recognizer ? _activeRecModel : _requestedRecModel);
info["recognizer_loaded"] = (_recognizer != nullptr);
info["agegender_backend"] = agegenderBackend;
info["emotion_backend"] = emotionBackend;
info["attribute_interval"] = _attributeInterval;
return info.dump();
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::GetActiveModelInfo", e.what());
return "{}";
}
}
bool ANSFacialRecognition::UpdateParameters( bool ANSFacialRecognition::UpdateParameters(
float knownPersonThreshold, float knownPersonThreshold,
bool enableAgeGender, bool enableAgeGender,
@@ -1353,6 +1679,17 @@ namespace ANSCENTER {
// Get largest face (CPU operation) // Get largest face (CPU operation)
Object faceObject = GetLargestObject(detectedFaces); Object faceObject = GetLargestObject(detectedFaces);
// The recognizer must be up before we can compute an embedding.
// Feature() returns empty when uninitialized, which the size check
// below would misreport as a bad face — name the real cause here.
if (!_recognizer || !_recognizer->IsInitialized()) {
LogThreadSafe("ANSFacialRecognition::InsertFace",
"Cannot enroll face for user ID " + std::to_string(userId) +
": recognizer is NOT initialized (engine failed to load?). "
"No embedding stored — fix the engine errors, then re-enroll.");
return 0;
}
// LOCK ONLY DURING RECOGNITION (shared recognizer) // LOCK ONLY DURING RECOGNITION (shared recognizer)
std::vector<float> embedding; std::vector<float> embedding;
{ {
@@ -1363,8 +1700,10 @@ namespace ANSCENTER {
// Validate embedding // Validate embedding
if (embedding.empty() || embedding.size() != _config._recOutputDim) { if (embedding.empty() || embedding.size() != _config._recOutputDim) {
LogThreadSafe("ANSFacialRecognition::InsertFace", LogThreadSafe("ANSFacialRecognition::InsertFace",
"Invalid embedding generated for user ID " + std::to_string(userId) + "Invalid embedding for user ID " + std::to_string(userId) +
" (size: " + std::to_string(embedding.size()) + ")"); " (size: " + std::to_string(embedding.size()) + ", expected " +
std::to_string(_config._recOutputDim) + ") — face crop unusable "
"or recognizer busy/reloading. Face NOT enrolled.");
return 0; return 0;
} }
@@ -1461,6 +1800,16 @@ namespace ANSCENTER {
// Get largest face // Get largest face
Object faceObject = GetLargestObject(detectedFaces); Object faceObject = GetLargestObject(detectedFaces);
// Same readiness gate as InsertFace — name the real cause instead
// of letting the size check blame the face crop.
if (!_recognizer || !_recognizer->IsInitialized()) {
LogThreadSafe("ANSFacialRecognition::InsertMultipleFaces",
"Cannot enroll face for user ID " + std::to_string(userId) +
": recognizer is NOT initialized (engine failed to load?). "
"No embedding stored — fix the engine errors, then re-enroll.");
return faceIds;
}
// LOCK ONLY DURING RECOGNITION (shared recognizer) // LOCK ONLY DURING RECOGNITION (shared recognizer)
std::vector<float> embedding; std::vector<float> embedding;
{ {
@@ -1470,7 +1819,10 @@ namespace ANSCENTER {
if (embedding.empty() || embedding.size() != _config._recOutputDim) { if (embedding.empty() || embedding.size() != _config._recOutputDim) {
LogThreadSafe("ANSFacialRecognition::InsertMultipleFaces", LogThreadSafe("ANSFacialRecognition::InsertMultipleFaces",
"Invalid embedding generated for user ID " + std::to_string(userId)); "Invalid embedding for user ID " + std::to_string(userId) +
" (size: " + std::to_string(embedding.size()) + ", expected " +
std::to_string(_config._recOutputDim) + ") — face crop unusable "
"or recognizer busy/reloading. Face NOT enrolled.");
return faceIds; return faceIds;
} }
@@ -2737,9 +3089,10 @@ namespace ANSCENTER {
} }
camDataPtr->previousTrackIdCount = currentTrackIdCount; camDataPtr->previousTrackIdCount = currentTrackIdCount;
// Adapt interval: stable scene → increase (max 10), changes → decrease (min 2) // Adapt interval: stable scene → double the configured
// interval, changes → decrease (min 2)
if (camDataPtr->stableFrameCount > 30) { if (camDataPtr->stableFrameCount > 30) {
camDataPtr->currentAttributeInterval = std::min(10, _attributeInterval * 2); camDataPtr->currentAttributeInterval = _attributeInterval * 2;
} else if (camDataPtr->stableFrameCount > 10) { } else if (camDataPtr->stableFrameCount > 10) {
camDataPtr->currentAttributeInterval = _attributeInterval; camDataPtr->currentAttributeInterval = _attributeInterval;
} else { } else {
+64 -5
View File
@@ -97,6 +97,26 @@ namespace ANSCENTER
bool UpdateFaceQueue(int queueSize, int faceThresholdSize, bool enableFaceQueue); bool UpdateFaceQueue(int queueSize, int faceThresholdSize, bool enableFaceQueue);
bool GetFaceQueue(int& queueSize, int& faceThresholdSize, bool& enableFaceQueue); bool GetFaceQueue(int& queueSize, int& faceThresholdSize, bool& enableFaceQueue);
// How often the slow attributes (age/gender + emotions) refresh:
// every N frames per camera (1 = every frame). Head pose still runs
// every frame. The adaptive logic doubles N on stable scenes and
// halves it (min 2) when faces appear/disappear. Default 20 —
// benchmarked at throughput parity with the v1.1 pack while the
// heavier v1.4 ONNX age-gender model is loaded.
bool UpdateAttributeInterval(int frames);
int GetAttributeInterval() const { return _attributeInterval; }
// Recognizer model switching (0 = AdaFace [default], 1 = ArcFace).
// Before LoadEngine: just records the preference. After LoadEngine:
// re-initializes the recognizer, regenerates every stored embedding
// from the enrolled face crops (embeddings from different models are
// not comparable), and rebuilds the FAISS index.
bool SetRecognizerModelType(int modelType);
int GetRecognizerModelType() const;
// JSON summary of what is actually loaded: recognizer model plus the
// backend ("onnx" | "openvino" | "disabled") serving each attribute.
[[nodiscard]] std::string GetActiveModelInfo();
[[nodiscard]] std::vector<FaceResultObject> Recognize(const cv::Mat& frame); // Expect to return on 1 object [[nodiscard]] std::vector<FaceResultObject> Recognize(const cv::Mat& frame); // Expect to return on 1 object
[[nodiscard]] std::vector<FaceResultObject> Inference(const cv::Mat& input); [[nodiscard]] std::vector<FaceResultObject> Inference(const cv::Mat& input);
[[nodiscard]] std::vector<FaceResultObject> Detect(const cv::Mat& input); [[nodiscard]] std::vector<FaceResultObject> Detect(const cv::Mat& input);
@@ -141,6 +161,11 @@ namespace ANSCENTER
bool InitializeAgeGenderModel(const std::string& deviceName); bool InitializeAgeGenderModel(const std::string& deviceName);
bool InitializeEmotionModel(const std::string& deviceName); bool InitializeEmotionModel(const std::string& deviceName);
bool InitializeHeadPoseModel(const std::string& deviceName); bool InitializeHeadPoseModel(const std::string& deviceName);
// Regenerates stored embeddings when they were produced by a
// different recognizer model than the one currently loaded
// (tracked via the EMBEDDING_MODEL key in the face database).
bool SyncEmbeddingsWithActiveModel();
bool RegenerateEmbeddings();
void ensureUniqueUserIdWithHighestConfidence(std::vector<FaceResultObject>& resultObjects, CameraData* camData = nullptr); void ensureUniqueUserIdWithHighestConfidence(std::vector<FaceResultObject>& resultObjects, CameraData* camData = nullptr);
Object GetLargestObject(const std::vector<Object>& objects); Object GetLargestObject(const std::vector<Object>& objects);
bool AreFacesSimilar(const FaceResultObject& face1, const FaceResultObject& face2); bool AreFacesSimilar(const FaceResultObject& face1, const FaceResultObject& face2);
@@ -172,7 +197,7 @@ namespace ANSCENTER
int _faceThresholdSize; // number of frames to consider for face recognition int _faceThresholdSize; // number of frames to consider for face recognition
bool _enableFaceQueue; bool _enableFaceQueue;
double _faceDetectorScoreThreshold; // Face threshold score for face checker double _faceDetectorScoreThreshold; // Face threshold score for face checker
int _attributeInterval{ 5 }; // Run slow attributes every N frames (1=every frame) int _attributeInterval{ 20 }; // Run slow attributes every N frames (1=every frame)
FaceChecker faceChecker; FaceChecker faceChecker;
@@ -214,9 +239,18 @@ namespace ANSCENTER
std::unique_ptr<ANSFRBase>_recognizer = nullptr; std::unique_ptr<ANSFRBase>_recognizer = nullptr;
std::unique_ptr<ANSFDBase>_detector = nullptr; std::unique_ptr<ANSFDBase>_detector = nullptr;
std::unique_ptr<AntispoofingClassifier>_antiSpoofDetector = nullptr; std::unique_ptr<AntispoofingClassifier>_antiSpoofDetector = nullptr;
std::unique_ptr<AgeGenderDetection>_ageGenderDetector = nullptr; std::unique_ptr<IAgeGenderEstimator>_ageGenderDetector = nullptr;
std::unique_ptr<HeadPoseDetection>_headPoseDetector = nullptr; std::unique_ptr<HeadPoseDetection>_headPoseDetector = nullptr;
std::unique_ptr<EmotionsDetection>_emotionsDetector = nullptr; std::unique_ptr<IEmotionsClassifier>_emotionsDetector = nullptr;
// Recognizer model preference (settable before/after LoadEngine)
// and what actually loaded (fallback may downgrade ADAFACE->ARCFACE).
FaceRecognizerModel _requestedRecModel{ FaceRecognizerModel::ADAFACE };
FaceRecognizerModel _activeRecModel{ FaceRecognizerModel::ADAFACE };
// True once SetRecognizerModelType was called this session. An
// explicit choice is persisted (RECOGNIZER_MODEL_PREF in the face
// DB) and restored by LoadEngine on later runs; without it the
// stored preference (if any) wins over the built-in default.
bool _recModelExplicit{ false };
const size_t QUEUE_SIZE = 10; const size_t QUEUE_SIZE = 10;
const size_t FACE_THRESHOLD_SIZE = 5; const size_t FACE_THRESHOLD_SIZE = 5;
// Cached attribute JSON per tracked face (everything except head pose). // Cached attribute JSON per tracked face (everything except head pose).
@@ -244,7 +278,7 @@ namespace ANSCENTER
std::unordered_map<int, TrackIdentity> trackIdentities; // trackId → accumulated identity std::unordered_map<int, TrackIdentity> trackIdentities; // trackId → accumulated identity
// Adaptive interval state // Adaptive interval state
int currentAttributeInterval = 5; // current adaptive interval int currentAttributeInterval = 20; // current adaptive interval
int stableFrameCount = 0; // frames with no new/lost trackIds int stableFrameCount = 0; // frames with no new/lost trackIds
int previousTrackIdCount = 0; // trackId count from last frame int previousTrackIdCount = 0; // trackId count from last frame
@@ -258,7 +292,7 @@ namespace ANSCENTER
attributeFrameCounter = 0; attributeFrameCounter = 0;
cachedAttributes.clear(); cachedAttributes.clear();
trackIdentities.clear(); trackIdentities.clear();
currentAttributeInterval = 5; currentAttributeInterval = 20;
stableFrameCount = 0; stableFrameCount = 0;
previousTrackIdCount = 0; previousTrackIdCount = 0;
} }
@@ -348,6 +382,31 @@ extern "C" ANSFR_API int GetParamters(ANSCENTER::ANSFacialRecognition** Handl
extern "C" ANSFR_API int UpdateFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, int queueSize, int numKnownFaceInQueue, int enableFaceQueue); extern "C" ANSFR_API int UpdateFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, int queueSize, int numKnownFaceInQueue, int enableFaceQueue);
extern "C" ANSFR_API int GetFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle faceQueue); extern "C" ANSFR_API int GetFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle faceQueue);
// Recognizer model switching (ANS_FaceRecognizer v1.4+).
// modelType: 0 = AdaFace (default), 1 = ArcFace.
// Call before LoadANSRFEngine to pick the model for the initial load, or
// after it to switch live — a live switch reloads the recognizer engine and
// regenerates every stored face embedding from the enrolled face images
// (embeddings produced by different models are not comparable), so it can
// take a while on large databases. The explicit choice is persisted in the
// face database (RECOGNIZER_MODEL_PREF) and restored automatically on the
// next LoadANSRFEngine, so it survives restarts without re-calling this.
// Returns 1 on success, 0 on failure, -1 on invalid arguments.
extern "C" ANSFR_API int SetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle, int modelType);
// Returns the ACTIVE model (0 = AdaFace, 1 = ArcFace) or -1 on error. May
// differ from what was requested when the model pack has no
// adafacerecognizer.onnx (pre-v1.4 zips fall back to ArcFace).
extern "C" ANSFR_API int GetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle);
// JSON describing the loaded models, e.g.
// {"recognizer_model":"adaface","agegender_backend":"onnx","emotion_backend":"onnx",...}
extern "C" ANSFR_API int GetANSRFModelInfo(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle modelInfo);
// Slow-attribute (age/gender + emotions) refresh interval in frames per
// camera (1 = every frame, default 20; clamped to [1, 60]). Larger values
// cut the attribute-model GPU cost proportionally at the price of slower
// attribute updates; lower it for the freshest attributes on GPUs with
// headroom. Returns 1 on success, -1 on invalid arguments.
extern "C" ANSFR_API int SetANSRFAttributeInterval(ANSCENTER::ANSFacialRecognition** Handle, int frames);
extern "C" ANSFR_API int GetUserString(ANSCENTER::ANSFacialRecognition * *Handle, int userId, std::string& userRecord); extern "C" ANSFR_API int GetUserString(ANSCENTER::ANSFacialRecognition * *Handle, int userId, std::string& userRecord);
+12 -13
View File
@@ -638,30 +638,29 @@ namespace ANSCENTER {
detectedNode.put("Age", "Adults"); detectedNode.put("Age", "Adults");
else else
detectedNode.put("Age", "Elders"); detectedNode.put("Age", "Elders");
// Numeric age alongside the bucket — additive key, existing
// consumers keep parsing "Age" unchanged.
detectedNode.put("AgeYears", std::to_string(age));
} }
if (face.isEmotionsEnabled()) { if (face.isEmotionsEnabled()) {
const auto& [emotionName, emotionConfidence] = face.getMainEmotion(); const auto& [emotionName, emotionConfidence] = face.getMainEmotion();
std::ostringstream emotionStr; std::ostringstream emotionStr;
// The v1.4 ONNX classifier's classes (emotionsrecognition.names:
// Angry, Fear, Happy, Neutral, Sad) are reported VERBATIM. Only
// the legacy OpenVINO model's lowercase classes keep the
// historical display remap so v1.1 output is unchanged.
std::string adjustedEmotionName = emotionName; std::string adjustedEmotionName = emotionName;
if (emotionName == "Sad" || emotionName == "sad") if (emotionName == "sad")
adjustedEmotionName = "Attentive"; adjustedEmotionName = "Attentive";
else if (emotionName == "Happy" || emotionName == "happy") else if (emotionName == "happy")
adjustedEmotionName = "Happy"; adjustedEmotionName = "Happy";
else if (emotionName == "Neutral" || emotionName == "neutral") else if (emotionName == "neutral")
adjustedEmotionName = "Neutral"; adjustedEmotionName = "Neutral";
else if (emotionName == "Angry" || emotionName == "angry") else if (emotionName == "anger")
adjustedEmotionName = "Attentive"; adjustedEmotionName = "Attentive";
else if (emotionName == "Surprised" || emotionName == "surprised") else if (emotionName == "surprise")
adjustedEmotionName = "Attentive"; adjustedEmotionName = "Attentive";
else if (emotionName == "Disgusted" || emotionName == "disgusted")
adjustedEmotionName = "Attentive";
else if (emotionName == "Fearful" || emotionName == "fearful")
adjustedEmotionName = "Attentive";
else if (emotionName == "Anger" || emotionName == "anger")
adjustedEmotionName = "Attentive";
else
adjustedEmotionName = "Neutral";
emotionStr << adjustedEmotionName << " (" << static_cast<int>(emotionConfidence * 100) << "%)"; emotionStr << adjustedEmotionName << " (" << static_cast<int>(emotionConfidence * 100) << "%)";
detectedNode.put("Emotion", emotionStr.str()); detectedNode.put("Emotion", emotionStr.str());
+44 -4
View File
@@ -107,6 +107,42 @@ namespace ANSCENTER {
float age; float age;
float maleProb; float maleProb;
}; };
// Which face-embedding model the recognizer runs. ADAFACE is the default
// since ANS_FaceRecognizer v1.4 (adafacerecognizer.onnx); when that file
// is missing from the model zip the recognizer silently falls back to
// ARCFACE (ansfacerecognizer.onnx) so v1.3-and-older zips keep working.
enum class FaceRecognizerModel : int {
ADAFACE = 0,
ARCFACE = 1,
};
inline const char* FaceRecognizerModelName(FaceRecognizerModel m) {
return (m == FaceRecognizerModel::ARCFACE) ? "arcface" : "adaface";
}
// ----------------------------------------------------------------
// Attribute-model backends. The v1.4 model pack ships ONNX attribute
// models (agegender.onnx / emotionsrecognition.onnx) next to the
// legacy OpenVINO IR pairs (.xml/.bin). These interfaces let the
// pipeline hold either backend behind the same two calls it already
// makes: runInfer() and enabled().
// ----------------------------------------------------------------
class ANSF_API IAgeGenderEstimator {
public:
virtual ~IAgeGenderEstimator() = default;
virtual AgeGenderResults runInfer(const cv::Mat& frame) = 0;
virtual bool enabled() const = 0;
virtual const char* backendName() const = 0; // "openvino" | "onnx"
};
class ANSF_API IEmotionsClassifier {
public:
virtual ~IEmotionsClassifier() = default;
virtual std::map<std::string, float> runInfer(const cv::Mat& frame) = 0;
virtual bool enabled() const = 0;
virtual const char* backendName() const = 0; // "openvino" | "onnx"
};
//// Class for face attribute //// Class for face attribute
class ANSF_API Face { class ANSF_API Face {
public: public:
@@ -200,7 +236,7 @@ namespace ANSCENTER {
//std::vector<HeadPoseResults> getAllResults(); //std::vector<HeadPoseResults> getAllResults();
}; };
class ANSF_API AgeGenderDetection :public BaseDetection { class ANSF_API AgeGenderDetection : public BaseDetection, public IAgeGenderEstimator {
public: public:
std::recursive_mutex _mutex; std::recursive_mutex _mutex;
std::string outputAge; std::string outputAge;
@@ -211,7 +247,9 @@ namespace ANSCENTER {
bool doRawOutputMessages); bool doRawOutputMessages);
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override; virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
AgeGenderResults runInfer(const cv::Mat& frame); AgeGenderResults runInfer(const cv::Mat& frame) override;
bool enabled() const override { return BaseDetection::enabled(); }
const char* backendName() const override { return "openvino"; }
//void submitRequest(); //void submitRequest();
//void enqueue(const cv::Mat& face); //void enqueue(const cv::Mat& face);
//AgeGenderResults getResult(int idx); //AgeGenderResults getResult(int idx);
@@ -219,7 +257,7 @@ namespace ANSCENTER {
}; };
class ANSF_API EmotionsDetection : public BaseDetection { class ANSF_API EmotionsDetection : public BaseDetection, public IEmotionsClassifier {
public: public:
std::recursive_mutex _mutex; std::recursive_mutex _mutex;
//size_t enquedFaces; //size_t enquedFaces;
@@ -230,7 +268,9 @@ namespace ANSCENTER {
bool doRawOutputMessages); bool doRawOutputMessages);
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override; virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
std::map<std::string, float> runInfer(const cv::Mat& frame); std::map<std::string, float> runInfer(const cv::Mat& frame) override;
bool enabled() const override { return BaseDetection::enabled(); }
const char* backendName() const override { return "openvino"; }
//void submitRequest(); //void submitRequest();
//void enqueue(const cv::Mat& face); //void enqueue(const cv::Mat& face);
//std::map<std::string, float> getResult(int idx); //std::map<std::string, float> getResult(int idx);
+163 -37
View File
@@ -1,5 +1,7 @@
#include "ANSFaceRecognizer.h" #include "ANSFaceRecognizer.h"
#include "engine.h" #include "engine.h"
#include <cctype>
#include <filesystem>
//#define DEBUG_TIME //#define DEBUG_TIME
#ifdef DEBUG_TIME #ifdef DEBUG_TIME
@@ -17,7 +19,99 @@
#define FR_END_TIMER(name, label) #define FR_END_TIMER(name, label)
#endif #endif
namespace {
// Batch >1 only helps when several faces arrive in one frame, but the TRT
// workspace grows ~linearly with maxBatchSize (~54 MiB per batch unit for
// this model). The batch-4 profile was tuned for 8 GB GPUs; on smaller
// cards (e.g. 4 GB RTX A400) that workspace crowds out the other engines
// sharing the card. Below the 8 GB class, fall back to single-face
// batches — results are identical, multi-face frames just run
// sequentially. Threshold is 7.5 GiB because 8 GB cards report slightly
// under 8192 MiB (~8188).
int VramCappedBatch(int desiredBatch, int deviceIndex) {
cudaDeviceProp prop{};
if (cudaGetDeviceProperties(&prop, deviceIndex >= 0 ? deviceIndex : 0) != cudaSuccess)
return desiredBatch; // can't query — keep the configured profile
const size_t totalMiB = prop.totalGlobalMem / (1024ULL * 1024ULL);
return (totalMiB >= 7680) ? desiredBatch : 1;
}
// A ServerOptimised zip is a per-GPU cache of a previous engine build. When
// one was supplied but the engine still had to be rebuilt from ONNX, the
// zip's cached engine is stale (batch profile or TRT/CUDA version changed).
// Delete the stale engine variants for this GPU from the model folder (so a
// regenerated zip doesn't carry two engines) and the zip itself — the
// ANSVIS Server recreates the zip from the model folder when it is missing.
// Base model zips (the ONNX source artifact) are never touched: only zips
// whose parent folder is ServerOptimised qualify.
void PurgeStaleOptimisedModel(const std::string& modelZipFilePath,
const std::string& currentEnginePath) {
namespace fs = std::filesystem;
try {
const fs::path zip(modelZipFilePath);
std::string parent = zip.parent_path().filename().string();
for (char& c : parent) c = (char)std::tolower((unsigned char)c);
if (parent != "serveroptimised" && parent != "serveroptimized")
return;
// Stale variants share the "<model>.engine.<GPU>." prefix of the
// freshly built engine; other GPUs' engines on multi-GPU machines
// have a different prefix and are left alone.
const fs::path engine(currentEnginePath);
const std::string engineName = engine.filename().string();
const size_t tagPos = engineName.find(".engine.");
if (tagPos != std::string::npos && engine.has_parent_path()) {
const size_t gpuEnd = engineName.find('.', tagPos + 8);
const std::string prefix = (gpuEnd != std::string::npos)
? engineName.substr(0, gpuEnd + 1) : engineName;
for (const auto& entry : fs::directory_iterator(engine.parent_path())) {
if (!entry.is_regular_file()) continue;
const std::string name = entry.path().filename().string();
if (name != engineName && name.rfind(prefix, 0) == 0) {
std::error_code ec;
fs::remove(entry.path(), ec);
if (!ec) std::cout << "[ANSFR] Purged stale engine: " << name << std::endl;
}
}
}
std::error_code ec;
fs::remove(zip, ec);
if (!ec) std::cout << "[ANSFR] Purged stale optimised model zip (server will regenerate): "
<< modelZipFilePath << std::endl;
}
catch (const std::exception&) {
// Cleanup is best-effort; a leftover stale zip only costs a rebuild.
}
}
}
namespace ANSCENTER { namespace ANSCENTER {
std::string ANSFaceRecognizer::ResolveRecognizerModelFile() {
const std::string adaPath = CreateFilePath(_modelFolder, "adafacerecognizer.onnx");
const std::string arcPath = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (_preferredModel == FaceRecognizerModel::ADAFACE) {
if (std::filesystem::exists(adaPath)) {
_activeModel = FaceRecognizerModel::ADAFACE;
m_swapRB = false; // AdaFace consumes BGR
ANS_DBG("FaceRecognizer", "Recognizer model resolved: ADAFACE (%s)", adaPath.c_str());
return adaPath;
}
_logger.LogWarn("ANSFaceRecognizer::ResolveRecognizerModelFile",
"adafacerecognizer.onnx not found in model pack — falling back to ArcFace",
__FILE__, __LINE__);
}
_activeModel = FaceRecognizerModel::ARCFACE;
m_swapRB = true; // ArcFace consumes RGB
if (std::filesystem::exists(arcPath)) {
ANS_DBG("FaceRecognizer", "Recognizer model resolved: ARCFACE (%s)", arcPath.c_str());
return arcPath;
}
return std::string();
}
std::string ANSFaceRecognizer::GetOpenVINODevice() { std::string ANSFaceRecognizer::GetOpenVINODevice() {
ov::Core core; ov::Core core;
std::vector<std::string> available_devices = core.get_available_devices(); std::vector<std::string> available_devices = core.get_available_devices();
@@ -77,16 +171,16 @@ namespace ANSCENTER {
static_cast<int>(EngineType::AMD_GPU), static_cast<int>(EngineType::AMD_GPU),
static_cast<int>(EngineType::CPU)); static_cast<int>(EngineType::CPU));
if (engineType == EngineType::NVIDIA_GPU) { if (engineType == EngineType::NVIDIA_GPU) {
// 1. Load ONNX model // 1. Load ONNX model (adaface by default, arcface fallback)
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx"); std::string onnxfile = ResolveRecognizerModelFile();
if (std::filesystem::exists(onnxfile)) { if (!onnxfile.empty()) {
_modelFilePath = onnxfile; _modelFilePath = onnxfile;
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading ansfacerecognizer weight", _logger.LogDebug("ANSFaceRecognizer::Initialize. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__); _modelFilePath, __FILE__, __LINE__);
} }
else { else {
_logger.LogError("ANSFaceRecognizer::Initialize. Model ansfacerecognizer.onnx does not exist", _logger.LogError("ANSFaceRecognizer::Initialize. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) exists",
_modelFilePath, __FILE__, __LINE__); _modelFolder, __FILE__, __LINE__);
return false; return false;
} }
@@ -97,8 +191,10 @@ namespace ANSCENTER {
// comfortably on 8 GB GPUs while freeing ~1.5 GB VRAM vs max=32 // comfortably on 8 GB GPUs while freeing ~1.5 GB VRAM vs max=32
// — most scenes have ≤4 faces visible, so throughput cost is // — most scenes have ≤4 faces visible, so throughput cost is
// near-zero (amortized per-face latency drops too at lower batch). // near-zero (amortized per-face latency drops too at lower batch).
m_options.optBatchSize = 4; // VramCappedBatch drops this to 1 on sub-8 GB cards.
m_options.maxBatchSize = 4; const int frBatch = VramCappedBatch(4, _modelConfig.gpuDeviceIndex);
m_options.optBatchSize = frBatch;
m_options.maxBatchSize = frBatch;
m_options.maxInputHeight = GPU_FACE_HEIGHT; m_options.maxInputHeight = GPU_FACE_HEIGHT;
m_options.minInputHeight = GPU_FACE_HEIGHT; m_options.minInputHeight = GPU_FACE_HEIGHT;
@@ -127,6 +223,13 @@ namespace ANSCENTER {
return false; return false;
} }
// If we were handed a ServerOptimised zip yet still had to
// rebuild from ONNX, that zip's engine is stale — remove it
// so the server regenerates it with the fresh engine.
if (m_trtEngine->rebuiltFromOnnx()) {
PurgeStaleOptimisedModel(modelZipFilePath, m_trtEngine->engineFilePath());
}
// Create CUDA stream for GPU preprocessing (lazy init) // Create CUDA stream for GPU preprocessing (lazy init)
if (!m_gpuStream) { if (!m_gpuStream) {
m_gpuStream = std::make_unique<cv::cuda::Stream>(); m_gpuStream = std::make_unique<cv::cuda::Stream>();
@@ -135,24 +238,28 @@ namespace ANSCENTER {
} }
else { else {
#ifdef USE_ONNX_ENGINE #ifdef USE_ONNX_ENGINE
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx"); std::string faceidModel = ResolveRecognizerModelFile();
if (std::filesystem::exists(faceidModel)) { if (!faceidModel.empty()) {
_modelFilePath = faceidModel; _modelFilePath = faceidModel;
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading ANSFaceRecognizer weight", _logger.LogDebug("ANSFaceRecognizer::Initialize. Loading ANSFaceRecognizer weight",
_modelFilePath, __FILE__, __LINE__); _modelFilePath, __FILE__, __LINE__);
} }
else { else {
_logger.LogError("ANSFaceRecognizer::Initialize. Model ansfacerecognizer.onnx does not exist", _logger.LogError("ANSFaceRecognizer::Initialize. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) exists",
_modelFilePath, __FILE__, __LINE__); _modelFolder, __FILE__, __LINE__);
return false; return false;
} }
ANS_DBG("FaceRecognizer", "Creating GlintArcFace with engineType=%d model=%s", ANS_DBG("FaceRecognizer", "Creating %s with engineType=%d model=%s",
FaceRecognizerModelName(_activeModel),
static_cast<int>(engineType), faceidModel.c_str()); static_cast<int>(engineType), faceidModel.c_str());
faceRecognizer = std::make_unique<GlintArcFace>(faceidModel, engineType); if (_activeModel == FaceRecognizerModel::ADAFACE)
faceRecognizer = std::make_unique<AdaFace>(faceidModel, engineType);
else
faceRecognizer = std::make_unique<GlintArcFace>(faceidModel, engineType);
if (!faceRecognizer) { if (!faceRecognizer) {
_logger.LogFatal("ANSFaceRecognizer::Initialize", _logger.LogFatal("ANSFaceRecognizer::Initialize",
"Failed to initialize ONNX face recognizer", __FILE__, __LINE__); "Failed to initialize ONNX face recognizer", __FILE__, __LINE__);
ANS_DBG("FaceRecognizer", "FAILED: GlintArcFace returned null"); ANS_DBG("FaceRecognizer", "FAILED: ONNX face recognizer returned null");
return false; return false;
} }
#else #else
@@ -201,29 +308,29 @@ namespace ANSCENTER {
if (!result) return false; if (!result) return false;
if (engineType == EngineType::NVIDIA_GPU) { if (engineType == EngineType::NVIDIA_GPU) {
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx"); std::string onnxfile = ResolveRecognizerModelFile();
if (std::filesystem::exists(onnxfile)) { if (!onnxfile.empty()) {
_modelFilePath = onnxfile; _modelFilePath = onnxfile;
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading ansfacerecognizer weight", _logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__); _modelFilePath, __FILE__, __LINE__);
} }
else { else {
_logger.LogError("ANSFaceRecognizer::LoadModel. ansfacerecognizer.onnx not found", _logger.LogError("ANSFaceRecognizer::LoadModel. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) found",
_modelFilePath, __FILE__, __LINE__); _modelFolder, __FILE__, __LINE__);
return false; return false;
} }
} }
else { else {
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx"); std::string faceidModel = ResolveRecognizerModelFile();
#ifdef USE_ONNX_ENGINE #ifdef USE_ONNX_ENGINE
if (std::filesystem::exists(faceidModel)) { if (!faceidModel.empty()) {
_modelFilePath = faceidModel; _modelFilePath = faceidModel;
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading ansfacerecognizer weight", _logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__); _modelFilePath, __FILE__, __LINE__);
} }
else { else {
_logger.LogError("ANSFaceRecognizer::LoadModel. ansfacerecognizer.onnx not found", _logger.LogError("ANSFaceRecognizer::LoadModel. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) found",
_modelFilePath, __FILE__, __LINE__); _modelFolder, __FILE__, __LINE__);
return false; return false;
} }
#else #else
@@ -259,9 +366,10 @@ namespace ANSCENTER {
// engine built/looked-up here has a different profile than the one // engine built/looked-up here has a different profile than the one
// Initialize() expects, causing either a stale `.b32` rebuild every // Initialize() expects, causing either a stale `.b32` rebuild every
// launch (wasting 30-60s) or a load-time profile mismatch. // launch (wasting 30-60s) or a load-time profile mismatch.
// Keep in sync with lines 100-114. // Keep in sync with the Initialize() block (same VramCappedBatch call).
m_options.optBatchSize = 4; const int frBatch = VramCappedBatch(4, _modelConfig.gpuDeviceIndex);
m_options.maxBatchSize = 4; m_options.optBatchSize = frBatch;
m_options.maxBatchSize = frBatch;
m_options.maxInputHeight = GPU_FACE_HEIGHT; m_options.maxInputHeight = GPU_FACE_HEIGHT;
m_options.minInputHeight = GPU_FACE_HEIGHT; m_options.minInputHeight = GPU_FACE_HEIGHT;
@@ -490,8 +598,12 @@ namespace ANSCENTER {
if (engineType == EngineType::NVIDIA_GPU) { if (engineType == EngineType::NVIDIA_GPU) {
// Initialize TensorRT engine via shared pool // Initialize TensorRT engine via shared pool
if (!m_trtEngine) { if (!m_trtEngine) {
m_options.optBatchSize = 8; // Note this path still uses the legacy 8/32 profile on 8 GB+
m_options.maxBatchSize = 32; // cards (a .b32 engine costs ~1.5 GB more workspace than .b4).
// The VRAM cap only stops it from being fatal on small cards.
const int frMaxBatch = VramCappedBatch(32, _modelConfig.gpuDeviceIndex);
m_options.optBatchSize = (frMaxBatch < 8) ? frMaxBatch : 8;
m_options.maxBatchSize = frMaxBatch;
m_options.calibrationBatchSize = 8; m_options.calibrationBatchSize = 8;
m_options.deviceIndex = _modelConfig.gpuDeviceIndex; m_options.deviceIndex = _modelConfig.gpuDeviceIndex;
m_options.engineFileDir = _modelFolder; m_options.engineFileDir = _modelFolder;
@@ -751,7 +863,7 @@ namespace ANSCENTER {
} }
try { try {
// CPU preprocessing: resize + BGR→RGB before GPU upload // CPU preprocessing: resize + channel order before GPU upload
// Reduces PCIe transfer and eliminates GPU cvtColor/resize overhead // Reduces PCIe transfer and eliminates GPU cvtColor/resize overhead
cv::Mat srcImg = inputImage; cv::Mat srcImg = inputImage;
if (srcImg.channels() == 1) { if (srcImg.channels() == 1) {
@@ -763,8 +875,13 @@ namespace ANSCENTER {
} else { } else {
cpuResized = srcImg; cpuResized = srcImg;
} }
// ArcFace needs RGB; AdaFace consumes the Mat's native BGR.
cv::Mat cpuRGB; cv::Mat cpuRGB;
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB); if (m_swapRB) {
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
} else {
cpuRGB = cpuResized;
}
m_gpuRgb.upload(cpuRGB, *m_gpuStream); m_gpuRgb.upload(cpuRGB, *m_gpuStream);
m_gpuStream->waitForCompletion(); m_gpuStream->waitForCompletion();
@@ -880,14 +997,19 @@ namespace ANSCENTER {
cv::cuda::resize(d_img, d_resized, targetSize, 0, 0, cv::INTER_LINEAR, *m_gpuStream); cv::cuda::resize(d_img, d_resized, targetSize, 0, 0, cv::INTER_LINEAR, *m_gpuStream);
d_img = d_resized; d_img = d_resized;
} }
cv::cuda::GpuMat d_rgb; // ArcFace needs RGB; AdaFace consumes native BGR.
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, *m_gpuStream); if (m_swapRB) {
batchGpu.emplace_back(std::move(d_rgb)); cv::cuda::GpuMat d_rgb;
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, *m_gpuStream);
batchGpu.emplace_back(std::move(d_rgb));
} else {
batchGpu.emplace_back(std::move(d_img));
}
} else { } else {
const auto& roi = faceROIs[i]; const auto& roi = faceROIs[i];
if (roi.empty()) continue; if (roi.empty()) continue;
// CPU preprocessing: resize + BGR→RGB before upload // CPU preprocessing: resize + channel order before upload
cv::Mat srcImg = roi; cv::Mat srcImg = roi;
if (srcImg.channels() == 1) { if (srcImg.channels() == 1) {
cv::cvtColor(srcImg, srcImg, cv::COLOR_GRAY2BGR); cv::cvtColor(srcImg, srcImg, cv::COLOR_GRAY2BGR);
@@ -899,7 +1021,11 @@ namespace ANSCENTER {
cpuResized = srcImg; cpuResized = srcImg;
} }
cv::Mat cpuRGB; cv::Mat cpuRGB;
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB); if (m_swapRB) {
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
} else {
cpuRGB = cpuResized;
}
cv::cuda::GpuMat d_rgb; cv::cuda::GpuMat d_rgb;
d_rgb.upload(cpuRGB, *m_gpuStream); d_rgb.upload(cpuRGB, *m_gpuStream);
batchGpu.emplace_back(std::move(d_rgb)); batchGpu.emplace_back(std::move(d_rgb));
+18
View File
@@ -53,6 +53,12 @@ namespace ANSCENTER {
int GetEmbeddingSize() const { return FACE_EMBEDDING_SIZE; } int GetEmbeddingSize() const { return FACE_EMBEDDING_SIZE; }
std::shared_ptr<faiss::gpu::StandardGpuResources> GetGpuResources() const { return m_gpuResources; } std::shared_ptr<faiss::gpu::StandardGpuResources> GetGpuResources() const { return m_gpuResources; }
// Which embedding model to load (call BEFORE Initialize). Default is
// ADAFACE; Initialize falls back to ARCFACE when the model pack has
// no adafacerecognizer.onnx (pre-v1.4 zips).
void SetPreferredModel(FaceRecognizerModel m) { _preferredModel = m; }
FaceRecognizerModel GetActiveModel() const { return _activeModel; }
bool UpdateParamater(double knownPersonThreshold) { bool UpdateParamater(double knownPersonThreshold) {
_modelConfig.unknownPersonThreshold = knownPersonThreshold; _modelConfig.unknownPersonThreshold = knownPersonThreshold;
m_knownPersonThresh = _modelConfig.unknownPersonThreshold; m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
@@ -68,6 +74,11 @@ namespace ANSCENTER {
private: private:
bool LoadEngine(const std::string& xmlModelPath, bool engineOptimisation = true); bool LoadEngine(const std::string& xmlModelPath, bool engineOptimisation = true);
// Resolves the .onnx file for _preferredModel, falling back
// ADAFACE -> ARCFACE when adafacerecognizer.onnx is absent. Sets
// _activeModel and m_swapRB; returns "" when nothing usable exists.
std::string ResolveRecognizerModelFile();
// Batched forward: one embedding per Object.mask (caller must hold _mutex) // Batched forward: one embedding per Object.mask (caller must hold _mutex)
std::vector<std::vector<float>> ForwardUnlocked(const cv::Mat& input,const std::vector<ANSCENTER::Object>& outputBbox); std::vector<std::vector<float>> ForwardUnlocked(const cv::Mat& input,const std::vector<ANSCENTER::Object>& outputBbox);
@@ -127,6 +138,13 @@ namespace ANSCENTER {
float m_knownPersonThresh = 0.35f; float m_knownPersonThresh = 0.35f;
EngineType engineType; EngineType engineType;
FaceRecognizerModel _preferredModel = FaceRecognizerModel::ADAFACE;
FaceRecognizerModel _activeModel = FaceRecognizerModel::ADAFACE;
// ArcFace was trained on RGB (needs the BGR->RGB swap); AdaFace on
// BGR (OpenCV native order, no swap). Gates every cvtColor in the
// TensorRT preprocessing paths.
bool m_swapRB = true;
#ifdef USE_ONNX_ENGINE #ifdef USE_ONNX_ENGINE
std::unique_ptr<GlintArcFace> faceRecognizer = nullptr; std::unique_ptr<GlintArcFace> faceRecognizer = nullptr;
const int CPU_FACE_WIDTH = 112; const int CPU_FACE_WIDTH = 112;
+1
View File
@@ -4,6 +4,7 @@ set(ANSFR_SOURCES
ANSFR.cpp ANSFR.cpp
ANSFRCommon.cpp ANSFRCommon.cpp
ANSFaceRecognizer.cpp ANSFaceRecognizer.cpp
ONNXFaceAttributes.cpp
ANSGpuFrameRegistry.cpp ANSGpuFrameRegistry.cpp
GpuNV12SlotPool.cpp GpuNV12SlotPool.cpp
FaceDatabase.cpp FaceDatabase.cpp
+79
View File
@@ -265,6 +265,85 @@ namespace ANSCENTER {
} }
} }
int FaceDatabase::GetAllFaces(std::vector<FaceRecord>& faces) {
std::lock_guard<std::mutex> lock(_mutex);
faces.clear();
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db, "SELECT ID, USER, IMG_PATH FROM FACE;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::GetAllFaces", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return -1;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
FaceRecord record;
record.FaceId = sqlite3_column_int(stmt, 0);
record.UserId = sqlite3_column_int(stmt, 1);
const unsigned char* path = sqlite3_column_text(stmt, 2);
record.ImagePath = path ? reinterpret_cast<const char*>(path) : "";
faces.push_back(std::move(record));
}
sqlite3_finalize(stmt);
return 0;
}
int FaceDatabase::UpdateFaceEmbedding(int faceId, const float embedding[]) {
std::lock_guard<std::mutex> lock(_mutex);
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db, "UPDATE FACE SET EMBEDDING=? WHERE ID=?;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::UpdateFaceEmbedding", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return -1;
}
int resultCode = 1;
if (sqlite3_bind_blob(stmt, 1, embedding, sizeof(float) * m_embedding_dim, SQLITE_STATIC) != SQLITE_OK)
resultCode = -2;
if (sqlite3_bind_int(stmt, 2, faceId) != SQLITE_OK)
resultCode = -2;
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
_logger.LogError("FaceDatabase::UpdateFaceEmbedding", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
sqlite3_finalize(stmt);
return resultCode;
}
bool FaceDatabase::SetSetting(const std::string& key, const std::string& value) {
std::lock_guard<std::mutex> lock(_mutex);
// Older databases predate the SETTINGS table — create on demand.
sqlite3_exec(m_db,
"CREATE TABLE IF NOT EXISTS SETTINGS (KEY TEXT PRIMARY KEY, VALUE TEXT);",
nullptr, nullptr, nullptr);
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db,
"INSERT INTO SETTINGS (KEY, VALUE) VALUES (?, ?) "
"ON CONFLICT(KEY) DO UPDATE SET VALUE=excluded.VALUE;",
-1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::SetSetting", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return false;
}
sqlite3_bind_text(stmt, 1, key.c_str(), int(key.length()), SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, value.c_str(), int(value.length()), SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return (rc == SQLITE_ROW || rc == SQLITE_DONE);
}
std::string FaceDatabase::GetSetting(const std::string& key) {
std::lock_guard<std::mutex> lock(_mutex);
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db, "SELECT VALUE FROM SETTINGS WHERE KEY=?;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
// Missing table (legacy database) — treat as "no value".
return std::string();
}
std::string value;
sqlite3_bind_text(stmt, 1, key.c_str(), int(key.length()), SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char* v = sqlite3_column_text(stmt, 0);
if (v) value = reinterpret_cast<const char*>(v);
}
sqlite3_finalize(stmt);
return value;
}
int FaceDatabase::DeleteFace(int id) { int FaceDatabase::DeleteFace(int id) {
try { try {
// Fix #4: Single lock scope — GetFaceById + DELETE in one atomic section // Fix #4: Single lock scope — GetFaceById + DELETE in one atomic section
+9
View File
@@ -35,6 +35,15 @@ namespace ANSCENTER {
int GetUsersWithFaceIds(std::vector<int>& ids, std::vector<std::string>& codes, int GetUsersWithFaceIds(std::vector<int>& ids, std::vector<std::string>& codes,
std::vector<std::string>& userNames, std::vector<std::vector<int>>& faceIdsByUser); std::vector<std::string>& userNames, std::vector<std::vector<int>>& faceIdsByUser);
int GetFaceById(int faceId, int& userId, std::string& imagePath); int GetFaceById(int faceId, int& userId, std::string& imagePath);
// All enrolled faces (ID, USER, IMG_PATH) — used to regenerate
// embeddings after a recognizer model switch.
int GetAllFaces(std::vector<FaceRecord>& faces);
// Replaces the stored embedding of one face (same dim as Initialize).
int UpdateFaceEmbedding(int faceId, const float embedding[]);
// Simple key/value store (SETTINGS table, created on demand) — used
// to persist which recognizer model generated the stored embeddings.
bool SetSetting(const std::string& key, const std::string& value);
std::string GetSetting(const std::string& key);
std::map<std::string, std::string> GetUserDict(); std::map<std::string, std::string> GetUserDict();
int GetEmbeddings(ANSFRBase& recognizer); int GetEmbeddings(ANSFRBase& recognizer);
int GetEmbeddings(std::unique_ptr<ANSFRBase>& recognizer); int GetEmbeddings(std::unique_ptr<ANSFRBase>& recognizer);
+256
View File
@@ -0,0 +1,256 @@
#include "ONNXFaceAttributes.h"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <filesystem>
namespace {
// The exported classifier may or may not end with a softmax node
// (Ultralytics kept flipping this across versions). Treat the output
// as probabilities only when it already looks like a distribution.
void SoftmaxIfNeeded(std::vector<float>& v) {
if (v.empty()) return;
float sum = 0.0f;
bool inRange = true;
for (float x : v) {
if (x < 0.0f || x > 1.0f) { inRange = false; break; }
sum += x;
}
if (inRange && std::fabs(sum - 1.0f) < 0.05f) return;
float maxVal = *std::max_element(v.begin(), v.end());
float denom = 0.0f;
for (float& x : v) {
x = std::exp(x - maxVal);
denom += x;
}
if (denom > 0.0f) {
for (float& x : v) x /= denom;
}
}
void ResolveInputSize(const std::vector<int64_t>& dims, int& width, int& height) {
// NCHW; dynamic dims come back as -1 — the shipped models are 224x224.
width = 224;
height = 224;
if (dims.size() >= 4) {
if (dims[2] > 0) height = static_cast<int>(dims[2]);
if (dims[3] > 0) width = static_cast<int>(dims[3]);
}
}
}
namespace ANSCENTER {
// ================================================================
// OnnxAgeGenderEstimator
// ================================================================
OnnxAgeGenderEstimator::OnnxAgeGenderEstimator(const std::string& onnxPath,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads)
: BasicOrtHandler(onnxPath, engineType, options, numThreads)
{
_ready = true;
}
Ort::Value OnnxAgeGenderEstimator::transform(const cv::Mat& mat)
{
if (mat.empty())
throw std::runtime_error("OnnxAgeGenderEstimator::transform — input is empty.");
int width = 0, height = 0;
ResolveInputSize(input_node_dims, width, height);
cv::Mat canvas = mat;
if (canvas.channels() == 1)
cv::cvtColor(canvas, canvas, cv::COLOR_GRAY2BGR);
cv::Mat resized;
cv::resize(canvas, resized, cv::Size(width, height), 0, 0, cv::INTER_LINEAR);
cv::cvtColor(resized, resized, cv::COLOR_BGR2RGB);
resized.convertTo(resized, CV_32FC3, 1.0 / 255.0);
// ImageNet normalization (ViTFeatureExtractor defaults)
static const float mean_vals[3] = { 0.485f, 0.456f, 0.406f };
static const float scale_vals[3] = { 1.f / 0.229f, 1.f / 0.224f, 1.f / 0.225f };
utils::transform::normalize_inplace(resized, mean_vals, scale_vals);
std::vector<int64_t> shape = input_node_dims;
if (shape.size() < 4) shape = { 1, 3, height, width };
if (shape[0] <= 0) shape[0] = 1;
if (shape[2] <= 0) shape[2] = height;
if (shape[3] <= 0) shape[3] = width;
return utils::transform::create_tensor(
resized, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
Ort::Value OnnxAgeGenderEstimator::transformBatch(const std::vector<cv::Mat>&)
{
throw std::runtime_error("OnnxAgeGenderEstimator::transformBatch — not supported.");
}
AgeGenderResults OnnxAgeGenderEstimator::runInfer(const cv::Mat& frame)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!_ready || frame.empty()) return { -1, -1 };
Ort::Value input_tensor = transform(frame);
DmlInferenceGuard dmlGuard(m_engineType, "OnnxAgeGenderEstimator::runInfer");
auto output_tensors = ort_session->Run(
Ort::RunOptions{ nullptr },
input_node_names.data(), &input_tensor, 1,
output_node_names.data(), num_outputs);
// Two known export layouts, distinguished by tensor sizes:
// (a) v1.4 pack: age_pred [1] (years) + gender_logits [1,2]
// with class order [male, female]
// (b) research export: a single [1,2] tensor of
// [age_years, gender_logit] where sigmoid >= 0.5 -> Female
const float* agePtr = nullptr;
const float* genderLogits = nullptr;
for (auto& tensor : output_tensors) {
const size_t count =
tensor.GetTensorTypeAndShapeInfo().GetElementCount();
if (count == 1 && !agePtr)
agePtr = tensor.GetTensorMutableData<float>();
else if (count >= 2 && !genderLogits)
genderLogits = tensor.GetTensorMutableData<float>();
}
if (agePtr && genderLogits) { // layout (a)
const float age = std::clamp(agePtr[0], 0.0f, 100.0f);
const float m = genderLogits[0], f = genderLogits[1];
const float mx = std::max(m, f);
const float em = std::exp(m - mx), ef = std::exp(f - mx);
return { age, em / (em + ef) };
}
if (genderLogits) { // layout (b)
const float age = std::clamp(genderLogits[0], 0.0f, 100.0f);
const float femaleProb = 1.0f / (1.0f + std::exp(-genderLogits[1]));
return { age, 1.0f - femaleProb };
}
return { -1, -1 };
}
catch (const std::exception& error) {
std::cerr << "OnnxAgeGenderEstimator error: " << error.what() << std::endl;
return { -1, -1 };
}
}
// ================================================================
// OnnxEmotionsClassifier
// ================================================================
std::vector<std::string> OnnxEmotionsClassifier::LoadClassNames(const std::string& namesFilePath)
{
std::vector<std::string> names;
try {
std::ifstream file(namesFilePath);
std::string line;
while (std::getline(file, line)) {
while (!line.empty() && (line.back() == '\r' || line.back() == '\n'
|| line.back() == ' ' || line.back() == '\t'))
line.pop_back();
if (!line.empty()) names.push_back(line);
}
}
catch (const std::exception&) {
names.clear();
}
if (names.empty()) {
names = { "neutral", "happy", "sad", "surprise", "anger" };
}
return names;
}
OnnxEmotionsClassifier::OnnxEmotionsClassifier(const std::string& onnxPath,
std::vector<std::string> classNames,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads)
: BasicOrtHandler(onnxPath, engineType, options, numThreads)
, _classNames(std::move(classNames))
{
if (_classNames.empty())
_classNames = { "neutral", "happy", "sad", "surprise", "anger" };
_ready = true;
}
Ort::Value OnnxEmotionsClassifier::transform(const cv::Mat& mat)
{
if (mat.empty())
throw std::runtime_error("OnnxEmotionsClassifier::transform — input is empty.");
int width = 0, height = 0;
ResolveInputSize(input_node_dims, width, height);
cv::Mat canvas = mat;
if (canvas.channels() == 1)
cv::cvtColor(canvas, canvas, cv::COLOR_GRAY2BGR);
cv::Mat resized;
cv::resize(canvas, resized, cv::Size(width, height), 0, 0, cv::INTER_LINEAR);
cv::cvtColor(resized, resized, cv::COLOR_BGR2RGB);
// Ultralytics classifier preprocessing: RGB, scaled to [0, 1]
resized.convertTo(resized, CV_32FC3, 1.0 / 255.0);
std::vector<int64_t> shape = input_node_dims;
if (shape.size() < 4) shape = { 1, 3, height, width };
if (shape[0] <= 0) shape[0] = 1;
if (shape[2] <= 0) shape[2] = height;
if (shape[3] <= 0) shape[3] = width;
return utils::transform::create_tensor(
resized, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
Ort::Value OnnxEmotionsClassifier::transformBatch(const std::vector<cv::Mat>&)
{
throw std::runtime_error("OnnxEmotionsClassifier::transformBatch — not supported.");
}
std::map<std::string, float> OnnxEmotionsClassifier::runInfer(const cv::Mat& frame)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!_ready || frame.empty()) return { };
Ort::Value input_tensor = transform(frame);
DmlInferenceGuard dmlGuard(m_engineType, "OnnxEmotionsClassifier::runInfer");
auto output_tensors = ort_session->Run(
Ort::RunOptions{ nullptr },
input_node_names.data(), &input_tensor, 1,
output_node_names.data(), num_outputs);
auto info = output_tensors.at(0).GetTensorTypeAndShapeInfo();
const size_t count = info.GetElementCount();
if (count == 0) return { };
const float* vals = output_tensors.at(0).GetTensorMutableData<float>();
std::vector<float> scores(vals, vals + count);
SoftmaxIfNeeded(scores);
std::map<std::string, float> emotions;
for (size_t i = 0; i < scores.size(); ++i) {
const std::string name = (i < _classNames.size())
? _classNames[i]
: ("class_" + std::to_string(i));
emotions[name] = scores[i];
}
return emotions;
}
catch (const std::exception& error) {
std::cerr << "OnnxEmotionsClassifier error: " << error.what() << std::endl;
return { };
}
}
}
+69
View File
@@ -0,0 +1,69 @@
#ifndef ONNXFACEATTRIBUTES_H
#define ONNXFACEATTRIBUTES_H
#pragma once
// ONNX Runtime backends for the face attribute models shipped with
// ANS_FaceRecognizer v1.4:
// - agegender.onnx ViT-Base age/gender head (224x224, ImageNet norm,
// 2 logits: [age 0..100, gender sigmoid -> Female >= 0.5])
// - emotionsrecognition.onnx YOLOv11 classifier (class list in
// emotionsrecognition.names)
//
// Execution provider follows the detected hardware through BasicOrtHandler:
// NVIDIA -> TensorRT EP (disk-cached engines) with CUDA EP fallback,
// AMD -> DirectML, Intel -> OpenVINO, otherwise CPU.
#include "ANSFRCommon.h"
#include "ONNXEngine.h"
#include <mutex>
namespace ANSCENTER {
class OnnxAgeGenderEstimator : public BasicOrtHandler, public IAgeGenderEstimator {
public:
OnnxAgeGenderEstimator(const std::string& onnxPath,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads = 2);
~OnnxAgeGenderEstimator() override = default;
AgeGenderResults runInfer(const cv::Mat& frame) override;
bool enabled() const override { return _ready; }
const char* backendName() const override { return "onnx"; }
private:
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
std::recursive_mutex _mutex;
bool _ready{ false };
};
class OnnxEmotionsClassifier : public BasicOrtHandler, public IEmotionsClassifier {
public:
OnnxEmotionsClassifier(const std::string& onnxPath,
std::vector<std::string> classNames,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads = 2);
~OnnxEmotionsClassifier() override = default;
std::map<std::string, float> runInfer(const cv::Mat& frame) override;
bool enabled() const override { return _ready; }
const char* backendName() const override { return "onnx"; }
// Reads one class name per line; returns the legacy 5-emotion list
// when the file is missing/empty so the classifier still produces
// labelled output.
static std::vector<std::string> LoadClassNames(const std::string& namesFilePath);
private:
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
std::recursive_mutex _mutex;
std::vector<std::string> _classNames;
bool _ready{ false };
};
}
#endif
+50
View File
@@ -887,6 +887,8 @@ extern "C" ANSFR_API int InsertFaces(ANSCENTER::ANSFacialRecognition**
extern "C" ANSFR_API int CheckFaceEmbedding(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength) { extern "C" ANSFR_API int CheckFaceEmbedding(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength) {
ANS_DBG("ANSFR","CheckFaceEmbedding: HandlePtr=%p, *Handle=%p, bufLen=%u", ANS_DBG("ANSFR","CheckFaceEmbedding: HandlePtr=%p, *Handle=%p, bufLen=%u",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), bufferLength); (void*)Handle, (void*)(Handle ? *Handle : nullptr), bufferLength);
//return 1; // by pass
try { try {
if (!Handle || !*Handle || !jpeg_string || bufferLength == 0) return -1; if (!Handle || !*Handle || !jpeg_string || bufferLength == 0) return -1;
cv::Mat frame = cv::imdecode(cv::Mat(1, bufferLength, CV_8UC1, jpeg_string), cv::IMREAD_COLOR); cv::Mat frame = cv::imdecode(cv::Mat(1, bufferLength, CV_8UC1, jpeg_string), cv::IMREAD_COLOR);
@@ -1292,6 +1294,54 @@ extern "C" ANSFR_API int GetFaceQueue(ANSCENTER::ANSFacialRecognition** Handl
} }
} }
extern "C" ANSFR_API int SetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle, int modelType) {
ANS_DBG("ANSFR","SetANSRFRecognizerModel: HandlePtr=%p, *Handle=%p, modelType=%d (0=AdaFace,1=ArcFace)",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), modelType);
try {
if (!Handle || !*Handle) return -1;
if (modelType < 0 || modelType > 1) return -1;
FRHandleGuard guard(AcquireFRHandle(*Handle));
if (!guard) return -1;
return (*Handle)->SetRecognizerModelType(modelType) ? 1 : 0;
}
catch (const std::exception& e) {
ANS_DBG("ANSFR","SetANSRFRecognizerModel EXCEPTION: %s", e.what());
return -1;
}
}
extern "C" ANSFR_API int GetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle) {
try {
if (!Handle || !*Handle) return -1;
return (*Handle)->GetRecognizerModelType();
}
catch (const std::exception&) {
return -1;
}
}
extern "C" ANSFR_API int GetANSRFModelInfo(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle modelInfo) {
try {
if (!Handle || !*Handle || !modelInfo) return -1;
return CopyToLStrHandle(modelInfo, (*Handle)->GetActiveModelInfo());
}
catch (const std::exception&) {
return -1;
}
}
extern "C" ANSFR_API int SetANSRFAttributeInterval(ANSCENTER::ANSFacialRecognition** Handle, int frames) {
ANS_DBG("ANSFR","SetANSRFAttributeInterval: HandlePtr=%p, *Handle=%p, frames=%d",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), frames);
try {
if (!Handle || !*Handle) return -1;
return (*Handle)->UpdateAttributeInterval(frames) ? 1 : -1;
}
catch (const std::exception&) {
return -1;
}
}
extern "C" ANSFR_API int RunInferenceComplete_LV( extern "C" ANSFR_API int RunInferenceComplete_LV(
ANSCENTER::ANSFacialRecognition** Handle, ANSCENTER::ANSFacialRecognition** Handle,
cv::Mat** cvImage, cv::Mat** cvImage,
+1 -1
View File
@@ -22,7 +22,7 @@ namespace ANSCENTER
{1005, "ANSOCR"}, {1005, "ANSOCR"},
{1006, "ANSALPR"}, {1006, "ANSALPR"},
{1007, "ANSCV"}, {1007, "ANSCV"},
{1008, "ANSSRT"} {1008, "ANSTS"}
}; };
ansllmLicenceValid = false; ansllmLicenceValid = false;
for (const auto& [productId, productName] : licenseChecks) { for (const auto& [productId, productName] : licenseChecks) {
+55
View File
@@ -20,6 +20,7 @@
#include <chrono> #include <chrono>
#include "ANSMOT.h" #include "ANSMOT.h"
#include "onnxruntime_cxx_api.h" #include "onnxruntime_cxx_api.h"
#include "ANSModelSource.h"
#define USEONNXOV #define USEONNXOV
#define RETAINDETECTEDFRAMES 80 #define RETAINDETECTEDFRAMES 80
@@ -754,6 +755,11 @@ namespace ANSCENTER
virtual void AddEmbedding(const std::string& className, const std::vector<float>& embedding) =0; virtual void AddEmbedding(const std::string& className, const std::vector<float>& embedding) =0;
virtual bool Destroy()=0; virtual bool Destroy()=0;
virtual void SetMaxSlotsPerGpu(int n) {} // override in TRT-based subclasses virtual void SetMaxSlotsPerGpu(int n) {} // override in TRT-based subclasses
// True once Initialize() fully succeeded (license + model + engine).
// Callers that compute embeddings (enrollment) must check this first:
// Feature() returns an empty vector when uninitialized, which is
// indistinguishable from a bad face crop at the call site.
bool IsInitialized() const { return _isInitialized; }
~ANSFRBase(); ~ANSFRBase();
// Utility functions // Utility functions
@@ -792,6 +798,55 @@ namespace ANSCENTER
void CheckLicense(); void CheckLicense();
void LoadClassesFromString(); void LoadClassesFromString();
void LoadClassesFromFile(); void LoadClassesFromFile();
// ────────────────────────────────────────────────────────────────
// A model that arrived as an .ansmodel package.
//
// A package replaces the whole inflated folder: the model, the
// classes and the geometry model_config.json used to carry all
// come out of it, and nothing is written to disk. LoadModelPackage
// fills these in; the engine then builds its session from
// _packageModel instead of from _modelFilePath.
//
// The bytes stay for as long as the engine object does, because
// the ONNX handler holds a view of them and rebuilds its session
// from that view when a GPU driver fault forces a recreate.
// WipeModelPackage() clears them on teardown.
// ────────────────────────────────────────────────────────────────
bool _fromPackage{ false };
std::vector<unsigned char> _packageModel;
std::string _packageTask; // detection, classification, …
std::string _packageFamily; // yolo | yolo10 | rfdetr
std::string _packageMarker; // what the zip would have carried
std::string _packageName; // what logs call this model
int _packageInputWidth{ 0 }; // 0: the package does not say
int _packageInputHeight{ 0 };
// Where this engine's session should read its model from: the
// package's bytes when one was opened, the file otherwise. Engines
// keep their own _modelFilePath, so it is passed in.
ModelSource CurrentModelSource(const std::string& filePath) const {
if (_fromPackage && !_packageModel.empty())
return ModelSource::Memory(_packageModel.data(),
_packageModel.size(), _packageName);
return ModelSource::File(filePath);
}
// Engines reset the input geometry to their own default after the
// base class has loaded, which would throw away a package's own
// (a classification model is 224, not 640). This puts it back.
void ApplyPackageInputSize() {
if (_packageInputWidth > 0) _modelConfig.inpWidth = _packageInputWidth;
if (_packageInputHeight > 0) _modelConfig.inpHeight = _packageInputHeight;
}
static bool IsModelPackagePath(const std::string& path);
// Opens one, decrypts the model into _packageModel, and fills
// _classes and _modelConfig's geometry from the manifest. Returns
// false with the reason logged.
bool LoadModelPackage(const std::string& path);
void WipeModelPackage();
std::recursive_mutex _mutex; std::recursive_mutex _mutex;
// Guard: true while Initialize/LoadModel is in progress on any thread. // Guard: true while Initialize/LoadModel is in progress on any thread.
// Inference callers can check this to fail-fast instead of blocking. // Inference callers can check this to fail-fast instead of blocking.
+76
View File
@@ -0,0 +1,76 @@
#ifndef ANSMODELSOURCE_H
#define ANSMODELSOURCE_H
#pragma once
// ANSModelSource — where a model's bytes come from: a file, or memory.
//
// Every engine here built its session from a file path, which is why loading
// an encrypted model package means inflating it to a directory first: the
// unprotected model lands on disk, is optimised, and is zipped back into a
// cache. Handing the runtime a buffer instead removes both the round trip and
// the plaintext on disk, and it is the one change the new .ansmodel package
// format cannot work without.
//
// A model in memory still needs two things a path gave for free. `name` is
// what logs call it, since there is no file name to fall back on. And
// ONNX Runtime copies what it needs out of the buffer while the session is
// being built, so the caller may wipe its plaintext the moment MakeSession
// returns — which is exactly what a package reader should do.
#include <string>
#include <onnxruntime_cxx_api.h>
namespace ANSCENTER {
struct ModelSource {
std::string path; // file on disk; empty for a model already in memory
std::string name; // what to call it in logs
const void* data = nullptr;
size_t size = 0;
bool inMemory() const { return data != nullptr && size > 0; }
// Named factories, never implicit conversions: an engine that offers
// both Init(std::string) and Init(ModelSource) must not become
// ambiguous for a caller passing a plain string literal.
static ModelSource File(std::string p) {
ModelSource s;
s.name = p;
s.path = std::move(p);
return s;
}
static ModelSource Memory(const void* d, size_t n, std::string name) {
ModelSource s;
s.data = d;
s.size = n;
s.name = std::move(name);
return s;
}
};
// What to call this model in a log line.
inline std::string ModelSourceName(const ModelSource& src) {
if (!src.name.empty()) return src.name;
if (!src.path.empty()) return src.path;
return "<memory>";
}
// Build a session from wherever the bytes are. One place, so the Windows
// wide-path conversion and the from-memory call are not repeated in every
// engine — they were already duplicated eleven times.
inline Ort::Session MakeSession(Ort::Env& env, const ModelSource& src,
const Ort::SessionOptions& options) {
if (src.inMemory())
return Ort::Session(env, src.data, src.size, options);
#ifdef _WIN32
// ORT's Windows API takes a wide (ORTCHAR_T = wchar_t) path.
const std::wstring wide(src.path.begin(), src.path.end());
return Ort::Session(env, wide.c_str(), options);
#else
return Ort::Session(env, src.path.c_str(), options);
#endif
}
} // namespace ANSCENTER
#endif // ANSMODELSOURCE_H
+105
View File
@@ -1,9 +1,11 @@
#pragma once #pragma once
#include <set> #include <set>
#include <cstring>
#include <cmath> #include <cmath>
#include <json.hpp> #include <json.hpp>
#include "ANSODEngine.h" #include "ANSODEngine.h"
#include "ANSLicense.h" // ANS_DBG macro #include "ANSLicense.h" // ANS_DBG macro
#include "ANSModelOpen.h"
#include "OpenVINODeviceConfig.h" #include "OpenVINODeviceConfig.h"
#include "ANSYOLOOD.h" #include "ANSYOLOOD.h"
#include "ANSTENSORRTOD.h" #include "ANSTENSORRTOD.h"
@@ -61,6 +63,93 @@ namespace ANSCENTER
this->_logger.LogFatal("ANSODBase::CheckLicense. Error:", e.what(), __FILE__, __LINE__); this->_logger.LogFatal("ANSODBase::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
} }
} }
// ────────────────────────────────────────────────────────────────────────
// Model packages (.ansmodel)
//
// A package carries what the zip's loose files carried — the model, the
// class names, the task and the input geometry — sealed to a key only
// ANSCORE, ANSMINDCORE and the engines hold. Opening one fills in the
// same members the inflated folder used to fill in, with one difference
// that is the point of the whole format: the model stays in memory.
// ────────────────────────────────────────────────────────────────────────
bool ANSODBase::IsModelPackagePath(const std::string& path) {
return ANSCENTER::modelpkg::IsPackagePath(path);
}
void ANSODBase::WipeModelPackage() {
if (!_packageModel.empty()) {
std::memset(_packageModel.data(), 0, _packageModel.size());
_packageModel.clear();
_packageModel.shrink_to_fit();
}
_fromPackage = false;
_packageInputWidth = 0;
_packageInputHeight = 0;
}
bool ANSODBase::LoadModelPackage(const std::string& path) {
try {
WipeModelPackage();
ANSCENTER::modelpkg::OpenedModel opened;
std::string err;
if (!ANSCENTER::modelpkg::OpenModelPackage(path, opened, err)) {
this->_logger.LogError("ANSODBase::LoadModelPackage", err, __FILE__, __LINE__);
return false;
}
if (!opened.warning.empty()) {
// An expired release certificate warns and loads, by design:
// a model that worked yesterday keeps working on a box that
// has been offline for a year.
this->_logger.LogWarn("ANSODBase::LoadModelPackage", opened.warning, __FILE__, __LINE__);
}
_packageModel = std::move(opened.model);
if (_packageModel.empty()) {
this->_logger.LogError("ANSODBase::LoadModelPackage",
"The package carries no model", __FILE__, __LINE__);
return false;
}
_fromPackage = true;
_packageTask = opened.task;
_packageFamily = opened.family;
_packageMarker = opened.legacyMarker;
_packageName = opened.name;
// The classes come out in order, so there is nothing to parse and
// no classes.names to look for.
if (!opened.classes.empty()) _classes = opened.classes;
_packageInputWidth = opened.inputWidth;
_packageInputHeight = opened.inputHeight;
ApplyPackageInputSize();
// The manifest says in a word what the zip said by the presence of
// a marker file.
if (opened.task == "classification") _modelConfig.detectionType = ANSCENTER::DetectionType::CLASSIFICATION;
else if (opened.task == "segmentation") _modelConfig.detectionType = ANSCENTER::DetectionType::SEGMENTATION;
else if (opened.task == "obb") _modelConfig.detectionType = ANSCENTER::DetectionType::OBB;
else if (opened.task == "pose") _modelConfig.detectionType = ANSCENTER::DetectionType::KEYPOINT;
else _modelConfig.detectionType = ANSCENTER::DetectionType::DETECTION;
// There is no folder and no config file; every engine below tests
// for these, and must find them empty rather than stale.
_modelFolder.clear();
_modelConfigFile.clear();
_classFilePath.clear();
this->_logger.LogDebug("ANSODBase::LoadModelPackage",
"Opened " + path + " (" + opened.task + "/" + opened.family + ", "
+ std::to_string(_classes.size()) + " classes, "
+ std::to_string(_packageModel.size()) + " bytes)", __FILE__, __LINE__);
// Auto-enable tracker + stabilization by default (BYTETRACK)
SetTracker(TrackerType::BYTETRACK, true);
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSODBase::LoadModelPackage", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSODBase::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword) bool ANSODBase::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)
{ {
try { try {
@@ -69,6 +158,7 @@ namespace ANSCENTER
_modelConfigFile = ""; _modelConfigFile = "";
_modelFolder.clear(); _modelFolder.clear();
_modelConfigFile.clear(); _modelConfigFile.clear();
WipeModelPackage(); // whatever was loaded before, package or not
// 0. Check if the modelZipFilePath exist? // 0. Check if the modelZipFilePath exist?
if (!FileExist(modelZipFilePath)) { if (!FileExist(modelZipFilePath)) {
if (modelZipFilePath.empty()) {// Support motion detector that does not need model file if (modelZipFilePath.empty()) {// Support motion detector that does not need model file
@@ -82,6 +172,10 @@ namespace ANSCENTER
return false; return false;
} }
} }
// 0b. A model package replaces the zip entirely: it is opened in
// memory, and no password, no folder and no loose files are involved.
if (IsModelPackagePath(modelZipFilePath))
return LoadModelPackage(modelZipFilePath);
// 1. Unzip model zip file to a special location with folder name as model file (and version) // 1. Unzip model zip file to a special location with folder name as model file (and version)
std::string outputFolder; std::string outputFolder;
std::vector<std::string> passwordArray; std::vector<std::string> passwordArray;
@@ -126,6 +220,7 @@ namespace ANSCENTER
_modelFolder.clear(); _modelFolder.clear();
_modelConfigFile.clear(); _modelConfigFile.clear();
_modelConfig = modelConfig; _modelConfig = modelConfig;
WipeModelPackage(); // whatever was loaded before, package or not
CheckLicense(); CheckLicense();
if (!_licenseValid) { if (!_licenseValid) {
this->_logger.LogError("ANSODBase::Initialize", "Invalid License", __FILE__, __LINE__); this->_logger.LogError("ANSODBase::Initialize", "Invalid License", __FILE__, __LINE__);
@@ -145,6 +240,12 @@ namespace ANSCENTER
} }
} }
// 0b. A model package replaces the zip entirely; see LoadModel.
if (IsModelPackagePath(modelZipFilePath)) {
if (!LoadModelPackage(modelZipFilePath)) return false;
_isInitialized = true;
return true;
}
// 1. Unzip model zip file to a special location with folder name as model file (and version) // 1. Unzip model zip file to a special location with folder name as model file (and version)
std::string outputFolder; std::string outputFolder;
std::vector<std::string> passwordArray; std::vector<std::string> passwordArray;
@@ -952,6 +1053,10 @@ namespace ANSCENTER
ANSODBase::~ANSODBase() { ANSODBase::~ANSODBase() {
try { try {
// A decrypted model is what the package format exists to protect.
// This runs after the derived destructor has dropped the session,
// so nothing is left holding a view of these bytes.
WipeModelPackage();
// Per-camera trackers are released by CameraData::clear() // Per-camera trackers are released by CameraData::clear()
if (!_cameras.empty()) if (!_cameras.empty())
{ {
+7 -7
View File
@@ -55,6 +55,11 @@ namespace ANSCENTER
return std::accumulate(vector.begin(), vector.end(), 1LL, std::multiplies<int64_t>()); return std::accumulate(vector.begin(), vector.end(), 1LL, std::multiplies<int64_t>());
} }
bool ANSONNXCL::Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU) bool ANSONNXCL::Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU)
{
return Init(ModelSource::File(modelPath), targetInputShape, useGPU);
}
bool ANSONNXCL::Init(const ModelSource& model, const cv::Size& targetInputShape, bool useGPU)
{ {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
try { try {
@@ -198,12 +203,7 @@ namespace ANSCENTER
} }
// ── Load model ────────────────────────────────────────────────────── // ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32 session_ = MakeSession(env_, model, sessionOptions_);
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session_ = Ort::Session(env_, w_modelPath.c_str(), sessionOptions_);
#else
session_ = Ort::Session(env_, modelPath.c_str(), sessionOptions_);
#endif
Ort::AllocatorWithDefaultOptions allocator; Ort::AllocatorWithDefaultOptions allocator;
@@ -300,7 +300,7 @@ namespace ANSCENTER
std::cout << "Warning: No class names loaded. Predictions will use numeric IDs." << std::endl; std::cout << "Warning: No class names loaded. Predictions will use numeric IDs." << std::endl;
} }
std::cout << "[ANSONNXCL] Initialized successfully. Model: " << modelPath << std::endl; std::cout << "[ANSONNXCL] Initialized successfully. Model: " << ModelSourceName(model) << std::endl;
// ── Warmup ────────────────────────────────────────────────────────── // ── Warmup ──────────────────────────────────────────────────────────
DEBUG_PRINT("Starting model warmup..."); DEBUG_PRINT("Starting model warmup...");
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXCL_H #define ANSONNXCL_H
#pragma once #pragma once
#include "ANSEngineCommon.h" #include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h" #include "engine.h"
#include <memory> #include <memory>
@@ -65,6 +66,8 @@ namespace ANSCENTER {
const std::string& strategy = "resize"); const std::string& strategy = "resize");
void warmupModel(); void warmupModel();
bool Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU = true); bool Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU = true);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, const cv::Size& targetInputShape, bool useGPU = true);
bool preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape); bool preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() (the // outputTensors is non-const because GetTensorMutableData() (the
// ORT API that doesn't hang on AMD DirectML) requires a non-const // ORT API that doesn't hang on AMD DirectML) requires a non-const
+30 -6
View File
@@ -686,6 +686,11 @@ namespace ANSCENTER {
} }
} }
bool ANSONNXOBB::Init(const std::string& modelPath, bool useGPU, int deviceId) bool ANSONNXOBB::Init(const std::string& modelPath, bool useGPU, int deviceId)
{
return Init(ModelSource::File(modelPath), useGPU, deviceId);
}
bool ANSONNXOBB::Init(const ModelSource& model, bool useGPU, int deviceId)
{ {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
try { try {
@@ -834,12 +839,7 @@ namespace ANSCENTER {
} }
// ── Load model ────────────────────────────────────────────────────── // ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32 session = MakeSession(env, model, sessionOptions);
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
Ort::AllocatorWithDefaultOptions allocator; Ort::AllocatorWithDefaultOptions allocator;
@@ -1394,6 +1394,19 @@ namespace ANSCENTER {
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = (modelConfig.precisionType == PrecisionType::FP16); _fp16 = (modelConfig.precisionType == PrecisionType::FP16);
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
labelMap.clear();
if (!_classes.empty()) labelMap = VectorToCommaSeparatedString(_classes);
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
if (FileExist(_modelConfigFile)) { if (FileExist(_modelConfigFile)) {
ModelType modelType; ModelType modelType;
std::vector<int> inputShape; std::vector<int> inputShape;
@@ -1454,6 +1467,17 @@ namespace ANSCENTER {
// if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true; // if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true;
_fp16 = true; // Load Model from Here _fp16 = true; // Load Model from Here
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
// 0. Check if the configuration file exist // 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) { if (FileExist(_modelConfigFile)) {
ModelType modelType; ModelType modelType;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXOBB_H #define ANSONNXOBB_H
#pragma once #pragma once
#include "ANSEngineCommon.h" #include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h" #include "engine.h"
namespace ANSCENTER { namespace ANSCENTER {
@@ -73,6 +74,8 @@ namespace ANSCENTER {
void warmupModel(); void warmupModel();
bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0); bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0);
cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape); cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() requires // outputTensors is non-const because GetTensorMutableData() requires
// a non-const Ort::Value receiver — see ANSONNXCL.h for full note. // a non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+30 -6
View File
@@ -270,6 +270,11 @@ namespace ANSCENTER {
} }
bool ANSONNXPOSE::Init(const std::string& modelPath, bool useGPU, int deviceId) bool ANSONNXPOSE::Init(const std::string& modelPath, bool useGPU, int deviceId)
{
return Init(ModelSource::File(modelPath), useGPU, deviceId);
}
bool ANSONNXPOSE::Init(const ModelSource& model, bool useGPU, int deviceId)
{ {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
try { try {
@@ -418,12 +423,7 @@ namespace ANSCENTER {
} }
// ── Load model ────────────────────────────────────────────────────── // ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32 session = MakeSession(env, model, sessionOptions);
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
Ort::AllocatorWithDefaultOptions allocator; Ort::AllocatorWithDefaultOptions allocator;
@@ -1282,6 +1282,19 @@ namespace ANSCENTER {
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = (modelConfig.precisionType == PrecisionType::FP16); _fp16 = (modelConfig.precisionType == PrecisionType::FP16);
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
labelMap.clear();
if (!_classes.empty()) labelMap = VectorToCommaSeparatedString(_classes);
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
if (FileExist(_modelConfigFile)) { if (FileExist(_modelConfigFile)) {
ModelType modelType; ModelType modelType;
std::vector<int> inputShape; std::vector<int> inputShape;
@@ -1343,6 +1356,17 @@ namespace ANSCENTER {
// if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true; // if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true;
_fp16 = true; // Load Model from Here _fp16 = true; // Load Model from Here
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
// 0. Check if the configuration file exist // 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) { if (FileExist(_modelConfigFile)) {
ModelType modelType; ModelType modelType;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXPOSE_H #define ANSONNXPOSE_H
#pragma once #pragma once
#include "ANSEngineCommon.h" #include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h" #include "engine.h"
namespace ANSCENTER { namespace ANSCENTER {
@@ -40,6 +41,8 @@ namespace ANSCENTER {
float kptThreshold = 0.5); float kptThreshold = 0.5);
void warmupModel(); void warmupModel();
bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0); bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0);
cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape); cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() requires // outputTensors is non-const because GetTensorMutableData() requires
// a non-const Ort::Value receiver — see ANSONNXCL.h for full note. // a non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+30 -6
View File
@@ -267,6 +267,11 @@ namespace ANSCENTER {
} }
} }
bool ANSONNXSEG::Init(const std::string& modelPath, bool useGPU, int deviceId) bool ANSONNXSEG::Init(const std::string& modelPath, bool useGPU, int deviceId)
{
return Init(ModelSource::File(modelPath), useGPU, deviceId);
}
bool ANSONNXSEG::Init(const ModelSource& model, bool useGPU, int deviceId)
{ {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
try { try {
@@ -415,12 +420,7 @@ namespace ANSCENTER {
} }
// ── Load model ────────────────────────────────────────────────────── // ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32 session = MakeSession(env, model, sessionOptions);
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
numInputNodes = session.GetInputCount(); numInputNodes = session.GetInputCount();
numOutputNodes = session.GetOutputCount(); numOutputNodes = session.GetOutputCount();
@@ -1172,6 +1172,19 @@ namespace ANSCENTER {
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = (modelConfig.precisionType == PrecisionType::FP16); _fp16 = (modelConfig.precisionType == PrecisionType::FP16);
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
labelMap.clear();
if (!_classes.empty()) labelMap = VectorToCommaSeparatedString(_classes);
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
if (FileExist(_modelConfigFile)) { if (FileExist(_modelConfigFile)) {
ModelType modelType; ModelType modelType;
std::vector<int> inputShape; std::vector<int> inputShape;
@@ -1233,6 +1246,17 @@ namespace ANSCENTER {
// if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true; // if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true;
_fp16 = true; // Load Model from Here _fp16 = true; // Load Model from Here
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
// 0. Check if the configuration file exist // 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) { if (FileExist(_modelConfigFile)) {
ModelType modelType; ModelType modelType;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXSEG_H #define ANSONNXSEG_H
#pragma once #pragma once
#include "ANSEngineCommon.h" #include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h" #include "engine.h"
namespace ANSCENTER { namespace ANSCENTER {
@@ -50,6 +51,8 @@ namespace ANSCENTER {
void warmupModel(); void warmupModel();
bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0); bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0);
cv::Mat preprocess(const cv::Mat& image,float*& blobPtr,std::vector<int64_t>& inputTensorShape); cv::Mat preprocess(const cv::Mat& image,float*& blobPtr,std::vector<int64_t>& inputTensorShape);
// outputs is non-const because GetTensorMutableData() requires a // outputs is non-const because GetTensorMutableData() requires a
// non-const Ort::Value receiver — see ANSONNXCL.h for full note. // non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+97 -24
View File
@@ -41,6 +41,37 @@ namespace ANSCENTER {
} }
} }
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
const std::string& logName, unsigned int _num_threads)
: BasicOrtHandler(modelData, modelSize, logName, _num_threads)
{
if (input_node_dims.size() >= 4) {
int h = static_cast<int>(input_node_dims[2]);
int w = static_cast<int>(input_node_dims[3]);
isDynamicInputShape = (h == -1 || w == -1);
inputImageShape = isDynamicInputShape ? cv::Size(640, 640) : cv::Size(w, h);
}
else {
inputImageShape = cv::Size(640, 640);
}
}
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
const std::string& logName, EngineType engineType,
unsigned int _num_threads)
: BasicOrtHandler(modelData, modelSize, logName, engineType, _num_threads)
{
if (input_node_dims.size() >= 4) {
int h = static_cast<int>(input_node_dims[2]);
int w = static_cast<int>(input_node_dims[3]);
isDynamicInputShape = (h == -1 || w == -1);
inputImageShape = isDynamicInputShape ? cv::Size(640, 640) : cv::Size(w, h);
}
else {
inputImageShape = cv::Size(640, 640);
}
}
// ------------------------------------------------------------------ // ------------------------------------------------------------------
// letterBox — Ultralytics-compatible LetterBox transform // letterBox — Ultralytics-compatible LetterBox transform
// ------------------------------------------------------------------ // ------------------------------------------------------------------
@@ -1675,6 +1706,20 @@ namespace ANSCENTER {
bool ANSONNXYOLO::InitOrtEngine() { bool ANSONNXYOLO::InitOrtEngine() {
try { try {
// A model that came out of an .ansmodel package has no file
// behind it; the handler reads the bytes ANSODBase decrypted.
// Those bytes outlive the handler, which is what lets the DML
// recovery path below rebuild the session from them.
if (_fromPackage) {
if (_packageModel.empty()) {
_logger.LogError("ANSONNXYOLO::InitOrtEngine",
"The package carries no model", __FILE__, __LINE__);
return false;
}
m_ortEngine = std::make_shared<class ONNXYOLO>(
_packageModel.data(), _packageModel.size(), _packageName);
return true;
}
if (!FileExist(_modelFilePath)) { if (!FileExist(_modelFilePath)) {
_logger.LogError("ANSONNXYOLO::InitOrtEngine", _logger.LogError("ANSONNXYOLO::InitOrtEngine",
"Model file does not exist: " + _modelFilePath, __FILE__, __LINE__); "Model file does not exist: " + _modelFilePath, __FILE__, __LINE__);
@@ -1691,6 +1736,16 @@ namespace ANSCENTER {
bool ANSONNXYOLO::InitOrtEngine(ANSCENTER::EngineType engineType) { bool ANSONNXYOLO::InitOrtEngine(ANSCENTER::EngineType engineType) {
try { try {
if (_fromPackage) {
if (_packageModel.empty()) {
_logger.LogError("ANSONNXYOLO::InitOrtEngine",
"The package carries no model", __FILE__, __LINE__);
return false;
}
m_ortEngine = std::make_shared<class ONNXYOLO>(
_packageModel.data(), _packageModel.size(), _packageName, engineType);
return true;
}
if (!FileExist(_modelFilePath)) { if (!FileExist(_modelFilePath)) {
_logger.LogError("ANSONNXYOLO::InitOrtEngine", _logger.LogError("ANSONNXYOLO::InitOrtEngine",
"Model file does not exist: " + _modelFilePath, __FILE__, __LINE__); "Model file does not exist: " + _modelFilePath, __FILE__, __LINE__);
@@ -1846,21 +1901,30 @@ namespace ANSCENTER {
NUM_KPS = _modelConfig.numKPS; NUM_KPS = _modelConfig.numKPS;
KPS_THRESHOLD = _modelConfig.kpsThreshold; KPS_THRESHOLD = _modelConfig.kpsThreshold;
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx"); // A package has already supplied the classes and the geometry;
if (FileExist(_modelConfigFile)) { // there is no folder to look in.
ModelType modelType; if (!_fromPackage) {
std::vector<int> inputShape; _modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape); if (FileExist(_modelConfigFile)) {
if (inputShape.size() == 2) { ModelType modelType;
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0]; std::vector<int> inputShape;
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1]; _classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
}
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
} }
} }
else { else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names"); // The defaults above have just overwritten what the package
std::ifstream isValid(_classFilePath); // said its input size was; put it back.
if (!isValid) LoadClassesFromString(); ApplyPackageInputSize();
else LoadClassesFromFile();
} }
labelMap.clear(); labelMap.clear();
@@ -1920,21 +1984,30 @@ namespace ANSCENTER {
NUM_KPS = _modelConfig.numKPS; NUM_KPS = _modelConfig.numKPS;
KPS_THRESHOLD = _modelConfig.kpsThreshold; KPS_THRESHOLD = _modelConfig.kpsThreshold;
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx"); // A package has already supplied the classes and the geometry;
if (FileExist(_modelConfigFile)) { // there is no folder to look in.
ModelType modelType; if (!_fromPackage) {
std::vector<int> inputShape; _modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape); if (FileExist(_modelConfigFile)) {
if (inputShape.size() == 2) { ModelType modelType;
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0]; std::vector<int> inputShape;
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1]; _classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
}
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
} }
} }
else { else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names"); // The defaults above have just overwritten what the package
std::ifstream isValid(_classFilePath); // said its input size was; put it back.
if (!isValid) LoadClassesFromString(); ApplyPackageInputSize();
else LoadClassesFromFile();
} }
if (this->_loadEngineOnCreation) { if (this->_loadEngineOnCreation) {
+10
View File
@@ -33,6 +33,16 @@ namespace ANSCENTER {
explicit ONNXYOLO(const std::string& _onnx_path, explicit ONNXYOLO(const std::string& _onnx_path,
EngineType engineType, EngineType engineType,
unsigned int _num_threads = 1); unsigned int _num_threads = 1);
// The decrypted contents of an .ansmodel package: the same model,
// with no file behind it. `logName` is what logs call it.
explicit ONNXYOLO(const void* modelData, size_t modelSize,
const std::string& logName,
unsigned int _num_threads = 1);
explicit ONNXYOLO(const void* modelData, size_t modelSize,
const std::string& logName,
EngineType engineType,
unsigned int _num_threads = 1);
~ONNXYOLO() override = default; ~ONNXYOLO() override = default;
/// Run inference on a single image. /// Run inference on a single image.
+84 -29
View File
@@ -11,12 +11,37 @@
namespace ANSCENTER { namespace ANSCENTER {
// ====================================================================
// Sealed packages (.ansmodel)
//
// TensorRT still writes a plan to disk and still reads it back on the
// next run — that is what makes the second start fast, and a plan is a
// built artefact that carries no model. What must never be written is
// the ONNX itself, so the builder parses the decrypted bytes from
// memory and the path below names nothing that is ever created: it
// only tells the plan where to live and what to be called. The folder
// is the one the zip flow used, so a package converted from a zip
// reuses the plan that zip already built.
// ====================================================================
std::string ANSRTYOLO::PackagePlanBasePath(const std::string& packagePath) {
const std::string folder = GenerateOutputFolder(
packagePath, GetFileNameWithoutExtension(packagePath), false);
return CreateFilePath(folder, "train_last.onnx");
}
// ==================================================================== // ====================================================================
// ANSODBase interface — OptimizeModel // ANSODBase interface — OptimizeModel
// ==================================================================== // ====================================================================
bool ANSRTYOLO::OptimizeModel(bool fp16, std::string& optimizedModelFolder) { bool ANSRTYOLO::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!ANSODBase::OptimizeModel(fp16, optimizedModelFolder)) return false; if (!ANSODBase::OptimizeModel(fp16, optimizedModelFolder)) return false;
if (_fromPackage) {
// The plan is built from the decrypted bytes when the engine
// loads; there is no ONNX on disk to pre-optimise. Report the
// folder the plan is cached in so the caller sees a real path.
optimizedModelFolder = GetParentFolder(_modelFilePath);
return true;
}
if (!FileExist(_modelFilePath)) { if (!FileExist(_modelFilePath)) {
_logger.LogFatal("ANSRTYOLO::OptimizeModel", "Raw model file path does not exist", __FILE__, __LINE__); _logger.LogFatal("ANSRTYOLO::OptimizeModel", "Raw model file path does not exist", __FILE__, __LINE__);
return false; return false;
@@ -96,21 +121,33 @@ namespace ANSCENTER {
m_options.engineFileDir = _modelFolder; m_options.engineFileDir = _modelFolder;
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32); m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx"); if (_fromPackage) {
if (FileExist(_modelConfigFile)) { // The package already carries the classes and the geometry,
ModelType modelType; // and there is no folder to look in. All that is needed here
std::vector<int> inputShape; // is where the built plan lives.
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape); _modelFilePath = PackagePlanBasePath(modelZipFilePath);
if (inputShape.size() == 2) { m_options.engineFileDir = GetParentFolder(_modelFilePath);
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0]; // The defaults above have just overwritten the input size the
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1]; // package asked for; put it back.
} ApplyPackageInputSize();
} }
else { else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names"); _modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
std::ifstream isValid(_classFilePath); if (FileExist(_modelConfigFile)) {
if (!isValid) LoadClassesFromString(); ModelType modelType;
else LoadClassesFromFile(); std::vector<int> inputShape;
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
}
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
}
} }
if (this->_loadEngineOnCreation) { if (this->_loadEngineOnCreation) {
@@ -119,7 +156,9 @@ namespace ANSCENTER {
static_cast<int>(m_options.precision), m_options.maxBatchSize }; static_cast<int>(m_options.precision), m_options.maxBatchSize };
m_trtEngine = EnginePoolManager<float>::instance().acquire( m_trtEngine = EnginePoolManager<float>::instance().acquire(
m_poolKey, m_options, _modelFilePath, m_poolKey, m_options, _modelFilePath,
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu); SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu,
_fromPackage ? _packageModel.data() : nullptr,
_fromPackage ? _packageModel.size() : 0);
m_usingSharedPool = (m_trtEngine != nullptr); m_usingSharedPool = (m_trtEngine != nullptr);
} }
if (!m_trtEngine) { if (!m_trtEngine) {
@@ -221,7 +260,9 @@ namespace ANSCENTER {
static_cast<int>(m_options.precision), m_options.maxBatchSize }; static_cast<int>(m_options.precision), m_options.maxBatchSize };
m_trtEngine = EnginePoolManager<float>::instance().acquire( m_trtEngine = EnginePoolManager<float>::instance().acquire(
m_poolKey, m_options, _modelFilePath, m_poolKey, m_options, _modelFilePath,
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu); SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu,
_fromPackage ? _packageModel.data() : nullptr,
_fromPackage ? _packageModel.size() : 0);
m_usingSharedPool = (m_trtEngine != nullptr); m_usingSharedPool = (m_trtEngine != nullptr);
} }
if (!m_trtEngine) { if (!m_trtEngine) {
@@ -295,21 +336,33 @@ namespace ANSCENTER {
m_options.engineFileDir = _modelFolder; m_options.engineFileDir = _modelFolder;
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32); m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx"); if (_fromPackage) {
if (FileExist(_modelConfigFile)) { // The package already carries the classes and the geometry,
ModelType modelType; // and there is no folder to look in. All that is needed here
std::vector<int> inputShape; // is where the built plan lives.
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape); _modelFilePath = PackagePlanBasePath(modelZipFilePath);
if (inputShape.size() == 2) { m_options.engineFileDir = GetParentFolder(_modelFilePath);
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0]; // The defaults above have just overwritten the input size the
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1]; // package asked for; put it back.
} ApplyPackageInputSize();
} }
else { else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names"); _modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
std::ifstream isValid(_classFilePath); if (FileExist(_modelConfigFile)) {
if (!isValid) LoadClassesFromString(); ModelType modelType;
else LoadClassesFromFile(); std::vector<int> inputShape;
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
}
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
}
} }
labelMap.clear(); labelMap.clear();
@@ -322,7 +375,9 @@ namespace ANSCENTER {
static_cast<int>(m_options.precision), m_options.maxBatchSize }; static_cast<int>(m_options.precision), m_options.maxBatchSize };
m_trtEngine = EnginePoolManager<float>::instance().acquire( m_trtEngine = EnginePoolManager<float>::instance().acquire(
m_poolKey, m_options, _modelFilePath, m_poolKey, m_options, _modelFilePath,
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu); SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu,
_fromPackage ? _packageModel.data() : nullptr,
_fromPackage ? _packageModel.size() : 0);
m_usingSharedPool = (m_trtEngine != nullptr); m_usingSharedPool = (m_trtEngine != nullptr);
} }
if (!m_trtEngine) { if (!m_trtEngine) {
+5
View File
@@ -52,6 +52,11 @@ namespace ANSCENTER {
~ANSRTYOLO(); ~ANSRTYOLO();
private: private:
// Where a sealed package's TensorRT plan is cached, and what it is
// called. Nothing but the plan is ever written there — the model
// itself is parsed from memory.
static std::string PackagePlanBasePath(const std::string& packagePath);
std::string _modelFilePath; std::string _modelFilePath;
bool _modelLoadValid{ false }; bool _modelLoadValid{ false };
bool _fp16{ false }; bool _fp16{ false };
+122 -6
View File
@@ -10,6 +10,12 @@ namespace ANSCENTER {
if (!ANSODBase::OptimizeModel(fp16, optimizedModelFolder)) { if (!ANSODBase::OptimizeModel(fp16, optimizedModelFolder)) {
return false; return false;
} }
if (_fromPackage) {
// A package is already the optimised artefact, and there is no
// folder to point at because nothing was written out.
optimizedModelFolder.clear();
return true;
}
if (FileExist(_modelFilePath)) { if (FileExist(_modelFilePath)) {
optimizedModelFolder = GetParentFolder(_modelFilePath); optimizedModelFolder = GetParentFolder(_modelFilePath);
this->_logger.LogDebug("YOLO12OD::OptimizeModel", "This model is optimized. No need other optimization.", __FILE__, __LINE__); this->_logger.LogDebug("YOLO12OD::OptimizeModel", "This model is optimized. No need other optimization.", __FILE__, __LINE__);
@@ -34,6 +40,13 @@ namespace ANSCENTER {
_modelConfig.modelMNSThreshold = 0.5; _modelConfig.modelMNSThreshold = 0.5;
if (_modelConfig.modelConfThreshold < 0.2) if (_modelConfig.modelConfThreshold < 0.2)
_modelConfig.modelConfThreshold = 0.5; _modelConfig.modelConfThreshold = 0.5;
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
_isInitialized = loadModel(CurrentModelSource(_modelFilePath), true);
return _isInitialized;
}
// 0. Check if the configuration file exist // 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) { if (FileExist(_modelConfigFile)) {
ModelType modelType; ModelType modelType;
@@ -168,6 +181,15 @@ namespace ANSCENTER {
_modelConfig.modelMNSThreshold = 0.5; _modelConfig.modelMNSThreshold = 0.5;
if (_modelConfig.modelConfThreshold < 0.2) if (_modelConfig.modelConfThreshold < 0.2)
_modelConfig.modelConfThreshold = 0.5; _modelConfig.modelConfThreshold = 0.5;
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
labelMap.clear();
if (!_classes.empty()) labelMap = VectorToCommaSeparatedString(_classes);
_isInitialized = loadModel(CurrentModelSource(_modelFilePath), true);
return _isInitialized;
}
// 0. Check if the configuration file exist // 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) { if (FileExist(_modelConfigFile)) {
ModelType modelType; ModelType modelType;
@@ -278,6 +300,11 @@ namespace ANSCENTER {
} }
} }
bool YOLO12OD::loadModel(const std::string& modelPath, bool useGPU) bool YOLO12OD::loadModel(const std::string& modelPath, bool useGPU)
{
return loadModel(ModelSource::File(modelPath), useGPU);
}
bool YOLO12OD::loadModel(const ModelSource& model, bool useGPU)
{ {
try { try {
const auto& ep = ANSCENTER::EPLoader::Current(); const auto& ep = ANSCENTER::EPLoader::Current();
@@ -418,12 +445,7 @@ namespace ANSCENTER {
} }
// ── Load model ────────────────────────────────────────────────────── // ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32 session = MakeSession(env, model, sessionOptions);
std::wstring w_modelPath(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
Ort::AllocatorWithDefaultOptions allocator; Ort::AllocatorWithDefaultOptions allocator;
@@ -530,6 +552,30 @@ namespace ANSCENTER {
const float* rawOutput = outputTensors[0].GetTensorMutableData<float>(); const float* rawOutput = outputTensors[0].GetTensorMutableData<float>();
const std::vector<int64_t> outputShape = outputTensors[0].GetTensorTypeAndShapeInfo().GetShape(); const std::vector<int64_t> outputShape = outputTensors[0].GetTensorTypeAndShapeInfo().GetShape();
if (outputShape.size() < 3) {
return detections;
}
// WHICH HEAD THIS MODEL HAS. An export with end2end on has already
// done its own non-maximum suppression and answers [1, N, 6] —
// rows of (x1 y1 x2 y2 score class) — where an ordinary YOLO
// answers [1, 4+nc, anchors]. Its backend marker is still
// tensorrt.txt either way, because the marker names the runtime
// and not the head, so the tensor is the only thing that knows.
//
// Reading one as the other does not fail: it takes 300 as a
// feature count, decides the model has 296 classes, reads
// coordinates as class scores and returns boxes covering the
// frame. The trap in the other direction is a two-class export,
// whose [1, 4+nc, N] is [1, 6, 8400] — a 6 in the shape and not a
// decoded head — so the 6 must be the LAST axis with a detection
// count before it: hundreds, never thousands of anchors.
if (outputShape[0] == 1 && outputShape[2] == 6 &&
outputShape[1] > 0 && outputShape[1] <= 1000) {
return postprocessEndToEnd(originalImageSize, resizedImageShape,
outputTensors, confThreshold);
}
// Determine the number of features and detections // Determine the number of features and detections
const size_t num_features = outputShape[1]; const size_t num_features = outputShape[1];
const size_t num_detections = outputShape[2]; const size_t num_detections = outputShape[2];
@@ -653,6 +699,76 @@ namespace ANSCENTER {
} }
} }
// An end-to-end head has already chosen its detections: the rows are boxes
// in model-pixel corners with a score and a class, and the suppression
// happened inside the graph. None is run here — running one anyway would
// hide a broken export instead of showing it.
std::vector<Object> YOLO12OD::postprocessEndToEnd(const cv::Size& originalImageSize,
const cv::Size& resizedImageShape,
std::vector<Ort::Value>& outputTensors,
float confThreshold)
{
std::vector<Object> detections;
if (outputTensors.empty()) return detections;
// GetTensorMutableData (not GetTensorData) on DML — the const variant
// hangs on AMD after ~8 calls. Read-only despite the name.
const float* rawOutput = outputTensors[0].GetTensorMutableData<float>();
const std::vector<int64_t> outputShape =
outputTensors[0].GetTensorTypeAndShapeInfo().GetShape();
if (outputShape.size() < 3) return detections;
const int numDets = static_cast<int>(outputShape[1]);
const int numFeat = static_cast<int>(outputShape[2]);
const int classNameSize = static_cast<int>(_classes.size());
detections.reserve(static_cast<size_t>(numDets));
for (int i = 0; i < numDets; ++i) {
const float* det = rawOutput + static_cast<size_t>(i) * numFeat;
const float conf = det[4];
// The rows are not obliged to be sorted, so every one is read.
if (conf <= confThreshold) continue;
// Corners in model pixels, handed to the same scaler the grid path
// uses: it undoes this engine's own letterbox and clips to the
// frame, so both heads agree about where a box is.
const float left = det[0];
const float top = det[1];
const float width = det[2] - det[0];
const float height = det[3] - det[1];
if (width <= 0.0f || height <= 0.0f) continue;
// BoundingBox is integral and these corners are not, so the
// rounding is spelled out rather than left to a narrowing
// conversion that would quietly truncate every box by up to a
// pixel.
const BoundingBox modelBox(static_cast<int>(std::lround(left)),
static_cast<int>(std::lround(top)),
static_cast<int>(std::lround(width)),
static_cast<int>(std::lround(height)));
const BoundingBox scaled =
scaleCoords(resizedImageShape, modelBox, originalImageSize, true);
if (scaled.width <= 0 || scaled.height <= 0) continue;
Object detection;
detection.confidence = conf;
detection.classId = static_cast<int>(det[5]);
detection.box = cv::Rect(scaled.x, scaled.y, scaled.width, scaled.height);
if (classNameSize > 0) {
detection.className =
(detection.classId >= 0 && detection.classId < classNameSize)
? _classes[detection.classId]
: _classes[classNameSize - 1];
} else {
detection.className = "Unknown";
}
detection.polygon = ANSUtilityHelper::RectToNormalizedPolygon(
detection.box, m_imgWidth, m_imgHeight);
detections.push_back(std::move(detection));
}
return detections;
}
std::vector<Object> YOLO12OD::detect(const cv::Mat& image, float confThreshold, float iouThreshold) { std::vector<Object> YOLO12OD::detect(const cv::Mat& image, float confThreshold, float iouThreshold) {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
try { try {
+12
View File
@@ -2,6 +2,7 @@
#define ANSYOL12OOD_H #define ANSYOL12OOD_H
#pragma once #pragma once
#include "ANSEngineCommon.h" #include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include <onnxruntime_cxx_api.h> #include <onnxruntime_cxx_api.h>
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
@@ -46,6 +47,8 @@ namespace ANSCENTER {
float m_imgHeight = 0; float m_imgHeight = 0;
protected: protected:
bool loadModel(const std::string& modelPath, bool useGPU = true); bool loadModel(const std::string& modelPath, bool useGPU = true);
// Same load, from a model already in memory (an opened package).
bool loadModel(const ModelSource& model, bool useGPU = true);
std::vector<Object> detect(const cv::Mat& image, float confThreshold = 0.4f, float iouThreshold = 0.45f); std::vector<Object> detect(const cv::Mat& image, float confThreshold = 0.4f, float iouThreshold = 0.45f);
//cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape); //cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
cv::Mat preprocess(const cv::Mat& image, std::vector<float>& blob, std::vector<int64_t>& inputTensorShape); cv::Mat preprocess(const cv::Mat& image, std::vector<float>& blob, std::vector<int64_t>& inputTensorShape);
@@ -56,6 +59,15 @@ namespace ANSCENTER {
std::vector<Ort::Value>& outputTensors, std::vector<Ort::Value>& outputTensors,
float confThreshold, float iouThreshold); float confThreshold, float iouThreshold);
/// The [1, N, 6] head: rows of (x1 y1 x2 y2 score class) whose
/// non-maximum suppression happened inside the graph. postprocess()
/// hands the tensor here when it recognises that shape; reading it
/// as a [1, 4+nc, anchors] grid returns boxes covering the frame.
std::vector<Object> postprocessEndToEnd(const cv::Size& originalImageSize,
const cv::Size& resizedImageShape,
std::vector<Ort::Value>& outputTensors,
float confThreshold);
private: private:
template <typename T> template <typename T>
typename std::enable_if<std::is_arithmetic<T>::value, T>::type typename std::enable_if<std::is_arithmetic<T>::value, T>::type
+6 -2
View File
@@ -205,6 +205,11 @@ namespace ANSCENTER
} }
} }
bool YOLOOD::loadYoloModel(const std::string& modelPath, const bool& isGPU, const cv::Size& inputSize) bool YOLOOD::loadYoloModel(const std::string& modelPath, const bool& isGPU, const cv::Size& inputSize)
{
return loadYoloModel(ModelSource::File(modelPath), isGPU, inputSize);
}
bool YOLOOD::loadYoloModel(const ModelSource& model, const bool& isGPU, const cv::Size& inputSize)
{ {
try { try {
const auto& ep = ANSCENTER::EPLoader::Current(); const auto& ep = ANSCENTER::EPLoader::Current();
@@ -358,8 +363,7 @@ namespace ANSCENTER
} }
// ── Load model ────────────────────────────────────────────────────── // ── Load model ──────────────────────────────────────────────────────
std::wstring w_modelPath = String2WString(modelPath.c_str()); session = MakeSession(env, model, sessionOptions);
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
Ort::AllocatorWithDefaultOptions allocator; Ort::AllocatorWithDefaultOptions allocator;
// ── Input shape ───────────────────────────────────────────────────── // ── Input shape ─────────────────────────────────────────────────────
+3
View File
@@ -2,6 +2,7 @@
#define ANSYOLOOD_H #define ANSYOLOOD_H
#pragma once #pragma once
#include "ANSEngineCommon.h" #include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include <onnxruntime_cxx_api.h> #include <onnxruntime_cxx_api.h>
//#define USEOPENCVDNN //#define USEOPENCVDNN
namespace ANSCENTER { namespace ANSCENTER {
@@ -38,6 +39,8 @@ namespace ANSCENTER {
Ort::SessionOptions sessionOptions{ nullptr }; Ort::SessionOptions sessionOptions{ nullptr };
Ort::Session session{ nullptr }; Ort::Session session{ nullptr };
bool loadYoloModel(const std::string& modelPath,const bool& isGPU = true,const cv::Size& inputSize = cv::Size(640, 640)); bool loadYoloModel(const std::string& modelPath,const bool& isGPU = true,const cv::Size& inputSize = cv::Size(640, 640));
// Same load, from a model already in memory (an opened package).
bool loadYoloModel(const ModelSource& model, const bool& isGPU = true, const cv::Size& inputSize = cv::Size(640, 640));
std::vector<Object> detect(cv::Mat& image, const float& confThreshold, const float& iouThreshold); std::vector<Object> detect(cv::Mat& image, const float& confThreshold, const float& iouThreshold);
void preprocessing(const cv::Mat& image,std::vector<float>& blob,std::vector<int64_t>& inputTensorShape); void preprocessing(const cv::Mat& image,std::vector<float>& blob,std::vector<int64_t>& inputTensorShape);
//cv::Mat preprocessv11(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape); //cv::Mat preprocessv11(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
+3
View File
@@ -113,6 +113,9 @@ target_include_directories(ANSODEngine PRIVATE
target_link_libraries(ANSODEngine target_link_libraries(ANSODEngine
PUBLIC ANSLibsLoader PUBLIC ANSLibsLoader
# Reading .ansmodel packages. Header-only apart from the vendored
# curves, and free of CUDA/OpenCV, so it costs this target nothing.
PRIVATE ANSModelPackage
PUBLIC ANSLicensingSystem # PUBLIC: FileExist/SPDLogger symbols must be re-exported PUBLIC ANSLicensingSystem # PUBLIC: FileExist/SPDLogger symbols must be re-exported
PUBLIC opencv PUBLIC opencv
PRIVATE ANSMOT PRIVATE ANSMOT
+13 -2
View File
@@ -8,6 +8,7 @@
#include <climits> // INT_MIN #include <climits> // INT_MIN
#include "ANSLicense.h" // ANS_DBG macro for DebugView #include "ANSLicense.h" // ANS_DBG macro for DebugView
#include "ANSODVendorGate.h" // ansod_vendor_gate::IsNvidiaGpuAvailable() #include "ANSODVendorGate.h" // ansod_vendor_gate::IsNvidiaGpuAvailable()
#include "ANSModelOpen.h" // modelpkg::IsPackagePath (sealed .ansmodel)
// Process-wide flag: when true, all engines force single-GPU path (no pool, no idle timers). // Process-wide flag: when true, all engines force single-GPU path (no pool, no idle timers).
// Defined here, declared extern in EngineBuildLoadNetwork.inl. // Defined here, declared extern in EngineBuildLoadNetwork.inl.
@@ -1576,6 +1577,13 @@ ANSODENGINE_API int OptimizeModelStr(const char* modelFilePath, const char* mode
modelType, modelDetectionType, fp16, modelFilePath ? modelFilePath : "(null)"); modelType, modelDetectionType, fp16, modelFilePath ? modelFilePath : "(null)");
try { try {
bool optimizedResult = false; bool optimizedResult = false;
// A sealed .ansmodel is already the artefact: there is no zip to
// inflate and no loose ONNX to point an optimiser at. It still goes
// through the handle below, which opens it in memory and builds the
// TensorRT plan from the decrypted bytes — what it must not go
// through is ModelOptimizer, which only knows how to unzip.
const bool sealedPackage =
ANSCENTER::modelpkg::IsPackagePath(modelFilePath ? modelFilePath : "");
// NOTE: odMutex was removed here. OptimizeModelStr creates its own // NOTE: odMutex was removed here. OptimizeModelStr creates its own
// temporary Engine<float> on the stack — no shared state with running // temporary Engine<float> on the stack — no shared state with running
// inference tasks. The global mutex was blocking ALL running tasks' // inference tasks. The global mutex was blocking ALL running tasks'
@@ -1628,7 +1636,7 @@ ANSODENGINE_API int OptimizeModelStr(const char* modelFilePath, const char* mode
} }
if (modelType == 31) // If modelType is RTYOLO (31), handle separately. if (modelType == 31 && !sealedPackage) // If modelType is RTYOLO (31), handle separately.
{ {
if (engineType == ANSCENTER::EngineType::NVIDIA_GPU) { if (engineType == ANSCENTER::EngineType::NVIDIA_GPU) {
if (modelDetectionType == 0) { if (modelDetectionType == 0) {
@@ -1778,7 +1786,10 @@ ANSODENGINE_API int OptimizeModelStr(const char* modelFilePath, const char* mode
Handle.reset(); // Destroy engines BEFORE guard restores cache Handle.reset(); // Destroy engines BEFORE guard restores cache
if (optimizedResult && !modelFolder.empty()) return 1; // A package reports the folder its plan was cached in; when the
// handle declined to name one there is still nothing wrong, because
// nothing was ever meant to be written.
if (optimizedResult && (!modelFolder.empty() || sealedPackage)) return 1;
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
+27
View File
@@ -180,6 +180,25 @@ public:
[[nodiscard]] const std::vector<nvinfer1::Dims> &getOutputDims() const override { return m_outputDims; }; [[nodiscard]] const std::vector<nvinfer1::Dims> &getOutputDims() const override { return m_outputDims; };
[[nodiscard]] const ANSCENTER::Options& getOptions() const { return m_options; } [[nodiscard]] const ANSCENTER::Options& getOptions() const { return m_options; }
/// True when the last buildLoadNetwork() had to build the engine from ONNX
/// because no cached engine matched the current profile (GPU / precision /
/// batch / TRT version). Callers use this to invalidate stale caches that
/// carried the old engine (e.g. ServerOptimised model zips).
[[nodiscard]] bool rebuiltFromOnnx() const { return m_rebuiltFromOnnx; }
/// Full path of the engine file resolved by the last buildLoadNetwork().
[[nodiscard]] const std::string& engineFilePath() const { return m_engineFilePath; }
// ── ONNX bytes held in memory (sealed .ansmodel packages) ───────────────
// A package is decrypted into memory and the plaintext model must never
// reach the disk, so the parser reads these bytes instead of opening
// onnxModelPath. The path is still passed in and still matters: it names
// the TensorRT plan that gets cached beside it, and a plan is a built
// artefact, not the model. Set the bytes before any build call; they are
// wiped by clearOnnxBytes() and by the destructor.
void setOnnxBytes(const void* data, size_t size);
void clearOnnxBytes();
[[nodiscard]] bool hasOnnxBytes() const { return !m_onnxBytes.empty(); }
/// Return the CUDA device index that preprocessing should target. /// Return the CUDA device index that preprocessing should target.
/// In pool mode this returns the device of the first alive slot; /// In pool mode this returns the device of the first alive slot;
/// in single-engine mode it returns m_options.deviceIndex. /// in single-engine mode it returns m_options.deviceIndex.
@@ -236,6 +255,9 @@ public:
// retries until build succeeds or all candidates are exhausted. // retries until build succeeds or all candidates are exhausted.
// Fixed-spatial models fall through to a single build() call. // Fixed-spatial models fall through to a single build() call.
bool buildWithRetry(std::string onnxModelPath, const std::array<float, 3>& subVals, const std::array<float, 3>& divVals, bool normalize); bool buildWithRetry(std::string onnxModelPath, const std::array<float, 3>& subVals, const std::array<float, 3>& divVals, bool normalize);
// Where the ONNX bytes come from: the in-memory buffer when one was set,
// otherwise the file at onnxModelPath.
bool readOnnxBytes(const std::string& onnxModelPath, std::vector<char>& buffer) const;
void getDeviceNames(std::vector<std::string>& deviceNames); void getDeviceNames(std::vector<std::string>& deviceNames);
void clearGpuBuffers(); void clearGpuBuffers();
int getBindingIndexByName(const std::string& name); int getBindingIndexByName(const std::string& name);
@@ -393,6 +415,9 @@ private:
bool m_usingCachedEngine = false; bool m_usingCachedEngine = false;
bool m_skipEngineCache = false; // true = bypass TRTEngineCache (for OptimizeModelStr) bool m_skipEngineCache = false; // true = bypass TRTEngineCache (for OptimizeModelStr)
bool m_skipOnnxRebuild = false; // true = don't delete/rebuild corrupt engine files (elastic growth) bool m_skipOnnxRebuild = false; // true = don't delete/rebuild corrupt engine files (elastic growth)
std::vector<char> m_onnxBytes; // model bytes in memory (package); empty = read from disk
bool m_rebuiltFromOnnx = false; // last buildLoadNetwork built from ONNX (no matching cached engine)
std::string m_engineFilePath; // engine file resolved by last buildLoadNetwork
bool m_skipOnnxBuild = false; // true = skip ONNX→TRT build if no cached engine (demand growth) bool m_skipOnnxBuild = false; // true = skip ONNX→TRT build if no cached engine (demand growth)
bool m_lastLoadFailedVRAM = false; // set by loadNetwork when failure is due to insufficient VRAM bool m_lastLoadFailedVRAM = false; // set by loadNetwork when failure is due to insufficient VRAM
bool m_forceNoPool = false; // true = force single-GPU path, skip multi-GPU pool bool m_forceNoPool = false; // true = force single-GPU path, skip multi-GPU pool
@@ -565,6 +590,8 @@ template <typename T> Engine<T>::Engine() {
m_lastSetInputDims.clear(); m_lastSetInputDims.clear();
} }
template <typename T> Engine<T>::~Engine() { template <typename T> Engine<T>::~Engine() {
// A decrypted model must not outlive the engine that parsed it.
try { clearOnnxBytes(); } catch (...) {}
// ========================================================================= // =========================================================================
// CRITICAL: Destructors must NEVER throw. Every CUDA/TRT call below is // CRITICAL: Destructors must NEVER throw. Every CUDA/TRT call below is
// wrapped in try/catch so that a failure in one step does not skip the // wrapped in try/catch so that a failure in one step does not skip the
+1 -1
View File
@@ -33,7 +33,7 @@ namespace ANSCENTER
{1005, "ANSOCR"}, {1005, "ANSOCR"},
{1006, "ANSALPR"}, {1006, "ANSALPR"},
{1007, "ANSCV"}, {1007, "ANSCV"},
{1008, "ANSSRT"} {1008, "ANSTS"}
}; };
ansawss3LicenceValid = false; ansawss3LicenceValid = false;
for (const auto& [productId, productName] : licenseChecks) { for (const auto& [productId, productName] : licenseChecks) {
+1 -1
View File
@@ -40,7 +40,7 @@ namespace ANSCENTER
{1005, "ANSOCR"}, {1005, "ANSOCR"},
{1006, "ANSALPR"}, {1006, "ANSALPR"},
{1007, "ANSCV"}, {1007, "ANSCV"},
{1008, "ANSSRT"} {1008, "ANSTS"}
}; };
ansutilityLicenceValid = false; ansutilityLicenceValid = false;
for (const auto& [productId, productName] : licenseChecks) { for (const auto& [productId, productName] : licenseChecks) {
+572 -19
View File
@@ -19,6 +19,8 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <iomanip> #include <iomanip>
#include <filesystem>
#include <set>
#include "ANSFR.h" #include "ANSFR.h"
#include "fastdeploy/vision.h" #include "fastdeploy/vision.h"
#include "ANSFilePlayer.h" #include "ANSFilePlayer.h"
@@ -29,6 +31,12 @@ using namespace cv;
using namespace dnn; using namespace dnn;
using namespace std; using namespace std;
// Official model packs — C:\Projects\SharedCore is the canonical
// distribution folder for released ANS_FaceRecognizer zips.
static const std::string kFaceRecognizerV14Zip = "C:\\Projects\\SharedCore\\ANS_FaceRecognizer_v1.4.zip";
static const std::string kFaceRecognizerV11Zip = "C:\\Projects\\SharedCore\\ANS_FaceRecognizer_v1.1.zip";
static const std::string kFaceDetectorZip = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ANS_GenericFD(GPU)_v1.0.zip";
template<typename T> template<typename T>
T GetOptionalValue(const boost::property_tree::ptree& pt, std::string attribute, T defaultValue) { T GetOptionalValue(const boost::property_tree::ptree& pt, std::string attribute, T defaultValue) {
if (pt.count(attribute)) { if (pt.count(attribute)) {
@@ -723,7 +731,7 @@ int ANSVISTest() {
// Get the current working directory // Get the current working directory
std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db"; std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db";
std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANS_FaceRecognizer_v1.1.zip"; std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANS_FaceRecognizer_v1.1.zip";
std::string facedetectorFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ANS_GenericFD(GPU)_v1.0.zip"; std::string facedetectorFilePath = kFaceDetectorZip;
//std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANS_FaceRecognizer_v1.1.zip"; //std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANS_FaceRecognizer_v1.1.zip";
@@ -731,7 +739,7 @@ int ANSVISTest() {
//std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\ANSVIS_Issues\\face.mp4"; //std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\ANSVIS_Issues\\face.mp4";
//std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\Face31.mp4"; //std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\Face31.mp4";
std::string videoFilePath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4"; std::string videoFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\classroom.mp4";
//std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\school1.mp4";// "C:\\ProgramData\\ANSCENTER\\Shared\\classroom.mp4"; //std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\school1.mp4";// "C:\\ProgramData\\ANSCENTER\\Shared\\classroom.mp4";
const char* configFilePath = ""; const char* configFilePath = "";
@@ -1141,7 +1149,7 @@ int ANSVISTestFilePlayer() {
std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db"; std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db";
std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANS_FaceRecognizer_v1.1.zip"; std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANS_FaceRecognizer_v1.1.zip";
std::string facedetectorFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ANS_GenericFD(GPU)_v1.0.zip"; std::string facedetectorFilePath = kFaceDetectorZip;
// std::string facedetectorFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANS_GenericFD(CPU)_v1.0.zip"; // std::string facedetectorFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANS_GenericFD(CPU)_v1.0.zip";
std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\school1.mp4"; std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\school1.mp4";
@@ -1294,7 +1302,7 @@ int FRStressTest() {
std::filesystem::path currentPath = "C:\\Programs\\DemoAssets"; std::filesystem::path currentPath = "C:\\Programs\\DemoAssets";
std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db"; std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db";
std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANS_FaceRecognizer_v1.0.zip"; std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANS_FaceRecognizer_v1.0.zip";
std::string facedetectorFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ANS_GenericFD(GPU)_v1.0.zip"; std::string facedetectorFilePath = kFaceDetectorZip;
//std::string facedetectorFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANS_GenericFD(CPU)_v1.0.zip"; //std::string facedetectorFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANS_GenericFD(CPU)_v1.0.zip";
std::string imagePath = "C:\\Programs\\DemoAssets\\Images\\TestFaces\\Tien1.jpg"; std::string imagePath = "C:\\Programs\\DemoAssets\\Images\\TestFaces\\Tien1.jpg";
@@ -1619,13 +1627,13 @@ int InsertFace(std::string databaseFilePath, std::string recognizerFilePath, std
} }
int TestCompleteFR1() { int TestCompleteFR1() {
std::filesystem::path currentPath = "E:\\Programs\\DemoAssets"; //std::filesystem::path currentPath = "E:\\Programs\\DemoAssets";
std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db"; std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db";
std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANS_FaceRecognizer_v1.1.zip"; std::string recognizerFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ServerOptimised\\ANS_FaceRecognizer_v1.1_NVIDIAGeForceRTX4070LaptopGPU.zip";
std::string facedetectorFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ServerOptimised\\ANS_GenericFD(GPU)_v1.0_NVIDIAGeForceRTX4070LaptopGPU.zip"; std::string facedetectorFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ServerOptimised\\ANS_GenericFD(GPU)_v1.0_NVIDIAGeForceRTX4070LaptopGPU.zip";
//std::string imageFile = "E:\\Programs\\DemoAssets\\Images\\Face\\Original.jpg"; //std::string imageFile = "E:\\Programs\\DemoAssets\\Images\\Face\\Original.jpg";
std::string imageFile = "E:\\Programs\\DemoAssets\\Images\\Face\\61.png"; std::string imageFile = "C:\\Programs\\DemoAssets\\OIP.jpg";
//InsertUser(databaseFilePath, recognizerFilePath, facedetectorFilePath,"0001","Tien"); InsertUser(databaseFilePath, recognizerFilePath, facedetectorFilePath,"0001","Tien");
InsertFace(databaseFilePath, recognizerFilePath, facedetectorFilePath, imageFile); InsertFace(databaseFilePath, recognizerFilePath, facedetectorFilePath, imageFile);
std::cout << "End of program.\n"; std::cout << "End of program.\n";
@@ -1713,8 +1721,551 @@ int SCRFDVideoRepro()
return 0; return 0;
} }
// ===========================================================================
// ANS_FaceRecognizer v1.4 test:
// - AdaFace loads by default from the v1.4 model pack
// - ONNX age-gender + emotion models load (TensorRT EP on NVIDIA)
// - SetANSRFRecognizerModel switches adaface <-> arcface live, and stored
// embeddings are regenerated so recognition keeps working after a switch
// ===========================================================================
static bool V14Check(bool condition, const std::string& label, int& failures)
{
std::cout << (condition ? "[PASS] " : "[FAIL] ") << label << std::endl;
if (!condition) ++failures;
return condition;
}
int TestModelSwitchV14()
{
const std::string modelZip = kFaceRecognizerV14Zip;
const std::string detectorZip = kFaceDetectorZip;
const std::string dbFolder = "C:\\Programs\\DemoAssets\\ANSFRDB\\v14_test";
const std::string dbFile = dbFolder + "\\ANSFR_v14_test.db";
const std::string enrollImage = "C:\\Programs\\DemoAssets\\TestImages\\FacialRecognition\\Obama+Oprah1.jpg";
const std::string userName = "V14TestUser";
int failures = 0;
std::cout << "\n===== TestModelSwitchV14 =====" << std::endl;
// Fresh database each run so the test is repeatable
std::error_code ec;
std::filesystem::remove_all(dbFolder, ec);
cv::Mat frame = cv::imread(enrollImage, cv::IMREAD_COLOR);
if (!V14Check(!frame.empty(), "test image readable: " + enrollImage, failures))
return failures;
ANSCENTER::ANSFacialRecognition* h = nullptr;
int cr = CreateANSRFHandle(&h, "", "", dbFile.c_str(), modelZip.c_str(), detectorZip.c_str(),
0, 0.35f, 1, 1, 1, 30, 0.5f, 0, 0);
if (!V14Check(cr > 0 && h != nullptr, "CreateANSRFHandle", failures))
return failures;
int le = LoadANSRFEngine(&h);
if (!V14Check(le == 1, "LoadANSRFEngine (v1.4 model pack)", failures)) {
ReleaseANSRFHandle(&h);
return failures;
}
// --- default model + attribute backends -------------------------------
std::string info = h->GetActiveModelInfo();
std::cout << "Model info: " << info << std::endl;
V14Check(GetANSRFRecognizerModel(&h) == 0, "default recognizer model is AdaFace (0)", failures);
V14Check(info.find("\"recognizer_model\":\"adaface\"") != std::string::npos,
"model info reports adaface", failures);
V14Check(info.find("\"agegender_backend\":\"onnx\"") != std::string::npos,
"age-gender uses agegender.onnx backend", failures);
V14Check(info.find("\"emotion_backend\":\"onnx\"") != std::string::npos,
"emotions use emotionsrecognition.onnx backend", failures);
// --- enroll one user ---------------------------------------------------
int userId = InsertUser(&h, "0001", userName.c_str());
V14Check(userId > 0, "InsertUser", failures);
unsigned int bufLen = 0;
unsigned char* buf = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen);
int faceInserted = InsertFaceBinary(&h, userId, buf, frame.cols, frame.rows);
delete buf;
V14Check(faceInserted > 0, "InsertFaceBinary (enrollment)", failures);
Reload(&h);
auto recognizesUser = [&](const char* label) {
// Full pipeline (detect + match). The Recognize API needs a config
// file with _recInputShape, which this test intentionally omits.
unsigned int len = 0;
unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, len);
std::string res = RunANSRFInferenceBinary(&h, bytes, frame.cols, frame.rows);
delete bytes;
std::cout << " recognition result: " << res << std::endl;
V14Check(res.find(userName) != std::string::npos, label, failures);
};
recognizesUser("recognized with AdaFace embeddings");
// --- live switch to ArcFace (regenerates stored embeddings) ------------
V14Check(SetANSRFRecognizerModel(&h, 1) == 1, "switch to ArcFace", failures);
V14Check(GetANSRFRecognizerModel(&h) == 1, "active model is ArcFace (1)", failures);
std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl;
recognizesUser("recognized after switch to ArcFace");
// --- persistence: the explicit ArcFace choice must survive a restart ---
ReleaseANSRFHandle(&h);
h = nullptr;
{
int cr2 = CreateANSRFHandle(&h, "", "", dbFile.c_str(), modelZip.c_str(), detectorZip.c_str(),
0, 0.35f, 1, 1, 1, 30, 0.5f, 0, 0);
V14Check(cr2 > 0 && h != nullptr, "re-create handle (simulated restart)", failures);
V14Check(LoadANSRFEngine(&h) == 1, "reload engine after restart", failures);
V14Check(GetANSRFRecognizerModel(&h) == 1,
"persisted ArcFace choice restored after restart", failures);
recognizesUser("recognized after restart with persisted ArcFace");
}
// --- and back to AdaFace ------------------------------------------------
V14Check(SetANSRFRecognizerModel(&h, 0) == 1, "switch back to AdaFace", failures);
V14Check(GetANSRFRecognizerModel(&h) == 0, "active model is AdaFace (0)", failures);
recognizesUser("recognized after switch back to AdaFace");
// --- attributes through the full pipeline (ONNX age/gender + emotions) --
{
unsigned int len = 0;
unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, len);
std::string res = RunANSRFInferenceBinary(&h, bytes, frame.cols, frame.rows);
delete bytes;
std::cout << " inference (attributes): " << res << std::endl;
V14Check(res.find("Age") != std::string::npos || res.find("results") != std::string::npos,
"inference with ONNX attribute models returns results", failures);
}
ReleaseANSRFHandle(&h);
std::cout << "===== TestModelSwitchV14: "
<< (failures == 0 ? "ALL PASSED" : (std::to_string(failures) + " FAILURE(S)"))
<< " =====" << std::endl;
return failures;
}
// ===========================================================================
// Classroom video demo — v1.4 pack with face attributes drawn as overlays.
// Each face gets its box, user name, and the Gender / Age / Emotion parsed
// from the result's extra_info JSON. ESC quits; an annotated frame is saved
// every 30 frames to C:\Programs\DemoAssets\ so there is a reviewable
// artifact even in headless runs.
// ===========================================================================
int ClassroomAttributesVideoTest()
{
const std::string modelZip = kFaceRecognizerV14Zip;
const std::string detectorZip = kFaceDetectorZip;
const std::string dbFile = "C:\\Programs\\DemoAssets\\ANSFRDB\\v14_test\\ANSFR_v14_test.db";
const std::string videoPath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
const int maxFrames = 600;
std::cout << "\n===== ClassroomAttributesVideoTest =====" << std::endl;
ANSCENTER::ANSFacialRecognition* h = nullptr;
// age/gender + emotions + head pose ON, liveness/antispoof OFF for speed
int cr = CreateANSRFHandle(&h, "", "", dbFile.c_str(), modelZip.c_str(), detectorZip.c_str(),
0, 0.35f, 1, 1, 1, 30, 0.5f, 0, 0);
std::cout << "CreateANSRFHandle = " << cr << std::endl;
if (cr <= 0 || !h) return 1;
int le = LoadANSRFEngine(&h);
std::cout << "LoadANSRFEngine = " << le << std::endl;
std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl;
if (le != 1) { ReleaseANSRFHandle(&h); return 1; }
cv::VideoCapture cap(videoPath);
if (!cap.isOpened()) {
std::cout << "ERROR: cannot open video " << videoPath << std::endl;
ReleaseANSRFHandle(&h);
return 1;
}
const std::string win = "ANSFR v1.4 - Face Attributes (ESC to quit)";
cv::namedWindow(win, cv::WINDOW_NORMAL);
cv::resizeWindow(win, 1280, 720);
int frameIdx = 0, saved = 0;
cv::Mat frame;
while (cap.read(frame)) {
if (frame.empty()) break;
if (++frameIdx > maxFrames) break;
unsigned int bufLen = 0;
unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen);
std::string res = RunANSRFInferenceBinary(&h, bytes, frame.cols, frame.rows);
delete bytes;
try {
boost::property_tree::ptree pt;
std::stringstream ss; ss << res;
boost::property_tree::read_json(ss, pt);
if (pt.count("results")) {
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) {
const boost::property_tree::ptree& r = child.second;
const auto name = GetData<std::string>(r, "user_name");
const int x = static_cast<int>(GetData<float>(r, "x"));
const int y = static_cast<int>(GetData<float>(r, "y"));
const int w = static_cast<int>(GetData<float>(r, "width"));
const int ht = static_cast<int>(GetData<float>(r, "height"));
// extra_info is itself a JSON string:
// {"attributes":[{"Gender":..,"Age":..,"Emotion":..,"Pose(r,p,y)":..}]}
std::string gender, age, ageYears, emotion;
const auto extra = GetData<std::string>(r, "extra_info");
if (!extra.empty()) {
try {
boost::property_tree::ptree ept;
std::stringstream ess; ess << extra;
boost::property_tree::read_json(ess, ept);
if (ept.count("attributes")) {
for (const auto& attr : ept.get_child("attributes")) {
gender = GetOptionalValue<std::string>(attr.second, "Gender", "");
age = GetOptionalValue<std::string>(attr.second, "Age", "");
ageYears = GetOptionalValue<std::string>(attr.second, "AgeYears", "");
emotion = GetOptionalValue<std::string>(attr.second, "Emotion", "");
break;
}
}
}
catch (...) {}
}
if (!ageYears.empty())
age += " (" + ageYears + ")";
const bool known = (name != "Unknown");
const cv::Scalar boxColor = known ? cv::Scalar(0, 255, 0) : cv::Scalar(0, 200, 255);
cv::rectangle(frame, cv::Rect(x, y, w, ht), boxColor, 2);
std::vector<std::string> lines;
lines.push_back(name);
if (!gender.empty())
lines.push_back("Gender: " + gender);
if (!age.empty())
lines.push_back("Age: " + age);
if (!emotion.empty())
lines.push_back("Emotion: " + emotion);
int ty = y - 8 - 18 * static_cast<int>(lines.size() - 1);
for (const auto& line : lines) {
cv::putText(frame, line, cv::Point(x, ty),
cv::FONT_HERSHEY_SIMPLEX, 0.55, cv::Scalar(0, 0, 0), 3, cv::LINE_AA);
cv::putText(frame, line, cv::Point(x, ty),
cv::FONT_HERSHEY_SIMPLEX, 0.55, boxColor, 1, cv::LINE_AA);
ty += 18;
}
}
}
}
catch (...) {}
cv::putText(frame, cv::format("Frame %d", frameIdx), cv::Point(25, 45),
cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0, 255, 255), 2, cv::LINE_AA);
if (frameIdx % 30 == 0) {
std::string fp = cv::format("C:\\Programs\\DemoAssets\\ANSFR_v14_attr_frame_%04d.jpg", frameIdx);
cv::imwrite(fp, frame);
++saved;
}
cv::imshow(win, frame);
if (cv::waitKey(1) == 27) break; // ESC
}
std::cout << "Played " << frameIdx << " frames, saved " << saved
<< " annotated frames to C:\\Programs\\DemoAssets\\" << std::endl;
cap.release();
cv::destroyAllWindows();
ReleaseANSRFHandle(&h);
return 0;
}
// ===========================================================================
// Speed benchmark — v1.1 (arcface + OpenVINO attributes) vs
// v1.4 (adaface + ONNX/TensorRT attributes) on the same classroom video.
// Full pipeline per frame (detect + recognize + attributes), attributes ON.
// First run of a pack pays its engine builds; those are excluded from the
// timing (LoadEngine reported separately, plus warmup frames).
// Run one pack per process with: ANSFR-UnitTest.exe --bench old|new
// ===========================================================================
static void RunSinglePackBenchmark(const std::string& label,
const std::string& modelZip, const std::string& dbFolder,
int warmupFrames = 10, int measuredFrames = 100,
int attributeInterval = 0) // 0 = keep the DLL default (20)
{
const std::string videoPath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
const std::string detectorZip = kFaceDetectorZip;
std::cout << "\n===== BENCH [" << label << "] =====" << std::endl;
std::cout << "model pack: " << modelZip << std::endl;
std::error_code ec;
std::filesystem::remove_all(dbFolder, ec);
const std::string dbFile = dbFolder + "\\bench.db";
ANSCENTER::ANSFacialRecognition* h = nullptr;
int cr = CreateANSRFHandle(&h, "", "", dbFile.c_str(), modelZip.c_str(), detectorZip.c_str(),
0, 0.35f, 1, 1, 1, 30, 0.5f, 0, 0);
if (cr <= 0 || !h) {
std::cout << "[BENCH " << label << "] CreateANSRFHandle FAILED: " << cr << std::endl;
return;
}
auto loadStart = std::chrono::steady_clock::now();
int le = LoadANSRFEngine(&h);
auto loadMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - loadStart).count();
std::cout << "LoadANSRFEngine = " << le << " (" << loadMs << " ms)" << std::endl;
if (attributeInterval > 0) {
SetANSRFAttributeInterval(&h, attributeInterval);
std::cout << "Attribute interval overridden to " << attributeInterval << std::endl;
}
std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl;
if (le != 1) { ReleaseANSRFHandle(&h); return; }
cv::VideoCapture cap(videoPath);
if (!cap.isOpened()) {
std::cout << "ERROR: cannot open video " << videoPath << std::endl;
ReleaseANSRFHandle(&h);
return;
}
double totalMs = 0.0, minMs = 1e9, maxMs = 0.0;
long long totalFaces = 0;
int measured = 0, frameIdx = 0;
cv::Mat frame;
while (cap.read(frame) && measured < measuredFrames) {
if (frame.empty()) break;
++frameIdx;
unsigned int bufLen = 0;
unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen);
auto t0 = std::chrono::steady_clock::now();
std::string res = RunANSRFInferenceBinary(&h, bytes, frame.cols, frame.rows);
double ms = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - t0).count();
delete bytes;
if (frameIdx <= warmupFrames) continue; // engine warmup / TRT autotuning
++measured;
totalMs += ms;
minMs = (std::min)(minMs, ms);
maxMs = (std::max)(maxMs, ms);
// rough face count for context
size_t pos = 0;
while ((pos = res.find("\"user_name\"", pos)) != std::string::npos) {
++totalFaces; pos += 11;
}
}
cap.release();
if (measured > 0) {
const double avg = totalMs / measured;
std::cout << std::fixed << std::setprecision(1);
std::cout << "[BENCH " << label << "] frames=" << measured
<< " avg=" << avg << " ms"
<< " min=" << minMs << " ms"
<< " max=" << maxMs << " ms"
<< " fps=" << (1000.0 / avg)
<< " avg faces/frame=" << (double)totalFaces / measured
<< std::endl;
}
else {
std::cout << "[BENCH " << label << "] no frames measured" << std::endl;
}
ReleaseANSRFHandle(&h);
}
int ModelSpeedBenchmark()
{
RunSinglePackBenchmark("v1.1 arcface + OpenVINO attrs",
kFaceRecognizerV11Zip,
"C:\\Programs\\DemoAssets\\ANSFRDB\\v11_bench");
RunSinglePackBenchmark("v1.4 adaface + ONNX attrs",
kFaceRecognizerV14Zip,
"C:\\Programs\\DemoAssets\\ANSFRDB\\v14_bench");
return 0;
}
// ===========================================================================
// Output-format parity: the v1.4 pack (adaface + ONNX attributes) must emit
// the same JSON schema as the v1.1 pack (arcface + OpenVINO attributes).
// Runs the identical enroll + inference flow through both packs and compares
// the result-object keys, the attribute keys (v1.4 may only ADD "AgeYears"),
// and the value vocabularies (Gender / Age buckets / Emotion labels).
// ===========================================================================
static std::string ParityRunOnePack(const std::string& label,
const std::string& modelZip, const std::string& dbFolder)
{
const std::string detectorZip = kFaceDetectorZip;
const std::string imagePath = "C:\\Programs\\DemoAssets\\TestImages\\FacialRecognition\\Obama+Oprah1.jpg";
std::error_code ec;
std::filesystem::remove_all(dbFolder, ec);
const std::string dbFile = dbFolder + "\\parity.db";
ANSCENTER::ANSFacialRecognition* h = nullptr;
int cr = CreateANSRFHandle(&h, "", "", dbFile.c_str(), modelZip.c_str(), detectorZip.c_str(),
0, 0.35f, 1, 1, 1, 30, 0.5f, 0, 0);
if (cr <= 0 || !h) { std::cout << "[PARITY " << label << "] CreateANSRFHandle FAILED\n"; return ""; }
if (LoadANSRFEngine(&h) != 1) { std::cout << "[PARITY " << label << "] LoadANSRFEngine FAILED\n"; ReleaseANSRFHandle(&h); return ""; }
std::cout << "[PARITY " << label << "] " << h->GetActiveModelInfo() << std::endl;
cv::Mat frame = cv::imread(imagePath, cv::IMREAD_COLOR);
int userId = InsertUser(&h, "0001", "ParityUser");
unsigned int bufLen = 0;
unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen);
InsertFaceBinary(&h, userId, bytes, frame.cols, frame.rows);
delete bytes;
Reload(&h);
std::string res;
// Several frames so attributes are computed and smoothed like production
for (int i = 0; i < 6; ++i) {
unsigned int len = 0;
unsigned char* b = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, len);
res = RunANSRFInferenceBinary(&h, b, frame.cols, frame.rows);
delete b;
}
ReleaseANSRFHandle(&h);
return res;
}
int TestOutputFormatParity()
{
int failures = 0;
std::cout << "\n===== TestOutputFormatParity =====" << std::endl;
std::string oldJson = ParityRunOnePack("v1.1",
kFaceRecognizerV11Zip,
"C:\\Programs\\DemoAssets\\ANSFRDB\\v11_parity");
std::string newJson = ParityRunOnePack("v1.4",
kFaceRecognizerV14Zip,
"C:\\Programs\\DemoAssets\\ANSFRDB\\v14_parity");
if (!V14Check(!oldJson.empty() && !newJson.empty(), "both packs produced results", failures))
return failures;
std::cout << "v1.1 JSON: " << oldJson << std::endl;
std::cout << "v1.4 JSON: " << newJson << std::endl;
auto parse = [](const std::string& json) {
boost::property_tree::ptree pt;
std::stringstream ss; ss << json;
boost::property_tree::read_json(ss, pt);
return pt;
};
auto keysOf = [](const boost::property_tree::ptree& node) {
std::set<std::string> keys;
for (const auto& kv : node) keys.insert(kv.first);
return keys;
};
try {
auto oldPt = parse(oldJson);
auto newPt = parse(newJson);
const auto& oldFirst = oldPt.get_child("results").begin()->second;
const auto& newFirst = newPt.get_child("results").begin()->second;
// 1. Result-object fields must be identical
V14Check(keysOf(oldFirst) == keysOf(newFirst),
"result-object field names identical", failures);
// 2. Attribute keys must be identical — AgeYears comes from the
// shared serializer, so both backends emit the exact same key set.
auto attrKeys = [&](const boost::property_tree::ptree& r) {
auto ept = parse(r.get<std::string>("extra_info"));
return keysOf(ept.get_child("attributes").begin()->second);
};
V14Check(attrKeys(oldFirst) == attrKeys(newFirst),
"attribute keys identical", failures);
// 3. Value vocabularies must match on every face of both packs.
// Gender/Age vocabularies are shared; the Emotion vocabulary is
// per-pack: v1.1's legacy display names vs v1.4's verbatim class
// names from emotionsrecognition.names.
auto checkVocab = [&](const boost::property_tree::ptree& pt, const char* label,
const std::vector<std::string>& emotionVocab) {
for (const auto& child : pt.get_child("results")) {
auto ept = parse(child.second.get<std::string>("extra_info"));
for (const auto& attr : ept.get_child("attributes")) {
const auto gender = GetOptionalValue<std::string>(attr.second, "Gender", "");
const auto age = GetOptionalValue<std::string>(attr.second, "Age", "");
const auto emotion = GetOptionalValue<std::string>(attr.second, "Emotion", "");
V14Check(gender == "Male" || gender == "Female",
std::string(label) + " Gender in {Male, Female}: " + gender, failures);
V14Check(age == "Kids" || age == "Young Adults" || age == "Adults" || age == "Elders",
std::string(label) + " Age in bucket set: " + age, failures);
bool emotionOk = emotion.find("%)") != std::string::npos;
if (emotionOk) {
emotionOk = false;
for (const auto& name : emotionVocab)
if (emotion.rfind(name, 0) == 0) { emotionOk = true; break; }
}
V14Check(emotionOk,
std::string(label) + " Emotion label/format: " + emotion, failures);
}
}
};
checkVocab(oldPt, "v1.1", { "Happy", "Neutral", "Attentive" });
checkVocab(newPt, "v1.4", { "Angry", "Fear", "Happy", "Neutral", "Sad" });
// 4. Both packs must recognize the enrolled user (same matching semantics)
V14Check(oldJson.find("ParityUser") != std::string::npos, "v1.1 recognizes enrolled user", failures);
V14Check(newJson.find("ParityUser") != std::string::npos, "v1.4 recognizes enrolled user", failures);
}
catch (const std::exception& e) {
std::cout << "[FAIL] parity comparison threw: " << e.what() << std::endl;
++failures;
}
std::cout << "===== TestOutputFormatParity: "
<< (failures == 0 ? "ALL PASSED" : (std::to_string(failures) + " FAILURE(S)"))
<< " =====" << std::endl;
return failures;
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// Output-format parity between v1.1 and v1.4 packs:
// ANSFR-UnitTest.exe --parity
if (argc >= 2 && std::string(argv[1]) == "--parity") {
return TestOutputFormatParity();
}
// v1.4 model-switch + persistence test: ANSFR-UnitTest.exe --test
if (argc >= 2 && std::string(argv[1]) == "--test") {
return TestModelSwitchV14();
}
// Single-pack benchmark mode (one pack per process so the packs cannot
// influence each other): ANSFR-UnitTest.exe --bench old|new|both [interval]
// The optional interval overrides the slow-attribute refresh interval
// (default 5) via SetANSRFAttributeInterval.
if (argc >= 3 && std::string(argv[1]) == "--bench") {
const std::string which = argv[2];
const int interval = (argc >= 4) ? std::atoi(argv[3]) : 0;
const std::string tag = (interval > 0)
? (" @interval " + std::to_string(interval)) : std::string();
if (which == "old")
RunSinglePackBenchmark("v1.1 arcface + OpenVINO attrs" + tag,
kFaceRecognizerV11Zip,
"C:\\Programs\\DemoAssets\\ANSFRDB\\v11_bench",
10, 100, interval);
else if (which == "new")
RunSinglePackBenchmark("v1.4 adaface + ONNX attrs" + tag,
kFaceRecognizerV14Zip,
"C:\\Programs\\DemoAssets\\ANSFRDB\\v14_bench",
10, 100, interval);
else
ModelSpeedBenchmark();
return 0;
}
// ── GPU visibility bisect (Blackwell Myelin l2cm debugging) ───────────── // ── GPU visibility bisect (Blackwell Myelin l2cm debugging) ─────────────
// Usage: ANSFR-UnitTest.exe --gpu 0 → only GPU0 visible (Test A, display GPU) // Usage: ANSFR-UnitTest.exe --gpu 0 → only GPU0 visible (Test A, display GPU)
// ANSFR-UnitTest.exe --gpu 1 → only GPU1 visible (Test B, headless GPU) // ANSFR-UnitTest.exe --gpu 1 → only GPU1 visible (Test B, headless GPU)
@@ -1722,22 +2273,24 @@ int main(int argc, char** argv)
// Sets CUDA_VISIBLE_DEVICES before the first CUDA call in this process, so // Sets CUDA_VISIBLE_DEVICES before the first CUDA call in this process, so
// the CUDA runtime only enumerates the chosen physical GPU (it becomes // the CUDA runtime only enumerates the chosen physical GPU (it becomes
// device 0 inside the process). // device 0 inside the process).
for (int i = 1; i + 1 < argc; ++i) { //for (int i = 1; i + 1 < argc; ++i) {
if (std::string(argv[i]) == "--gpu") { // if (std::string(argv[i]) == "--gpu") {
_putenv_s("CUDA_VISIBLE_DEVICES", argv[i + 1]); // _putenv_s("CUDA_VISIBLE_DEVICES", argv[i + 1]);
std::cout << "==============================================\n" // std::cout << "==============================================\n"
<< " GPU BISECT: CUDA_VISIBLE_DEVICES=" << argv[i + 1] << "\n" // << " GPU BISECT: CUDA_VISIBLE_DEVICES=" << argv[i + 1] << "\n"
<< " (only this physical GPU is visible to CUDA)\n" // << " (only this physical GPU is visible to CUDA)\n"
<< "==============================================" << std::endl; // << "==============================================" << std::endl;
} // }
} //}
//FaceDetectorTest(); //FaceDetectorTest();
//TestFaceRecognition(); //TestFaceRecognition();
//TestCompleteFR1(); //TestCompleteFR1();
//TestModelSwitchV14();
ClassroomAttributesVideoTest();
//ANSVISTest();
//ANSVISImageTest(); //ANSVISImageTest();
ANSVISTest(); //ANSVISTest();
//ANSVISTestFilePlayer(); //ANSVISTestFilePlayer();
// ANSVISRecordingTest(); // ANSVISRecordingTest();
//FRStressTest(); //FRStressTest();
@@ -0,0 +1,553 @@
// ANSModelPackage-UnitTest — the model package format, and its agreement with
// the other implementation.
//
// Usage: ANSModelPackage-UnitTest [data-dir]
//
// The interop fixture in data/ was written by ANSMINDCORE's packer. Opening it
// here is what makes "the two products read the same format" a fact rather
// than an intention: a change to either implementation that alters the wire
// shows up as this test failing.
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#include "ANSModelOpen.h"
#include "ANSOnnxShape.h"
#include "ANSModelPackage.h"
extern "C" {
#include "monocypher-ed25519.h"
#include "monocypher.h"
}
namespace fs = std::filesystem;
using namespace ANSCENTER;
using modelpkg::Json;
static int gChecks = 0, gFailures = 0;
static void CHECK(bool ok, const char* what) {
++gChecks;
if (!ok) {
++gFailures;
std::printf("FAIL %s\n", what);
}
}
// The fixture's keys, by the rules its generator recorded (see data/README).
struct Identity {
modelpkg::Key basePriv{}, basePub{};
std::vector<uint8_t> rootSk = std::vector<uint8_t>(64);
std::vector<uint8_t> rootPk = std::vector<uint8_t>(32);
std::vector<uint8_t> signerSk = std::vector<uint8_t>(64);
std::vector<uint8_t> signerPk = std::vector<uint8_t>(32);
};
static Identity fixtureIdentity() {
Identity id;
std::vector<uint8_t> seedRoot(32), seedSigner(32);
for (size_t i = 0; i < 32; ++i) {
id.basePriv[i] = uint8_t(i * 3 + 9);
seedRoot[i] = uint8_t(i * 7 + 1);
seedSigner[i] = uint8_t(i * 11 + 5);
}
crypto_x25519_public_key(id.basePub.data(), id.basePriv.data());
crypto_ed25519_key_pair(id.rootSk.data(), id.rootPk.data(), seedRoot.data());
crypto_ed25519_key_pair(id.signerSk.data(), id.signerPk.data(), seedSigner.data());
return id;
}
static modelpkg::Keyring fixtureKeyring(const Identity& id) {
modelpkg::Keyring ring;
ring.addX25519("ansk-v1-base", id.basePriv);
modelpkg::Key rootPub{};
std::memcpy(rootPub.data(), id.rootPk.data(), rootPub.size());
ring.addRoot("ansroot-v1", rootPub);
ring.nowSec = 1788000000; // a fixed "now", so the fixture does not age
return ring;
}
// ── the primitives, against their own published numbers ───────────────────
static void testVectors() {
auto hex = [](const std::string& h) {
std::vector<uint8_t> v;
for (size_t i = 0; i + 1 < h.size(); i += 2)
v.push_back(uint8_t(std::stoi(h.substr(i, 2), nullptr, 16)));
return v;
};
const std::string text =
"Ladies and Gentlemen of the class of '99: If I could offer you only one "
"tip for the future, sunscreen would be it.";
const auto key = hex("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f");
const auto aad = hex("50515253c0c1c2c3c4c5c6c7");
const auto nonce = hex("070000004041424344454647");
const std::vector<uint8_t> plain(text.begin(), text.end());
std::vector<uint8_t> ct(plain.size());
uint8_t tag[16];
aead::aeadSeal(key.data(), nonce.data(), aad.data(), aad.size(), plain.data(),
plain.size(), ct.data(), tag);
CHECK(std::memcmp(tag, hex("1ae10b594f09e26a7e902ecbd0600691").data(), 16) == 0,
"RFC 8439 §2.8.2: the AEAD tag is the published one");
const auto aSec = hex("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
const auto bSec = hex("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
std::vector<uint8_t> aPub(32), bPub(32), s1(32), s2(32);
crypto_x25519_public_key(aPub.data(), aSec.data());
crypto_x25519_public_key(bPub.data(), bSec.data());
crypto_x25519(s1.data(), aSec.data(), bPub.data());
crypto_x25519(s2.data(), bSec.data(), aPub.data());
CHECK(s1 == hex("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742") &&
s1 == s2,
"RFC 7748: X25519 reaches the published shared secret");
auto seed = hex("4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb");
const auto msg = hex("72");
std::vector<uint8_t> sk(64), pk(32), sig(64);
crypto_ed25519_key_pair(sk.data(), pk.data(), seed.data());
crypto_ed25519_sign(sig.data(), sk.data(), msg.data(), msg.size());
CHECK(sig == hex("92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da"
"085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00"),
"RFC 8032 test 2: the signature is the published one, so this is "
"SHA-512 Ed25519 rather than Monocypher's BLAKE2b EdDSA");
}
// ── the package ANSMINDCORE wrote ─────────────────────────────────────────
static void testInterop(const fs::path& dataDir) {
const std::string path = (dataDir / "interop.ansmodel").string();
if (!fs::exists(path)) {
std::printf("SKIP interop: %s is not here\n", path.c_str());
return;
}
const Identity id = fixtureIdentity();
const modelpkg::Keyring ring = fixtureKeyring(id);
modelpkg::Package pkg;
std::string err;
CHECK(modelpkg::open(path, ring, pkg, err),
("a package written by ANSMINDCORE opens here: " + err).c_str());
if (!err.empty()) return;
CHECK(pkg.signerId == "anssig-2026-09", "signed by the release key it names");
CHECK(!pkg.certExpired, "with a certificate still in date");
CHECK(pkg.manifest.value("project", std::string()) == "Interop",
"the manifest decrypts to what was packed");
CHECK(pkg.manifest.value("family", std::string()) == "yolo", "including the family");
CHECK(pkg.manifest.value("classes", Json::array()).size() == 2, "and its classes");
CHECK(pkg.entryPayloadId() == "p0", "and names the model to run");
std::vector<uint8_t> got;
CHECK(modelpkg::readPayload(pkg, "p0", got, err), ("read: " + err).c_str());
std::vector<uint8_t> want(12000);
for (size_t i = 0; i < want.size(); ++i) want[i] = uint8_t((i * 31 + 21) & 0xff);
CHECK(got == want, "and the model comes back byte for byte, across chunks");
}
// ── this implementation on its own ────────────────────────────────────────
static std::vector<uint8_t> fakeModel(size_t n, uint8_t seed) {
std::vector<uint8_t> v(n);
for (size_t i = 0; i < n; ++i) v[i] = uint8_t((i * 31 + seed) & 0xff);
return v;
}
static void testRoundTripAndRefusals(const fs::path& dir) {
const Identity id = fixtureIdentity();
const modelpkg::Keyring ring = fixtureKeyring(id);
modelpkg::PackRequest req;
req.keyId = "ansk-v1-base";
req.useX25519 = true;
req.basePublic = id.basePub;
req.chunk = 2048;
req.signerId = "anssig-2026-09";
req.signerSecret = id.signerSk;
req.signerPublic = id.signerPk;
req.rootKeyId = "ansroot-v1";
req.notAfter = "2030-01-01T00:00:00Z";
{
Json cert{{"keyId", req.signerId},
{"pub", bits::base64Encode(
std::string(id.signerPk.begin(), id.signerPk.end()))},
{"notAfter", req.notAfter}};
const std::string cb = modelpkg::detail::certBytes(cert);
req.rootSig.assign(64, 0);
crypto_ed25519_sign(req.rootSig.data(), id.rootSk.data(),
reinterpret_cast<const uint8_t*>(cb.data()), cb.size());
}
req.manifest = Json{{"task", "segmentation"},
{"family", "rfdetr"},
{"runtime", "onnx"},
{"classes", Json::array({Json{{"id", 1}, {"name", "Road"}}})},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
const auto model = fakeModel(7000, 3);
req.models.push_back({"p0", model});
const std::string path = (dir / "core.ansmodel").string();
std::string err;
CHECK(modelpkg::pack(path, req, err), ("pack: " + err).c_str());
modelpkg::Package pkg;
CHECK(modelpkg::open(path, ring, pkg, err), ("open: " + err).c_str());
std::vector<uint8_t> got;
CHECK(modelpkg::readPayload(pkg, "p0", got, err) && got == model,
"what this implementation writes, it reads back");
auto bytes = [&](const std::string& p) {
std::ifstream f(p, std::ios::binary);
return std::string((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
};
auto write = [&](const std::string& p, const std::string& b) {
std::ofstream f(p, std::ios::binary | std::ios::trunc);
f.write(b.data(), std::streamsize(b.size()));
};
const std::string good = bytes(path);
{ // a flipped bit in the model body
std::string bad = good;
bad[bad.size() - 30] = char(bad[bad.size() - 30] ^ 0x01);
const std::string p = (dir / "flipped.ansmodel").string();
write(p, bad);
modelpkg::Package t;
std::vector<uint8_t> out;
CHECK(modelpkg::open(p, ring, t, err) &&
!modelpkg::readPayload(t, "p0", out, err) && out.empty(),
"a flipped bit fails its chunk and yields no partial model");
}
{ // an unknown root
modelpkg::Keyring stranger;
stranger.addX25519("ansk-v1-base", id.basePriv);
stranger.addRoot("ansroot-other", modelpkg::Key{});
modelpkg::Package t;
CHECK(!modelpkg::open(path, stranger, t, err) &&
err.find("unknown root") != std::string::npos,
"an unknown root is refused");
}
{ // a revoked signer
modelpkg::Keyring revoking = ring;
revoking.revokedSigners.push_back("anssig-2026-09");
modelpkg::Package t;
CHECK(!modelpkg::open(path, revoking, t, err) &&
err.find("revoked") != std::string::npos,
"a revoked signing key is refused");
}
{ // not a package
const std::string p = (dir / "rubbish.bin").string();
write(p, "nothing to see here");
modelpkg::Package t;
CHECK(!modelpkg::open(p, ring, t, err) &&
err.find("not a model package") != std::string::npos,
"something else entirely is refused by its magic");
}
}
// ── the glue the engines load through ─────────────────────────────────────
//
// ANSODBase asks these three questions and nothing else: is this path a
// package, can this build open one, and what came out of it. The engines
// themselves need CUDA to compile, so this is where that contract is checked.
// ── every package gets its own key ────────────────────────────────────────
// pack() used to derive the content key, the ephemeral key and the nonces from
// one byte of seed, and the training box passed a seed made of the project's
// name and version: anyone holding a package could try the 256 seeds and read
// it without the base key. Two packs of the very same inputs must now share
// nothing that seals them — and still open with the base key.
static void testEachPackIsSealedAfresh(const fs::path& dir) {
const Identity id = fixtureIdentity();
const modelpkg::Keyring ring = fixtureKeyring(id);
modelpkg::PackRequest req;
req.keyId = "ansk-v1-base";
req.useX25519 = true;
req.basePublic = id.basePub;
req.chunk = 2048;
req.manifest = Json{{"task", "detection"},
{"family", "yolo"},
{"runtime", "onnx"},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
const auto model = fakeModel(5000, 11);
req.models.push_back({"p0", model});
req.models.push_back({"p1", model}); // two parts under one key: bases differ
const std::string pa = (dir / "fresh-a.ansmodel").string();
const std::string pb = (dir / "fresh-b.ansmodel").string();
std::string err;
CHECK(modelpkg::pack(pa, req, err), ("pack a: " + err).c_str());
CHECK(modelpkg::pack(pb, req, err), ("pack b: " + err).c_str());
modelpkg::Package a, b;
CHECK(modelpkg::open(pa, ring, a, err) && modelpkg::open(pb, ring, b, err),
("both open with the base key: " + err).c_str());
CHECK(a.contentKey != b.contentKey,
"two packs of identical inputs have different content keys");
bool seeded = false;
for (int seed = 0; seed < 256; ++seed) {
modelpkg::Key guess{};
for (size_t i = 0; i < guess.size(); ++i)
guess[i] = uint8_t(seed * 31 + i * 7 + 11);
seeded = seeded || guess == a.contentKey || guess == b.contentKey;
}
CHECK(!seeded, "and neither is one of the 256 a seed could have made");
const Json& wa = a.header["wraps"][0];
const Json& wb = b.header["wraps"][0];
CHECK(wa.value("epk", std::string()) != wb.value("epk", std::string()),
"each has its own ephemeral key");
CHECK(wa.value("nonce", std::string()) != wb.value("nonce", std::string()) &&
wa.value("ct", std::string()) != wb.value("ct", std::string()),
"and its own wrap nonce and wrapped key");
CHECK(a.header["payloads"][0].value("sha256", std::string()) !=
b.header["payloads"][0].value("sha256", std::string()),
"so the same model seals to different ciphertext");
CHECK(a.header["payloads"][0].value("nonceBase", std::string()) !=
a.header["payloads"][1].value("nonceBase", std::string()) &&
a.header["payloads"][0].value("nonceBase", std::string()) !=
a.header["manifest"].value("nonceBase", std::string()),
"and no two parts of one package share a nonce base");
std::vector<uint8_t> ga, gb;
CHECK(modelpkg::readPayload(a, "p0", ga, err) && ga == model &&
modelpkg::readPayload(b, "p1", gb, err) && gb == model,
"and both still give back the model that went in");
// The seeded path is for tests built as such; this one is not.
req.insecureDeterministicForTests = true;
CHECK(!modelpkg::pack((dir / "seeded.ansmodel").string(), req, err) &&
err.find("tests only") != std::string::npos,
"a build without the test switch refuses to pack from a seed");
}
static void testOpenForAnEngine(const fs::path& dataDir) {
const std::string path = (dataDir / "interop.ansmodel").string();
if (!fs::exists(path)) {
std::printf("SKIP engine glue: %s is not here\n", path.c_str());
return;
}
CHECK(modelpkg::IsPackagePath(path), "a .ansmodel path is recognised as one");
CHECK(!modelpkg::IsPackagePath("/models/ANS_Test_v1.0.zip"),
"and a zip is not");
CHECK(!modelpkg::IsPackagePath(".ansmodel"),
"nor is a bare extension with no name in front of it");
// A build with no keys installed refuses every package with a sentence
// that says why, rather than failing somewhere deeper.
{
modelpkg::SetProcessModelKeys(modelpkg::Keyring{});
CHECK(!modelpkg::HaveProcessModelKeys(),
"a build with no keys knows it cannot open packages");
modelpkg::OpenedModel m;
std::string err;
CHECK(!modelpkg::OpenModelPackage(path, m, err) &&
err.find("no model-package key") != std::string::npos,
"and says so instead of failing at the decryption");
}
const Identity id = fixtureIdentity();
modelpkg::SetProcessModelKeys(fixtureKeyring(id));
CHECK(modelpkg::HaveProcessModelKeys(), "with keys set, it can");
modelpkg::OpenedModel m;
std::string err;
CHECK(modelpkg::OpenModelPackage(path, m, err), ("open: " + err).c_str());
CHECK(m.model.size() == 12000, "the model comes out whole");
CHECK(m.task == "detection" && m.family == "yolo",
"with the task and family the engine branches on");
CHECK(m.classes.size() == 2 && m.classes[0] == "Cavity",
"and the classes in order, so there is no classes.names to read");
CHECK(!m.name.empty(), "and a name for the log line");
// What the engine does the moment its session has the bytes.
m.wipe();
CHECK(m.model.empty(), "wiping leaves the plaintext nowhere");
// Describing one is the same answer without the decryption.
modelpkg::OpenedModel d;
CHECK(modelpkg::DescribeModelPackage(path, d, err) && d.model.empty() &&
d.classes.size() == 2 && d.task == "detection",
"describing a package answers without decrypting the model");
}
// ── what a model says about its own input ─────────────────────────────────
//
// A package written before manifests carried an input size still has to
// answer, so the model's own graph is read. These are hand-written graphs
// with node and initializer ahead of the input, as a real export has them —
// which also proves the reader seeks past the weights rather than reading
// them.
static std::string pbVarint(uint64_t v) {
std::string out;
do {
uint8_t b = uint8_t(v & 0x7f);
v >>= 7;
if (v) b |= 0x80;
out.push_back(char(b));
} while (v);
return out;
}
static std::string pbField(uint32_t num, const std::string& payload) {
return pbVarint(uint64_t(num) << 3 | 2) + pbVarint(payload.size()) + payload;
}
static std::string pbDim(int64_t v) { // Dimension.dim_value
return pbField(1, pbVarint(uint64_t(1) << 3 | 0) + pbVarint(uint64_t(v)));
}
static std::string pbDynamicDim(const std::string& name) {
return pbField(1, pbField(2, name)); // dim_param: the axis is open
}
static std::string fakeOnnx(const std::string& dims,
const std::string& outDims = std::string()) {
auto value = [](const std::string& name, const std::string& d) {
return pbField(1, name) + pbField(2, pbField(1, pbField(2, d)));
};
std::string graph = pbField(1, std::string(4096, 'n')) +
pbField(5, std::string(65536, 'w')) +
pbField(11, value("images", dims));
if (!outDims.empty()) graph += pbField(12, value("output0", outDims));
return pbField(7, graph);
}
static void testShapeFromTheGraph(const fs::path& dir) {
onnxshape::InputShape shape;
std::string why;
const std::string fixed = fakeOnnx(pbDim(1) + pbDim(3) + pbDim(224) + pbDim(224));
CHECK(onnxshape::fromMemory(fixed.data(), fixed.size(), shape, why) &&
shape.width == 224 && shape.height == 224 && shape.channels == 3,
"a graph in memory gives the size it fixes, not a default");
const fs::path onFile = dir / "graph.onnx";
{
std::ofstream f(onFile, std::ios::binary | std::ios::trunc);
f.write(fixed.data(), std::streamsize(fixed.size()));
}
onnxshape::InputShape viaFile;
CHECK(onnxshape::fromFile(onFile.string(), viaFile, why) &&
viaFile.width == shape.width && viaFile.height == shape.height,
"a graph on disk reads the same as the same graph in memory");
const std::string dynamic = fakeOnnx(pbDynamicDim("batch") + pbDim(3) +
pbDynamicDim("height") + pbDynamicDim("width"));
CHECK(!onnxshape::fromMemory(dynamic.data(), dynamic.size(), shape, why) &&
why.find("dynamic") != std::string::npos,
"a model that takes any size says so rather than inventing one");
const std::string rubbish(600, '\x7f');
CHECK(!onnxshape::fromMemory(rubbish.data(), rubbish.size(), shape, why),
"and something that is not a model is refused, not guessed at");
// End to end: a package whose manifest says nothing about size, opened the
// way ANSODBase opens one.
const Identity id = fixtureIdentity();
modelpkg::SetProcessModelKeys(fixtureKeyring(id));
modelpkg::PackRequest req;
req.keyId = "ansk-v1-base";
req.useX25519 = true;
req.basePublic = id.basePub;
req.signerId = "anssig-2026-09";
req.signerSecret = id.signerSk;
req.signerPublic = id.signerPk;
req.rootKeyId = "ansroot-v1";
req.notAfter = "2030-01-01T00:00:00Z";
{
Json cert{{"keyId", req.signerId},
{"pub", bits::base64Encode(
std::string(id.signerPk.begin(), id.signerPk.end()))},
{"notAfter", req.notAfter}};
const std::string cb = modelpkg::detail::certBytes(cert);
req.rootSig.assign(64, 0);
crypto_ed25519_sign(req.rootSig.data(), id.rootSk.data(),
reinterpret_cast<const uint8_t*>(cb.data()), cb.size());
}
req.manifest = Json{{"task", "classification"},
{"family", "yolo"},
{"classes", Json::array({Json{{"id", 1}, {"name", "Good"}}})},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
CHECK(!req.manifest.contains("input"), "this manifest says nothing about size");
req.models.push_back({"p0", std::vector<uint8_t>(fixed.begin(), fixed.end())});
const std::string path = (dir / "noinput.ansmodel").string();
std::string err;
CHECK(modelpkg::pack(path, req, err), ("pack: " + err).c_str());
modelpkg::OpenedModel opened;
CHECK(modelpkg::OpenModelPackage(path, opened, err), ("open: " + err).c_str());
CHECK(opened.inputWidth == 224 && opened.inputHeight == 224,
"so the engine is told 224 from the graph, not left to assume 640");
// AN END-TO-END HEAD IN A PACKAGE THAT SAYS "yolo". Boxes used to take the
// family from the backend marker, and the marker names the runtime, not the
// head — so a yolo26 export with end2end on arrived declared as plain yolo.
// Decoding [1,N,6] as a v8 grid reads coordinates as confidences: boxes
// everywhere, and no error to notice. The graph settles it.
{
const std::string graph =
fakeOnnx(pbDim(1) + pbDim(3) + pbDim(640) + pbDim(640),
pbDim(1) + pbDim(300) + pbDim(6));
modelpkg::PackRequest r = req;
r.manifest = Json{{"task", "detection"},
{"family", "yolo"},
{"classes", Json::array({Json{{"id", 1}, {"name", "Tooth"}}})},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
r.models.clear();
r.models.push_back({"p0", std::vector<uint8_t>(graph.begin(), graph.end())});
const std::string p = (dir / "e2e.ansmodel").string();
CHECK(modelpkg::pack(p, r, err), ("pack e2e: " + err).c_str());
modelpkg::OpenedModel m;
CHECK(modelpkg::OpenModelPackage(p, m, err) && m.family == "yolo10",
"an end-to-end head is read as yolo10 whatever the package says");
CHECK(m.warning.find("end-to-end") != std::string::npos,
"and the reason is said out loud rather than silently swapped");
CHECK(m.legacyMarker.empty() || m.legacyMarker == "tensorrt.txt",
"while the legacy marker is left as it was");
}
// A two-class v8 export is [1, 6, 8400]: a 6 in the shape, not a head that
// has already decoded itself.
{
const std::string graph =
fakeOnnx(pbDim(1) + pbDim(3) + pbDim(640) + pbDim(640),
pbDim(1) + pbDim(6) + pbDim(8400));
modelpkg::PackRequest r = req;
r.manifest = Json{{"task", "detection"},
{"family", "yolo"},
{"classes", Json::array({Json{{"id", 1}, {"name", "Tooth"}}})},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
r.models.clear();
r.models.push_back({"p0", std::vector<uint8_t>(graph.begin(), graph.end())});
const std::string p = (dir / "twoclass.ansmodel").string();
CHECK(modelpkg::pack(p, r, err), ("pack v8: " + err).c_str());
modelpkg::OpenedModel m;
CHECK(modelpkg::OpenModelPackage(p, m, err) && m.family == "yolo",
"a 6 on the wrong axis with thousands of anchors stays plain yolo");
}
}
int main(int argc, char** argv) {
const fs::path dataDir = argc > 1 ? fs::path(argv[1]) : fs::path("data");
const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
const fs::path tmp =
fs::temp_directory_path() / ("anscore-modelpkg-" + std::to_string(stamp));
std::error_code ec;
fs::create_directories(tmp, ec);
testVectors();
testInterop(dataDir);
testRoundTripAndRefusals(tmp);
testEachPackIsSealedAfresh(tmp);
testOpenForAnEngine(dataDir);
testShapeFromTheGraph(tmp);
fs::remove_all(tmp, ec);
std::printf("%d checks, %d failures\n", gChecks, gFailures);
return gFailures == 0 ? 0 : 1;
}
@@ -0,0 +1,9 @@
# The model package format: its primitives against published vectors, a
# package written by ANSMINDCORE, and this implementation's own round trip.
add_executable(ANSModelPackage-UnitTest ANSModelPackage-UnitTest.cpp)
target_link_libraries(ANSModelPackage-UnitTest PRIVATE ANSModelPackage)
add_test(NAME ANSModelPackage
COMMAND ANSModelPackage-UnitTest
${CMAKE_CURRENT_SOURCE_DIR}/data)
@@ -0,0 +1,23 @@
# interop.ansmodel
Written by **ANSMINDCORE's** packer (`src/model/ansmodel.hpp`), opened by
**this** implementation in the unit test. That is the point of it: the two
products must read the same bytes, and a fixture one of them produced is the
only way to prove it.
Keys, so any implementation can reconstruct them:
| Material | Rule |
|---|---|
| base X25519 private key | byte *i* = `i*3 + 9` |
| root Ed25519 seed | byte *i* = `i*7 + 1` |
| release Ed25519 seed | byte *i* = `i*11 + 5` |
| model payload, 12000 bytes | byte *i* = `(i*31 + 21) & 0xff` |
Ids: base key `ansk-v1-base`, root `ansroot-v1`, signer `anssig-2026-09`,
certificate valid until 2030-01-01. Chunk size 4096, so the payload really is
chunked.
To regenerate after a deliberate format change, pack the same manifest from
ANSMINDCORE with those keys and replace this file — and expect both suites to
fail until they agree again, which is the alarm working.
Binary file not shown.
@@ -2,6 +2,8 @@
#include "ANSCUSTOMPY.h" #include "ANSCUSTOMPY.h"
#include "ANSMotionDetector.h" #include "ANSMotionDetector.h"
#include "ANSONNXSAM3.h" #include "ANSONNXSAM3.h"
#include <cstdlib>
#include <iostream>
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
@@ -2235,8 +2237,31 @@ int FireNSmokeTest() {
return 0; return 0;
} }
int main() // RUN A MODEL THIS MACHINE ACTUALLY HAS. Every test below names a path from
// whichever workstation it was written on, so on any other machine the harness
// stops at "Model zip file is not exist" before it ever reaches the GPU. Given
// arguments it runs what it is told instead:
//
// ANSODEngine-UnitTest.exe <model> <video> [engineType] [detectionType]
//
// `model` is a .zip or a sealed .ansmodel — the engine opens either, and a
// package is the only way to see a sealed model run on the GPU. engineType is
// the ModelType enum (3 auto-detects a YOLO export, 30 ONNX YOLO, 16 custom,
// 32 RTMPose); detectionType 1 is object detection.
//
// With no arguments it does exactly what it did before.
int main(int argc, char** argv)
{ {
if (argc >= 3) {
const std::string model = argv[1];
const std::string video = argv[2];
const int engineType = (argc > 3) ? std::atoi(argv[3]) : 3;
const int detectionType = (argc > 4) ? std::atoi(argv[4]) : 1;
std::cout << "model: " << model << "\nvideo: " << video
<< "\nengineType: " << engineType
<< ", detectionType: " << detectionType << std::endl;
return VideoDetectorEngine_CPP(model, video, engineType, detectionType);
}
//FallDetectorTest(); //FallDetectorTest();
//ALPROD_Test(); //ALPROD_Test();
+30 -7
View File
@@ -1,12 +1,35 @@
# ANSCORE Unit Tests # ANSCORE Unit Tests
add_subdirectory(ANSCV-UnitTest) #
add_subdirectory(ANSODEngine-UnitTest) # A test is gated exactly like the code it exercises (see the top-level
add_subdirectory(ANSFR-UnitTest) # CMakeLists): tests for the CUDA-only modules, or that name CUDA::* targets
add_subdirectory(ANSLPR-UnitTest) # themselves, are added only when ANSCORE_HAS_CUDA; tests for the Windows-only
add_subdirectory(ANSOCR-UnitTest) # integrations only on WIN32. Without these gates a non-CUDA configure (macOS,
# any machine with no toolkit) fails at generate time on "links to CUDA::cudart
# but the target was not found", which blocks the cross-platform targets too.
# Cross-platform
add_subdirectory(ANSUtilities-UnitTest) add_subdirectory(ANSUtilities-UnitTest)
add_subdirectory(ANSRabbitMQ-UnitTest) add_subdirectory(ANSRabbitMQ-UnitTest)
add_subdirectory(ANSIO-UnitTest)
add_subdirectory(ANSLS-UnitTest) add_subdirectory(ANSLS-UnitTest)
add_subdirectory(ANSONVIF-UnitTest) add_subdirectory(ANSModelPackage-UnitTest)
add_subdirectory(ANSODTRE-UnitTest) add_subdirectory(ANSODTRE-UnitTest)
# Windows-only — integrations/ANSIO is built on WIN32 only
if(WIN32)
add_subdirectory(ANSIO-UnitTest)
endif()
# CUDA-only — modules/ANSCV, ANSODEngine, ANSOCR, ANSFR and ANSLPR
if(ANSCORE_HAS_CUDA)
add_subdirectory(ANSCV-UnitTest)
add_subdirectory(ANSODEngine-UnitTest)
add_subdirectory(ANSFR-UnitTest)
add_subdirectory(ANSLPR-UnitTest)
add_subdirectory(ANSOCR-UnitTest)
# integrations/ANSONVIF itself uses no CUDA — the test does, because it
# drives capture through ANSCV. Hence both gates.
if(WIN32)
add_subdirectory(ANSONVIF-UnitTest)
endif()
endif()