Author SHA1 Message Date
nghianguyenandClaude Opus 5.5 0baca1b20d feat(ANSODEngine): run Darknet/YOLO exports ("darknet" family) in ONNXYOLO and RTYOLO
A Darknet/YOLO model exported by hank-ai's darknet_onnx_export (v5.1+) has
two outputs: confs [B,N,nc], already objectness x class probability, and
boxes [B,N,1,4], corners x1,y1,x2,y2 normalized to the input. It trains and
predicts on a stretched square and has no NMS in the graph. ONNXYOLO took
the 4-D boxes tensor for segmentation proto masks and letterboxed the input,
so such a model decoded as garbage; a sealed package named as YOLOV4 went to
YOLOOD, which cannot open an .ansmodel at all.

- ONNXYOLO / RTYOLO: recognise the layout once at load (package family
  "darknet", output names confs/boxes, or their shapes), before the seg
  check; stretch-resize the input (single, batch, and RTYOLO's NV12 path);
  new postprocessDarknet: best class per candidate, threshold, stretch map
  to the original image, clip, class-aware NMS.
- dllmain: a sealed package passed as modelType 1 (YOLOV4) is routed as
  detection (3), i.e. to RTYOLO on NVIDIA and ONNXYOLO elsewhere, in
  CreateANSODHandle, CreateANSODHandleEx and OptimizeModelStr.
- Every other model is unchanged: all new branches are gated on the layout.

Verified on macOS only: ANSONNXYOLO.cpp passes a syntax check, and the new
decode functions, extracted into a standalone harness running the model
through onnxruntime, reproduce Darknet's own detections on dog.jpg. NOT yet
built on Windows, and the TensorRT path has not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 18:43: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 5 86b520d85c ANSAWSS3: recover from dead sockets, and report why an upload failed
Field logs showed clusters of uploads taking exactly 20004/20007/20003 ms —
the default _idleTimeoutMs — across several workers at the same instant, then
recurring. Cause: a connection whose request failed was returned to the pool,
so a socket silently dropped by a middlebox was handed to worker after worker,
each paying the full idle timeout to rediscover it. Chilkat's autoReconnect
only covers a cleanly closed connection, not a half-open one.

Connection handling:
- DiscardConnection(): a TRANSPORT failure closes the connection instead of
  pooling it (23 sites). An HTTP error status is not a transport failure - the
  server answered - so those connections are still reused.
- Pooled connections idle longer than 15 s are dropped at acquire time.
- First request on a reused connection uses a short idle timeout (a quarter of
  the configured one, 5 s by default); a fresh connection keeps the full one.
  A dead socket now costs ~5 s once, not 20 s repeatedly.
- One upload call is capped at 30 s wall-clock. The attempt count alone was not
  a time bound: 20 attempts x 20 s could block the caller for ~400 s.
- RetryLoop no longer gives up after one failed connect. Callers call Connect()
  once at startup, so returning left the handle dead for the process lifetime.
  Now backs off 3 s -> 30 s until connected or stopped.
- Connections carry the config generation they were built against, so a connect
  completing just after SetAuthentication cleared the pool cannot leave an
  unauthenticated connection behind (those uploads would 403).
- Stale per-request state is cleared on acquire: multipart uploadId/partNumber
  query params, and the gzip Content-Encoding set by UploadTextData. Not
  ClearAllHeaders - that wipes SigV4 state (fcc985e).

Return codes - upload exports now say whether a retry is worthwhile:
-20 permanent (bad argument, licence, or HTTP 4xx), -21 not connected yet,
-22 transient. Recorded per thread, since one handle is shared by many workers.
Success stays the only positive value, so rc == 1 and rc != 1 are unchanged.
Handle and exception codes (-1/-3/-4) made consistent across all upload
wrappers; some previously returned 0 for these.

Also fixes a misclassification this surfaced: Chilkat reports failure for an
HTTP 4xx delivered during an Expect: 100-continue exchange, so NoSuchBucket was
treated as a network error and retried 20 times before reporting "transient".
The failure path now checks the response status. Chilkat's ~2 KB error dump per
failure is compressed to one line carrying the actual S3 error code.

Verified against live S3: 14,701 uploads across 15 threads with a 150 s idle
gap, 0 failures, no call over 5 s (avg 90 ms, p95 136 ms), matching the
pre-change baseline; and all six return-code classes confirmed end to end.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-12 21:24:12 +10:00
nghianguyenandClaude Fable 5 6f1d069d14 anslicensing: Linux hardware id uses PHYSICAL NICs only
The id is built from up to 6 enumerated MACs. On Linux getifaddrs() also
returns virtual interfaces — docker0 and the veth* pairs each container
creates, tun*/wg* from a VPN, br-*/virbr* bridges. Container veths carry
RANDOM MACs and appear/disappear with the containers, so the enumerated set
can differ between calls: the hardware id changes and an activated licence
silently stops matching its machine. Boxes running Docker (the Coder feature)
would hit this routinely.

This matters more on Linux than elsewhere because two of the four id
components are already constant there: the system-UUID slot is a literal and
GetHDDPropertyList is an empty function on non-Windows. The id rests on CPU id
+ MACs, so unstable MACs are most of the instability.

Filter mirrors what the Windows branch already does via WMI (PNPDeviceId LIKE
%PCI% — physical adapters only). The test is /sys/class/net/<name>/device,
the kernel's own record of a hardware-backed interface, so it stays correct as
new virtual-interface flavours appear; a name-prefix denylist is kept only for
environments without /sys. MACs are bucketed during the walk and chosen after
it, so a veth appearing earlier in the list cannot consume a slot and push a
real NIC out.

FAIL-SAFE: if no physical NIC is found, fall back to the unfiltered set. An
empty list would drop all MAC entropy and — with the UUID and disk slots
already constant — leave near-identical ids across different machines.

Windows and macOS/BSD paths are untouched (both #else branches preserve the
existing behaviour exactly). Logic verified against a simulated Docker host:
enp3s0/wlp2s0 accepted; lo, docker0, veth*, br-*, tun0 rejected.

ORDERING: this CHANGES the id on any Linux box that has virtual interfaces, so
it must land BEFORE the first Linux licences are issued — afterwards it would
invalidate them.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-28 19:34:26 +10:00
nghianguyenandClaude Fable 5 4f392ce95e anslicensing: fix 3-byte heap overflow in hardware-id BitStream3
GetCurrentHardwareId sized its bit stream at 125 bits (16 bytes) but
writes 147 (3 version + 9 x 16-bit hashes), so the last two hash slots
overflowed the heap block by 3 bytes on every id computation. The
Windows heap absorbed it silently for years; it SIGSEGVs under macOS
malloc — found when ANSGateway enabled this code on POSIX (fixed there
in ANSGateway 3a34c68/6c3cdfd; this is the upstream port).

Two parts, both id-preserving (only the first 16 bytes feed the base32
encode — verified byte-identical ids before/after in the gateway):
- hwid.cpp: size the stream to the 147 bits actually written.
- BitStream3: bounds-safe byteAt/setByte accessors on every Write/Read,
  Attach records the real buffer size, default ctor zero-inits — an
  under-sized stream can no longer corrupt the heap, and out-of-range
  reads return 0 (identical to the base32 padding zeros the Match path
  always read there).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-27 09:29:05 +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
nghianguyenandClaude Fable 5 ac79e51d17 ANSCV test: camera-dead watchdog with ReconnectRTSP (production parity)
Overnight soak revealed the gap: ANSRTSPClient does not auto-reconnect
by design (the application layer polls staleness and calls
ReconnectRTSP - ANSVIS/LabVIEW does this in production), but the unit
test never did, so when the laptop's Modern Standby killed the TCP
session at ~1 AM the client returned the cached frame until morning.

Poll GetRTSPLastFrameAgeMs each loop; at 20 s without a decoded frame,
call ReconnectRTSP (timestamped console line), retrying at most every
15 s. Threshold sits above the corruption keyframe gate's ~12 s
worst-case hold so a reconnect never preempts an in-flight recovery.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 10:42:31 +10:00
nghianguyenandClaude Fable 5 7909c8db2b RTP: resume at access-unit boundary after packet loss, not NAL start
After a sequence gap the H.264/H.265 depacketizers resumed reassembly at
the next NAL/FU start. On multi-slice streams every slice starts a NAL,
so reassembly resumed MID-FRAME and delivered access units missing their
leading slices — manufacturing "First slice in a frame missing" /
"Ignoring POC change between slices" / "Could not find ref" decoder
errors and green/blocky concealment output far beyond the actual loss.

Resume only at a true AU boundary instead:
  H.265 - non-VCL NAL (type >= 32) or VCL slice with
          first_slice_segment_in_pic_flag set (first bit after the
          2-byte NAL header)
  H.264 - non-VCL NAL (type >= 6) or VCL slice with
          first_mb_in_slice == 0 (ue(v): MSB set after 1-byte header)

Also: stderr visibility for RTP sequence gaps (rtp_rx logs went only to
the sys_log file) and for av_log-bridge latches in video_decoder — the
global av_log level filters decoder errors from the console, which made
keyframe-gate arms look causeless during diagnosis.

Live verification, HEVC camera on lossy Wi-Fi (NVDEC decode):
  before: 271 decoder errors / 3 min, delivery collapsed to ~2 fps
  after:  3 errors / 2 min, full ~21 fps, zero corrupt frames displayed
Remaining rare errors show POC ref misses with ZERO RTP sequence gaps,
i.e. pre-decode queue eviction under TCP burst clumps - a separate,
much smaller issue absorbed invisibly by the keyframe gate.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 06:09:37 +10:00
nghianguyenandClaude Fable 5 b341392e0f video_decoder: console visibility for keyframe gate arm/release
log_print writes to the sys_log file (unopened in unit tests / field
debugging), so the gate looked inert next to FFmpeg's stderr error lines.
Emit arm/release one-liners to stderr alongside them.

Live verification during a severe Wi-Fi loss burst (271 HEVC errors /
3 min, NVDEC path): gate armed 19x, released 17x, dropping 29-118
corrupt frames per burst and resuming on the next clean keyframe.
The av_log bridge (273dd44) is confirmed working end-to-end.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 05:40:34 +10:00
nghianguyenandClaude Fable 5 273dd44a6d video_decoder: av_log bridge — corruption detection for hwaccel decode
The corruption re-arm added in 6344062 keyed off AVFrame::decode_error_flags,
but the HEVC decoder on the hwaccel (NVDEC) path never sets it: 120 s of live
Wi-Fi packet loss produced 10+ "First slice in a frame missing" / "Could not
find ref with POC n" log bursts with decode_error_flags == 0 on every frame,
so the keyframe gate never fired.

Bridge: install a process-wide av_log callback (chains to
av_log_default_callback) that maps the logging AVCodecContext back to its
CVideoDecoder and latches a per-decoder m_bCorruptSeen flag on AV_LOG_ERROR
messages. readFrame() consumes the latch alongside decode_error_flags to
re-arm the keyframe gate. With hwaccel the decoder is single-threaded, so
the logging context is exactly our AVCodecContext; SW frame-threaded paths
log from per-thread clones but set decode_error_flags instead, which the
existing check covers.

Registered after avcodec_open2, unregistered in uninit() before the context
is freed. Map lookup only on error-level messages (rare). Also promote the
gate-arm message to log_print(HT_LOG_WARN) for console visibility.

Clean-path verified live: 6105 frames / 5 min NVDEC RTSP with zero errors
and no regressions. Loss-path verification pending a naturally lossy window.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 05:01:22 +10:00
nghianguyenandClaude Fable 5 6344062179 MediaClient: keyframe gating (startup + corruption re-arm), libyuv NV12 path
Restores the previously shelved decoder/player hardening and extends it:

1. Startup keyframe gating (video_decoder): after (re)start the decoder
   joins the stream mid-GOP; frames referencing unseen pictures render as
   green/blocky concealment garbage. Drop everything until the first IDR
   (300-frame safety valve so unmarked streams can't blank forever).

2. NEW - corruption re-arm: on lossy links (Wi-Fi cameras) dropped slices
   surface as "First slice in a frame missing" / "Could not find ref with
   POC n" and FFmpeg marks the output via AVFrame::decode_error_flags.
   Re-arm the keyframe gate and drop the rest of the GOP so the display
   holds the last clean frame instead of showing concealment artifacts;
   corrupt drops count toward the safety valve.

3. libyuv NV12->BGR conversion (video_player): honors NVDEC/D3D11VA padded
   strides (1080->1088 rows) that cv::cvtColorTwoPlane mishandled (green
   cast + striped noise), picks BT.601/709/2020 coefficients from the
   frame's signalled colorspace, and is ~3-5x faster than sws_scale.

Verified live against an RTSP HEVC camera (NVDEC path): steady frame flow,
no regressions on the clean-stream path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 21:49:22 +10:00
nghianguyenandClaude Fable 5 044fedd344 ANSCV test: pump GUI messages while waiting for frames
The empty-frame path slept 1 s without calling cv::waitKey, so the
HighGUI window stopped pumping Windows messages and went "Not
Responding" whenever the RTSP stream stalled for a few seconds.
Wait via cv::waitKey(1000) instead (ESC still exits). Same fix in
all three test loops that shared the pattern.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 21:39:39 +10:00
nghianguyenandClaude Fable 5 005f3bc3d5 ANSCV tests: update RTSP camera address, drop unused ans_imshow include
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 20:23:31 +10:00
nghianguyenandClaude Fable 5 13180921c9 Pool dispatch: prefer slot on the GPU where inputs already reside
Two-pass slot acquisition in runInferenceFromPool: Pass A picks an idle
slot whose deviceIndex matches the input GpuMats' resident device
(zero-copy dispatch); Pass B falls back to plain round-robin, where
runInference migrates the inputs via the pinned-host two-hop copy.

Callers that prepare frames on GPU A no longer pay the ~1 ms/frame PCIe
migration when a same-GPU slot is idle, while spillover to the other GPU
under load still works. Input residency is queried outside the slot
mutex via cudaPointerGetAttributes (cheap driver lookup).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 20:05:00 +10:00
nghianguyenandClaude Fable 5 a4ea532f36 Fix multi-GPU inference on Blackwell (dual RTX 5080): four stacked fixes
Verified on dual RTX 5080: 355/355 detections, both GPUs in round-robin,
zero CUDA errors. Requires TensorRT >= 10.16.1 in SharedCore (10.15.1 has
a Myelin l2cm crash when creating a context on a second Blackwell device).

1. blobFromGpuMats: thread_local GpuMat caches (tl_blob/tl_floatImg) were
   device-blind — a pool thread warming GPU[1] then GPU[0] reused device-1
   memory with a device-0 stream, causing an async illegal memory access
   that poisoned every CUDA context (sticky 700). Track owning device and
   rebuild the cache on device switch.

2. runInference: pool round-robin can dispatch to a slot on a different
   GPU than the one the caller prepared inputs on. Detect foreign input
   GpuMats via cudaPointerGetAttributes and migrate them through a pinned
   host staging buffer (two-hop D2H+H2D; direct cross-device cudaMemcpy2D
   is unsupported and GeForce has no P2P).

3. warmUp: now pins its own CUDA device before uploading dummy inputs and
   returns success/failure; loadNetwork rejects engines whose warmup fails
   so broken slots can never join the pool silently.

4. Blackwell builder workaround: setMaxAuxStreams(0) on SM 12.x plus an
   .aux0 engine-cache suffix so pre-workaround caches rebuild.

Also: ANSFR-UnitTest gains --gpu N flag (sets CUDA_VISIBLE_DEVICES) for
single-GPU bisection, and rich diagnostics on input-copy failures.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 20:00:19 +10:00
nghianguyen 43ebd25d07 Unitiest 2026-06-23 05:56:06 +10:00
nghianguyenandClaude Opus 4.8 eb29945cb7 ANSCV tests: add ans_imshow include + headless ConsoleTest
Add ConsoleTest.cpp, a headless (no-GUI) smoke test for ANSOPENCV image
ops plus an optional RTSP capture path, built as its own executable
(ANSCV-ConsoleTest) so it does not collide with ANSCV-UnitTest's GUI main.

Make the ans_imshow.hpp include (shipped with ANSMIND, outside the repo)
configurable via the ANSMIND_INCLUDE_DIR cache variable, defaulting to
C:/ProgramData/ANSCENTER/ANSMIND. Warn rather than fail configure when the
header is absent so unrelated targets still build.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 05:51:01 +10:00
nghianguyen cb3a32cdfb Merge remote-tracking branch 'ANSCORE/main' 2026-06-18 05:55:50 +10:00
nghianguyen b3436a61ee Prevent OOM 2026-06-18 05:55:31 +10:00
110 changed files with 13817 additions and 924 deletions
-238
View File
@@ -1,238 +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)"
]
}
}
+8 -1
View File
@@ -6,6 +6,7 @@ Release/
out/
# CLion / CMake
build/
.idea/
cmake-build-debug/
cmake-build-release/
@@ -41,4 +42,10 @@ ipch/
# Windows
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
+41 -6
View File
@@ -1,5 +1,11 @@
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) ───────────────────
include(CheckLanguage)
@@ -9,11 +15,22 @@ if(CMAKE_CUDA_COMPILER)
set(ANSCORE_HAS_CUDA ON)
set(CMAKE_CUDA_STANDARD 20)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 88 89 90 100)
# SASS (-real) for each shipping arch from Turing (sm_75) through Blackwell
# consumer (sm_120), plus compute_75 PTX (-virtual) so any newer/unlisted GPU
# JIT-compiles at first run (forward compatibility). NOTE: this only governs
# this project's own .cu files; prebuilt deps (odhublib, TensorRT, ORT, cuBLAS)
# carry their own arch coverage independently.
set(CMAKE_CUDA_ARCHITECTURES
75-real 80-real 86-real 87-real 88-real 89-real 90-real 100-real 120-real
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)
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()
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use_fast_math")
endif()
else()
project(ANSCORE LANGUAGES C CXX)
@@ -36,6 +53,12 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# ANSCORE uses no C++20 modules. CMake auto-enables module dependency scanning
# for C++20 + Ninja, which adds a fragile per-TU scan/dyndep (.ddi/.modmap) step
# that can fail (empty .ddi -> "cannot open ...modmap" / dyndep parse errors),
# especially after an interrupted build. Disable it: faster builds, no dyndep.
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
# Output all binaries to a single directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
@@ -141,7 +164,12 @@ include(cmake/Dependencies.cmake)
# Core — licensing & infrastructure
add_subdirectory(core/anslicensing)
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)
add_subdirectory(modules/ANSMOT)
@@ -164,11 +192,18 @@ if(ANSCORE_HAS_CUDA)
add_subdirectory(modules/ANSOCR)
add_subdirectory(modules/ANSFR)
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()
add_subdirectory(modules/ANSCV)
# Facade
add_subdirectory(ANSLIB)
# 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)
+174
View File
@@ -9,10 +9,76 @@
#include "ANSLicense.h" // ANS_DBG macro (gated by ANSCORE_DEBUGVIEW)
#include <cstdarg>
#include <mutex>
#include <unordered_map>
extern "C" {
#include "libavutil/imgutils.h"
#include "libavutil/buffer.h"
#include "libavutil/mem.h"
#include "libavutil/log.h"
}
// ---------------------------------------------------------------------------
// FFmpeg error-log bridge — corruption detection for hwaccel decode.
//
// On slice loss the HEVC decoder logs "First slice in a frame missing" /
// "Could not find ref with POC n" at AV_LOG_ERROR, but with hwaccel (NVDEC)
// it does NOT set AVFrame::decode_error_flags — verified empirically — so
// frame-level inspection cannot see the corruption. This bridge installs a
// process-wide av_log callback that maps the logging AVCodecContext back to
// its CVideoDecoder and latches a per-decoder corruption flag, which
// readFrame() consumes to re-arm the keyframe gate.
//
// Notes: with hwaccel the decoder runs single-threaded, so the logging ctx
// is exactly our AVCodecContext (SW frame-threading logs from per-thread
// clones and would not match — those paths set decode_error_flags instead).
// The callback chains to av_log_default_callback so console output remains.
// ---------------------------------------------------------------------------
namespace {
std::mutex s_logBridgeMutex;
std::unordered_map<void*, std::atomic<bool>*> s_logBridgeMap; // AVCodecContext* → corrupt flag
void ansAvLogBridge(void* ptr, int level, const char* fmt, va_list vl)
{
if (ptr && level <= AV_LOG_ERROR) {
bool latched = false;
{
std::lock_guard<std::mutex> lk(s_logBridgeMutex);
auto it = s_logBridgeMap.find(ptr);
if (it != s_logBridgeMap.end()) {
it->second->store(true, std::memory_order_relaxed);
latched = true;
}
}
if (latched) {
// Show exactly which decoder-context error latched the corrupt
// flag — av_log_default_callback may filter it out (global log
// level), which would otherwise make gate arms look causeless.
va_list vc;
va_copy(vc, vl);
fprintf(stderr, "[FFmpeg-latch] ");
vfprintf(stderr, fmt, vc);
va_end(vc);
}
}
av_log_default_callback(ptr, level, fmt, vl);
}
void registerLogBridge(void* codecCtx, std::atomic<bool>* corruptFlag)
{
static std::once_flag s_installOnce;
std::call_once(s_installOnce, []() { av_log_set_callback(ansAvLogBridge); });
std::lock_guard<std::mutex> lk(s_logBridgeMutex);
s_logBridgeMap[codecCtx] = corruptFlag;
}
void unregisterLogBridge(void* codecCtx)
{
std::lock_guard<std::mutex> lk(s_logBridgeMutex);
s_logBridgeMap.erase(codecCtx);
}
}
// ---------------------------------------------------------------------------
@@ -428,6 +494,10 @@ void CVideoDecoder::uninit()
m_pContext->hw_device_ctx = NULL;
}
// Stop the av_log bridge from touching this decoder's flag BEFORE the
// context is destroyed (the map key is the context pointer).
unregisterLogBridge(m_pContext);
// FIXED: Close codec before freeing context
avcodec_close(m_pContext);
avcodec_free_context(&m_pContext);
@@ -602,6 +672,13 @@ BOOL CVideoDecoder::init(enum AVCodecID codec, uint8* extradata, int extradata_s
return FALSE;
}
// Route this context's FFmpeg error logs to our corruption flag so the
// keyframe gate can react to slice loss on the hwaccel path (which never
// sets decode_error_flags). Unregistered in uninit() before the context
// is freed.
m_bCorruptSeen.store(false, std::memory_order_relaxed);
registerLogBridge(m_pContext, &m_bCorruptSeen);
// Debug: one-shot visibility into which decoder actually got opened.
// m_bHardwareDecoderEnabled is set by hwDecoderInit() on success; when
// hwMode == HW_DECODING_DISABLE or hwDecoderInit failed, it stays FALSE
@@ -761,6 +838,10 @@ void CVideoDecoder::Start() {
if (m_pContext) {
avcodec_flush_buffers(m_pContext);
}
// Re-arm keyframe gating: after a flush the DPB is empty, so wait for the
// next IDR before delivering frames again (avoids post-restart green/blocky).
m_bWaitKeyframe = true;
m_nKeyframeWaitCount = 0;
m_bRunning = TRUE;
log_print(HT_LOG_INFO, "%s, Video decoder started\r\n", __FUNCTION__);
}
@@ -1093,6 +1174,99 @@ BOOL CVideoDecoder::readFrame()
continue;
}
// Mid-stream corruption gating — re-arm the keyframe gate on decode errors.
//
// Packet loss on lossy links (Wi-Fi cameras) drops slices mid-GOP:
// FFmpeg logs "First slice in a frame missing" / "Could not find ref
// with POC n" and conceals the damage. Every subsequent frame in the
// GOP references the concealed one, so rather than display green/
// blocky garbage until the next IDR, drop the frame and re-arm the
// keyframe gate — the display holds the last clean frame and resumes
// on the next clean keyframe.
//
// Two detection signals, both required:
// • decode_error_flags — set by SW decode paths (H.264 always,
// HEVC frame-threaded).
// • m_bCorruptSeen — latched by the av_log bridge; the ONLY signal
// on the hwaccel (NVDEC) path, which logs the error but leaves
// decode_error_flags at 0 (verified empirically).
const bool corruptByFlags = (m_pFrame->decode_error_flags != 0);
const bool corruptByBridge = m_bCorruptSeen.exchange(false, std::memory_order_relaxed);
const bool frameCorrupt = corruptByFlags || corruptByBridge;
if (frameCorrupt) {
if (!m_bWaitKeyframe.load(std::memory_order_relaxed)) {
m_bWaitKeyframe.store(true, std::memory_order_relaxed);
m_nKeyframeWaitCount = 0;
log_print(HT_LOG_WARN,
"%s, corrupt frame (decode_error_flags=0x%x) -- gating until next keyframe\r\n",
__FUNCTION__, m_pFrame->decode_error_flags);
// Also to stderr so it appears next to FFmpeg's own [hevc @ ...]
// error lines (log_print goes to the log file, which unit tests
// and field debugging sessions usually don't have open).
fprintf(stderr, "[MediaClient] corrupt frame detected (flags=0x%x bridge=%d) -- gating until next keyframe\n",
m_pFrame->decode_error_flags, (int)corruptByBridge);
ANS_DBG("MEDIA_Decode",
"corrupt frame (decode_error_flags=0x%x) — re-arming keyframe gate",
m_pFrame->decode_error_flags);
} else {
// Count corrupt drops toward the safety valve so a stream that
// is corrupt for a long stretch eventually shows *something*
// (concealed frames) instead of freezing indefinitely.
++m_nKeyframeWaitCount;
}
if (m_nKeyframeWaitCount < 300) {
av_frame_unref(m_pFrame);
continue;
}
// Safety valve tripped: fall through and let the frame pass
// (concealment artifacts beat a permanently frozen display).
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
}
// Startup keyframe gating — discard everything until the first IDR.
//
// On (re)connect we join the stream mid-GOP. Until a keyframe arrives,
// every frame references pictures we never received; the decoder (and
// NVDEC especially) conceals them into green / blocky garbage and reports
// success, so there is no error flag to test. Waiting for the first
// keyframe gives a clean intra reference, after which inter frames decode
// correctly. This is the standard RTSP "wait for IDR" behaviour.
//
// Keyframe flag is set from the bitstream parse and is reliable for both
// SW and HW decode (it does not depend on the HW surface). pict_type is a
// fallback. The counter is a safety valve: if a stream never advertises a
// keyframe within ~300 frames (~10 s @30fps) we stop gating rather than
// blank the display forever.
if (m_bWaitKeyframe.load(std::memory_order_relaxed)) {
// pict_type==I is version-independent and set for HEVC IDR/I frames
// on both SW and HW decode. AV_FRAME_FLAG_KEY (FFmpeg 5.1+) is added
// when available for robustness; guarded so it also builds against
// the older vendored headers that predate that flag.
bool isKey = (m_pFrame->pict_type == AV_PICTURE_TYPE_I);
#if defined(AV_FRAME_FLAG_KEY)
isKey = isKey || ((m_pFrame->flags & AV_FRAME_FLAG_KEY) != 0);
#endif
if (isKey) {
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
ANS_DBG("MEDIA_Decode",
"keyframe acquired after %d dropped frame(s) — resuming output",
m_nKeyframeWaitCount);
if (m_nKeyframeWaitCount > 0) {
fprintf(stderr, "[MediaClient] keyframe acquired after %d dropped frame(s) -- resuming\n",
m_nKeyframeWaitCount);
}
} else if (++m_nKeyframeWaitCount >= 300) {
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
ANS_DBG("MEDIA_Decode",
"no keyframe after %d frames — releasing gate (safety valve)",
m_nKeyframeWaitCount);
} else {
// Pre-IDR frame: drop it (would render as green/blocky garbage).
av_frame_unref(m_pFrame);
continue;
}
}
if (m_pFrame->format == m_hwPixFmt)
{
// CUDA HW accel: clone the HW frame BEFORE transfer so inference
+13
View File
@@ -165,6 +165,19 @@ private:
std::atomic<BOOL> m_bRunning;
BOOL m_bHardwareDecoderEnabled; // Track if hardware decoder is enabled
bool m_bCudaHWAccel; // true when using AV_HWDEVICE_TYPE_CUDA
// Startup keyframe gating. On connect/reconnect we join the stream mid-GOP,
// before the first IDR, so early frames reference data we never received and
// the decoder conceals them (green / blocky macroblocks). We drop everything
// until the first keyframe (clean reference point), then deliver normally.
// The frame counter is a safety valve so a stream that never advertises a
// keyframe can't blank the display forever.
std::atomic<bool> m_bWaitKeyframe{ true }; // true until first IDR after (re)start
int m_nKeyframeWaitCount{ 0 }; // frames dropped while waiting
// Latched by the av_log bridge (video_decoder.cpp) when FFmpeg logs a decode
// error for this decoder's AVCodecContext. Needed because hwaccel (NVDEC)
// decode does NOT set AVFrame::decode_error_flags on slice loss — the log
// is the only corruption signal on that path. Consumed by readFrame().
std::atomic<bool> m_bCorruptSeen{ false };
int m_hwGpuIndex; // GPU index assigned by HWDecoderPool (-1 = legacy)
AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference
const AVCodec* m_pCodec;
+82 -3
View File
@@ -1405,6 +1405,80 @@ cv::Mat CVideoPlayer::avframeNV12ToCvMat(const AVFrame* frame)
m_nv12OrigWidth = width;
m_nv12OrigHeight = height;
// Preferred path: libyuv NV12→RGB24 (BGR in memory, matches cv::Mat).
// SIMD (SSSE3/AVX2) and ~3-5x faster than sws_scale's quality config.
// Crucially it honors src_stride_uv, so the padded strides/height that
// NVDEC and D3D11VA emit (e.g. 1080→1088 rows, aligned strides) convert
// correctly — that padding is what cv::cvtColorTwoPlane mishandled,
// reading the chroma plane at the wrong offset and rendering a green
// cast with striped noise toward the bottom of the frame.
// Coefficients are chosen from the frame's signalled colorspace/range,
// mirroring initNV12SwsContext() (BT.709 for HD/4K, BT.601 for SD,
// BT.2020 if tagged). The constants map studio-range YUV to full-range
// BGR, so the result is display-ready — no manual range expansion.
#if defined(ANSCORE_HAS_LIBYUV) && ANSCORE_HAS_LIBYUV
{
const bool fullRange = (frame->color_range == AVCOL_RANGE_JPEG);
const libyuv::YuvConstants* yuvc;
switch (frame->colorspace) {
case AVCOL_SPC_BT470BG:
case AVCOL_SPC_SMPTE170M:
yuvc = fullRange ? &libyuv::kYuvJPEGConstants : &libyuv::kYuvI601Constants;
break;
case AVCOL_SPC_BT2020_NCL:
case AVCOL_SPC_BT2020_CL:
yuvc = fullRange ? &libyuv::kYuvV2020Constants : &libyuv::kYuv2020Constants;
break;
case AVCOL_SPC_BT709:
yuvc = fullRange ? &libyuv::kYuvF709Constants : &libyuv::kYuvH709Constants;
break;
default:
// Auto-detect by resolution (matches initNV12SwsContext()).
if (width >= 1280 || height >= 720)
yuvc = fullRange ? &libyuv::kYuvF709Constants : &libyuv::kYuvH709Constants;
else
yuvc = fullRange ? &libyuv::kYuvJPEGConstants : &libyuv::kYuvI601Constants;
break;
}
cv::Mat bgrImage(height, width, CV_8UC3);
int ret = libyuv::NV12ToRGB24Matrix(
frame->data[0], frame->linesize[0],
frame->data[1], frame->linesize[1],
bgrImage.data, static_cast<int>(bgrImage.step),
yuvc, width, height);
if (ret == 0) {
return bgrImage;
}
ANS_DBG("MEDIA_Convert",
"libyuv NV12ToRGB24 failed (ret=%d) — falling back to sws_scale", ret);
}
#endif
// Fallback path: sws_scale via the dedicated NV12→BGR24 context. Also
// stride-safe (honors frame->linesize[]) and colorspace/range-correct;
// used on non-libyuv builds or if libyuv ever reports an error.
{
// Dedicated lock (not _mutex) so close()/dtor cannot free the context
// mid-scale, while still keeping onVideoFrame off the hot _mutex.
std::lock_guard<std::mutex> nv12Lock(m_nv12SwsMutex);
initNV12SwsContext(frame);
if (m_nv12SwsCtx) {
cv::Mat bgrImage(height, width, CV_8UC3);
uint8_t* dst[1] = { bgrImage.data };
int dstStride[1] = { static_cast<int>(bgrImage.step[0]) };
int ret = sws_scale(m_nv12SwsCtx, frame->data, frame->linesize, 0,
height, dst, dstStride);
if (ret > 0) {
return bgrImage;
}
ANS_DBG("MEDIA_Convert",
"NV12 sws_scale failed (ret=%d) — falling back to cvtColorTwoPlane", ret);
}
}
// Fallback: OpenCV two-plane conversion. Correct only when the planes
// are unpadded (linesize == width); kept for builds/inputs where the
// sws context could not be created.
cv::Mat yPlane(height, width, CV_8UC1, frame->data[0], frame->linesize[0]);
cv::Mat uvPlane(height / 2, width / 2, CV_8UC2, frame->data[1], frame->linesize[1]);
@@ -2200,9 +2274,14 @@ CVideoPlayer::~CVideoPlayer()
sws_freeContext(swsCtx);
swsCtx = nullptr;
}
if (m_nv12SwsCtx != nullptr) {
sws_freeContext(m_nv12SwsCtx);
m_nv12SwsCtx = nullptr;
{
// Lock order: _mutex (held) → m_nv12SwsMutex. Protects against a
// consumer thread still inside avframeNV12ToCvMat's sws_scale.
std::lock_guard<std::mutex> nv12Lock(m_nv12SwsMutex);
if (m_nv12SwsCtx != nullptr) {
sws_freeContext(m_nv12SwsCtx);
m_nv12SwsCtx = nullptr;
}
}
if (this->_tjInstance) {
tjDestroy(this->_tjInstance);
+7
View File
@@ -428,6 +428,13 @@ protected:
int m_nv12LastWidth = 0, m_nv12LastHeight = 0;
int m_nv12LastColorspace = -1;
int m_nv12LastRange = -1;
// Guards m_nv12SwsCtx create/use/free. The NV12→BGR conversion deliberately
// runs WITHOUT _mutex (see avframeToCvMat, commit "Fix RTSP stall by
// releasing _mutex during BGR conversion"), so this dedicated lock protects
// the context against close()/dtor freeing it mid-sws_scale without
// reintroducing contention with onVideoFrame. Lock order: _mutex → this
// (close()/dtor hold _mutex first); avframeNV12ToCvMat takes only this lock.
std::mutex m_nv12SwsMutex;
void initSwsContext(int width, int height, AVPixelFormat pixFmt, AVPixelFormat outputPixFmt = AV_PIX_FMT_YUVJ420P);
void initNV12SwsContext(const AVFrame* frame);
+22 -7
View File
@@ -197,16 +197,31 @@ BOOL h264_data_rx(H264RXI * p_rxi, uint8 * p_data, int len)
if (p_rxi->rtprxi.rxf_loss)
{
// Packet loss, until the next start packet
if (!beginPacket)
// Packet loss — resume ONLY at an access-unit boundary (see the
// H.265 counterpart in h265_rtp_rx.cpp for the full rationale).
// Resuming at a mere NAL/FU start delivers frames missing their
// leading slices on multi-slice streams. An AU boundary is a
// non-VCL NAL (SEI/SPS/PPS/AUD, type >= 6) or a VCL slice whose
// first_mb_in_slice == 0 — ue(v)-coded, so value 0 is a single
// '1' bit: MSB of the byte after the 1-byte NAL header.
BOOL auStart = FALSE;
if (beginPacket)
{
uint8* payload = headerStart + numBytesToSkip;
int payloadLen = (int)packetSize - (int)numBytesToSkip;
if (payloadLen >= 2)
{
uint8 nt = payload[0] & 0x1F;
if (nt >= 6) auStart = TRUE; // non-VCL
else if (payload[1] & 0x80) auStart = TRUE; // first_mb_in_slice == 0
}
}
if (!auStart)
{
p_rxi->d_offset = 0;
return FALSE;
}
else
{
p_rxi->rtprxi.rxf_loss = 0;
}
p_rxi->rtprxi.rxf_loss = 0;
}
// save the H264 parameter sets
+26 -7
View File
@@ -265,16 +265,35 @@ BOOL h265_data_rx(H265RXI * p_rxi, uint8 * p_data, int len)
if (p_rxi->rtprxi.rxf_loss)
{
// Packet loss, until the next start packet
if (!beginPacket)
// Packet loss — resume ONLY at an access-unit boundary.
//
// The old behaviour resumed at the next NAL/FU start, but with
// multi-slice frames every slice starts a NAL, so reassembly resumed
// MID-FRAME and delivered access units missing their leading slices.
// The decoder then logs "First slice in a frame missing" / "Ignoring
// POC change between slices" and conceals the damage as green/blocky
// garbage. An AU boundary is either a non-VCL NAL (VPS/SPS/PPS/SEI,
// type >= 32, which precede an IDR) or a VCL slice with
// first_slice_segment_in_pic_flag set — the first bit after the
// 2-byte NAL header (H.265 spec 7.3.6.1).
BOOL auStart = FALSE;
if (beginPacket)
{
uint8* payload = headerStart + numBytesToSkip;
int payloadLen = (int)packetSize - (int)numBytesToSkip;
if (payloadLen >= 3)
{
uint8 nt = (payload[0] >> 1) & 0x3F;
if (nt >= 32) auStart = TRUE; // non-VCL (param sets/SEI)
else if (payload[2] & 0x80) auStart = TRUE; // first slice in picture
}
}
if (!auStart)
{
p_rxi->d_offset = 0;
return FALSE;
}
else
{
p_rxi->rtprxi.rxf_loss = 0;
}
p_rxi->rtprxi.rxf_loss = 0;
}
// save the H265 parameter sets
+7 -2
View File
@@ -105,8 +105,13 @@ BOOL rtp_data_rx(RTPRXI * p_rxi, uint8 * p_data, int len)
if (p_rxi->seq && p_rxi->seq != (uint16)(rtpSeq - 1))
{
p_rxi->rxf_loss = 1;
log_print(HT_LOG_WARN, "%s, prev seq[%u], cur seq[%u]!!!\r\n", __FUNCTION__, p_rxi->seq, rtpSeq);
}
log_print(HT_LOG_WARN, "%s, prev seq[%u], cur seq[%u]!!!\r\n", __FUNCTION__, p_rxi->seq, rtpSeq);
// stderr too (log_print goes to the log file): over TCP transport a
// sequence gap means the SENDER (camera) skipped packets — usually
// backpressure from a slow receiver. Gap size shows how much vanished.
fprintf(stderr, "[RTP] seq gap: prev=%u cur=%u (lost ~%u pkt)\n",
p_rxi->seq, rtpSeq, (uint16)(rtpSeq - p_rxi->seq - 1));
}
p_rxi->seq = rtpSeq;
p_rxi->rxf_marker = rtpMarkerBit;
+55 -3
View File
@@ -78,6 +78,30 @@ else()
endif()
target_compile_definitions(onnxruntime INTERFACE ORT_API_MANUAL_INIT)
# ── cuDNN ───────────────────────────────────────────────────────
# TensorRT API headers (engines/TensorRTAPI) include <cudnn_version.h>, so
# cuDNN's include dir must be on the path. cuDNN ships separately from the
# CUDA toolkit, so wire it explicitly rather than relying on it being copied
# into the CUDA install.
if(NOT APPLE)
if(WIN32)
set(CUDNN_INCLUDE_DIR "${ANSLIBS_DIR}/cudnn/include")
set(CUDNN_LIB_DIR "${ANSLIBS_DIR}/cudnn/lib/x64")
else()
set(CUDNN_INCLUDE_DIR "${ANSLIBS_DIR}/cudnn/include")
set(CUDNN_LIB_DIR "${ANSLIBS_DIR}/cudnn/lib")
endif()
add_library(cudnn INTERFACE)
target_include_directories(cudnn INTERFACE ${CUDNN_INCLUDE_DIR})
target_link_directories(cudnn INTERFACE ${CUDNN_LIB_DIR})
if(WIN32)
target_link_libraries(cudnn INTERFACE cudnn.lib)
else()
target_link_libraries(cudnn INTERFACE cudnn)
endif()
message(STATUS "cuDNN: using ANSLibs at ${CUDNN_INCLUDE_DIR}")
endif()
# ── TensorRT (Windows + Linux only) ────────────────────────────
if(NOT APPLE)
set(TENSORRT_INCLUDE_DIR "${ANSLIBS_DIR}/TensorRT/include")
@@ -86,6 +110,9 @@ if(NOT APPLE)
add_library(tensorrt INTERFACE)
target_include_directories(tensorrt INTERFACE ${TENSORRT_INCLUDE_DIR})
target_link_directories(tensorrt INTERFACE ${TENSORRT_LIB_DIR})
# cuDNN is a transitive dependency of the TensorRT API headers/engine,
# so anything linking `tensorrt` also gets cuDNN's headers and import lib.
target_link_libraries(tensorrt INTERFACE cudnn)
if(WIN32)
target_link_libraries(tensorrt INTERFACE nvinfer_plugin_10.lib)
else()
@@ -211,9 +238,34 @@ endif()
# ── Intel MKL ───────────────────────────────────────────────────
add_library(mkl INTERFACE)
if(WIN32)
set(MKL_LIB_DIR "C:/Program Files (x86)/Intel/oneAPI/mkl/2025.0/lib")
target_link_directories(mkl INTERFACE "${MKL_LIB_DIR}")
target_link_libraries(mkl INTERFACE mkl_rt.lib)
# oneAPI installs under .../oneAPI/mkl/<version>/ where <version> varies
# by install (e.g. 2025.0, 2025.3, or a "latest" junction) and the import
# lib lives in either lib/ (newer flat layout) or lib/intel64/ (older).
# Auto-detect rather than hardcoding so it survives MKL version bumps.
set(MKL_LIB_DIR "")
file(GLOB _mkl_versions "C:/Program Files (x86)/Intel/oneAPI/mkl/*")
list(SORT _mkl_versions)
list(REVERSE _mkl_versions) # prefer the newest version dir
foreach(_mkl_root IN LISTS _mkl_versions)
foreach(_sub "lib" "lib/intel64")
if(EXISTS "${_mkl_root}/${_sub}/mkl_rt.lib")
set(MKL_LIB_DIR "${_mkl_root}/${_sub}")
break()
endif()
endforeach()
if(MKL_LIB_DIR)
break()
endif()
endforeach()
if(MKL_LIB_DIR)
target_link_directories(mkl INTERFACE "${MKL_LIB_DIR}")
target_link_libraries(mkl INTERFACE mkl_rt.lib)
message(STATUS "Intel MKL: using ${MKL_LIB_DIR}")
else()
message(WARNING "Intel MKL not found under C:/Program Files (x86)/Intel/oneAPI/mkl. "
"ANSFR (and ANSLIB/ANSFR-UnitTest that link it) will fail to link. "
"Install Intel oneMKL or adjust MKL_LIB_DIR in cmake/Dependencies.cmake.")
endif()
else()
# Try system MKL on Linux
find_library(MKL_RT_LIB mkl_rt PATHS /opt/intel/oneapi/mkl/latest/lib/intel64)
+1 -1
View File
@@ -461,7 +461,7 @@ namespace ANSCENTER {
") returned null — the DLL may be corrupt.");
s_ort_api = api;
Ort::Global<void>::api_ = api;
Ort::InitApi(api);
std::cout << "[EPLoader] ORT loaded successfully." << std::endl;
std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl;
+282 -49
View File
@@ -98,6 +98,16 @@ namespace {
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
// the variable name "privateKey" used at call sites, this is an 11-byte
// SDK token that identifies the ANSCENTER product to the licensing
@@ -690,6 +700,7 @@ struct GpuAdapter {
DWORD vendorId{0};
std::string name; // UTF-8
int64_t dedicatedVramMB{0};
int64_t sharedSysMemMB{0}; // iGPUs report ~0 dedicated; this is their pool
bool isDiscrete{false}; // heuristic: dedicated VRAM ≥ 1 GiB
// running totals — reset every Sample()
@@ -697,6 +708,19 @@ struct GpuAdapter {
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) {
switch (vendorId) {
case 0x10DE: return "NVIDIA";
@@ -737,6 +761,7 @@ std::vector<GpuAdapter> EnumerateGpuAdapters() {
a.vendorId = desc.VendorId;
a.name = WideToUtf8(desc.Description);
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
out.push_back(std::move(a));
}
@@ -747,6 +772,66 @@ std::vector<GpuAdapter> EnumerateGpuAdapters() {
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) {
if (!inst) return false;
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();
ANS_DBG("HWInfo", "Found %zu GPU adapter(s)", gpus.size());
for (auto& gpu : gpus) {
std::string vendor = gpu.vendor();
ANS_DBG("HWInfo", "GPU: name=%s vendor=%s", gpu.name().c_str(), vendor.c_str());
std::cout << "[HWInfo] Detected GPU: " << gpu.name() << " (Vendor: " << vendor << ")" << std::endl;
// Normalize to uppercase for reliable substring matching
std::transform(vendor.begin(), vendor.end(),
vendor.begin(), ::toupper);
auto adapters = EnumerateGpuAdapters();
ANS_DBG("HWInfo", "DXGI: found %zu GPU adapter(s)", adapters.size());
for (const auto& a : adapters) {
ANS_DBG("HWInfo", "GPU: name=%s vendorId=0x%04X vram=%lldMiB discrete=%d",
a.name.c_str(), a.vendorId,
static_cast<long long>(a.dedicatedVramMB), a.isDiscrete ? 1 : 0);
std::cout << "[HWInfo] Detected GPU: " << a.name
<< " (Vendor: " << VendorString(a.vendorId) << ")" << std::endl;
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;
switch (a.vendorId) {
case 0x10DE: gpuNvidiaAvailable = true; break; // NVIDIA
case 0x1002: gpuAMDAvailable = true; break; // AMD / ATI
case 0x1022: gpuAMDAvailable = true; break; // AMD (some APUs)
case 0x8086: gpuOpenVINOAvailable = true; break; // Intel
default: break;
}
}
// DXGI came back empty — no WDDM driver at all, or a session with no
// graphics stack. Fall back to the OpenCL list rather than assuming
// there is no GPU.
if (adapters.empty()) {
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",
cpuIntelAvailable, gpuNvidiaAvailable, gpuAMDAvailable, gpuOpenVINOAvailable);
@@ -1427,6 +1547,27 @@ namespace ANSCENTER
return str.c_str();
}
// Static, handle-free hardware id. Mirrors ANSLSHelper::GetCurrentHardwareId but has no
// instance/_logger, so diagnostics go to DebugView via ANS_DBG. Returns "" on failure.
std::string ANSLicenseHelper::GetHardwareId() {
try {
const wchar_t* hwid = KeyHelper::GetCurrentHardwareId();
std::wstring result_t = std::wstring(hwid);
std::string result = WString2String(result_t);
ANS_DBG("ANSLicenseHelper", "GetHardwareId -> '%s'", result.c_str());
return result;
}
catch (ANSCENTER::Licensing::Exception* ex) {
ANS_DBG("ANSLicenseHelper", "GetHardwareId failed: %s", ex->GetExceptionMessage());
ex->Destroy();
return "";
}
catch (...) {
ANS_DBG("ANSLicenseHelper", "GetHardwareId failed: unknown exception");
return "";
}
}
bool ANSLicenseHelper::isPathSafe(const std::string& pathStr) {
// Basic safety checks - customize as needed
if (pathStr.empty()) return false;
@@ -1636,10 +1777,19 @@ namespace ANSCENTER
registrationName = "ANSTS";
productId = 1008;
}
else {
registrationName = productName;
else if (productName == "ANSMIND") {//1009
registrationName = "ANSMIND";
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);
if (trialLicenseKey.empty()) {
@@ -1708,6 +1858,12 @@ namespace ANSCENTER
case ANSCENTER::ANSPRODUCTS::ANS_TS://1008
_productName = "ANSTS";
break;
case ANSCENTER::ANSPRODUCTS::ANSMIND://1009
_productName = "ANSMIND";
break;
case ANSCENTER::ANSPRODUCTS::ANSGW://1010
_productName = "ANSGW";
break;
}
if (_productName == productName) {
// Delete the license file
@@ -1755,6 +1911,12 @@ namespace ANSCENTER
else if (productName == "ANSTS") {//1008
registrationName = "ANSTS";
}
else if (productName == "ANSMIND") {//1009
registrationName = "ANSMIND";
}
else if (productName == "ANSGW") {//1010
registrationName = "ANSGW";
}
else {
registrationName = productName;
}
@@ -1795,6 +1957,12 @@ namespace ANSCENTER
else if (productName == "ANSTS") {//1008
registrationName = "ANSTS";
}
else if (productName == "ANSMIND") {//1009
registrationName = "ANSMIND";
}
else if (productName == "ANSGW") {//1010
registrationName = "ANSGW";
}
else {
registrationName = productName;
}
@@ -1861,6 +2029,8 @@ namespace ANSCENTER
ansLicenseCollection.push_back("ANSALPR");//1006
ansLicenseCollection.push_back("ANSCV");//1007
ansLicenseCollection.push_back("ANSTS");//1008
ansLicenseCollection.push_back("ANSMIND");//1009
ansLicenseCollection.push_back("ANSGW");//1010
for (auto& element : ansLicenseCollection) {
boost::property_tree::ptree dataNode;
@@ -1923,6 +2093,12 @@ namespace ANSCENTER
case ANSCENTER::ANSPRODUCTS::ANS_TS:
_productName = "ANSTS";
break;
case ANSCENTER::ANSPRODUCTS::ANSMIND:
_productName = "ANSMIND";
break;
case ANSCENTER::ANSPRODUCTS::ANSGW:
_productName = "ANSGW";
break;
}
if (_productName == element) {
_registrationName = ANSUtility::DecodeBase64Utility(_validationData.c_str());// Decode
@@ -2000,10 +2176,18 @@ namespace ANSCENTER
registrationName = "ANSTS";
productId = 1008;
}
else {
registrationName = ansLicenseInfo.productName;
else if (ansLicenseInfo.productName == "ANSMIND") {
registrationName = "ANSMIND";
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);
std::string trialLicenseKey = GetTrialLicenseKey(registrationName);
if (trialLicenseKey.empty()) {
@@ -2145,7 +2329,7 @@ namespace ANSCENTER
}
void ANSLSHelper::LoadLicense(std::string licenseKey) {
if (!this->_license != NULL) delete this->_license;
if (this->_license != NULL) delete this->_license;
this->_license = new License();
std::string licenseFileName = Dash2Underscore(licenseKey)+".json";
std::string licenseFolder = GetLicenseDir();
@@ -2791,6 +2975,9 @@ namespace ANSCENTER
std::wstring _registrationName = String2WString(registrationName);
std::wstring hardwareId = KeyHelper::GetCurrentHardwareId();
std::string _hardwareId = WString2String(hardwareId);
if (_hardwareId == kEmptyFingerprintHWID) {
this->_logger.LogWarn("ANSLSHelper::ValidateLicenseKey", kEmptyFingerprintWarning, __FILE__, __LINE__);
}
bool _overrideLicenseFile = false;
if (activationKey.empty()) {
if (this->_license != NULL) {// get it from there
@@ -2867,14 +3054,13 @@ namespace ANSCENTER
case STATUS_INVALID_HARDWARE_ID:
_licenseKeyStatus = INVALID_HARDWARE_ID;
activationStatusStr = "Hardware id does not match this computer. current HWID is " + _hardwareId +
"; client HWID is " + WString2String(licensingClient.GetHardwareId()) +
"; Current activation key is " + WString2String(_activationKey) +
"; client Activation key is " + WString2String(licensingClient.GetActivationKey()) +
"; URL is " + WString2String(this->_licenseServiceURL.c_str())+
activationStatusStr = "Local hardware fingerprint check rejected this computer (hardware changed, or WMI returned no components). Current HWID is " + _hardwareId +
"; stored activation key is " + mask_secret(WString2String(_activationKey)) +
"; server activation key is " + mask_secret(WString2String(licensingClient.GetActivationKey())) +
"; URL is " + WString2String(this->_licenseServiceURL.c_str())+
"; ProductId is " + WString2String(wProductId) +
"; RegistrationName is " + WString2String(_registrationName) +
"; Status is" + std::to_string(activationStatus);
"; Status is " + std::to_string(activationStatus);
break;
case STATUS_LICENSE_EXPIRED:
@@ -2948,7 +3134,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) {
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->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -2975,7 +3161,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) {
// Save license
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->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3005,6 +3191,9 @@ namespace ANSCENTER
std::wstring _registrationName = String2WString(registrationName);
std::string _hardwareId = customHWID;
std::wstring hardwareId = String2WString(customHWID);
if (_hardwareId == kEmptyFingerprintHWID) {
this->_logger.LogWarn("ANSLSHelper::ValidateLicenseWithCustomHWID", kEmptyFingerprintWarning, __FILE__, __LINE__);
}
bool _overrideLicenseFile = false;
if (activationKey.empty()) {
if (this->_license != NULL) {// get it from there
@@ -3081,14 +3270,13 @@ namespace ANSCENTER
case STATUS_INVALID_HARDWARE_ID:
_licenseKeyStatus = INVALID_HARDWARE_ID;
activationStatusStr = "Hardware id does not match this computer. current HWID is " + _hardwareId +
"; client HWID is " + WString2String(licensingClient.GetHardwareId()) +
"; Current activation key is " + WString2String(_activationKey) +
"; client Activation key is " + WString2String(licensingClient.GetActivationKey()) +
activationStatusStr = "Local hardware fingerprint check rejected this computer (hardware changed, or WMI returned no components). Current HWID is " + _hardwareId +
"; stored activation key is " + mask_secret(WString2String(_activationKey)) +
"; server activation key is " + mask_secret(WString2String(licensingClient.GetActivationKey())) +
"; URL is " + WString2String(this->_licenseServiceURL.c_str()) +
"; ProductId is " + WString2String(wProductId) +
"; RegistrationName is " + WString2String(_registrationName) +
"; Status is" + std::to_string(activationStatus);
"; Status is " + std::to_string(activationStatus);
break;
case STATUS_LICENSE_EXPIRED:
@@ -3162,7 +3350,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) {
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->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3189,7 +3377,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) {
// Save license
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->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3475,7 +3663,7 @@ namespace ANSCENTER
return 0; // invalid
}
else {
if (!this->_license != NULL) delete this->_license;
if (this->_license != NULL) delete this->_license;
this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3852,9 +4040,25 @@ namespace ANSCENTER
osNode.put("endianess", os.isLittleEndian() ? "little endian" : "big endian");
root.add_child("OS", osNode);
// GPU information
auto gpus = hwinfo::getAllGPUs();
for (auto& gpu : gpus) {
// GPU information.
//
// 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;
gpuNodeChild.put("GPU", gpu.id());
gpuNodeChild.put("vendor", gpu.vendor());
@@ -3865,8 +4069,37 @@ namespace ANSCENTER
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);
// RAM information
hwinfo::RAM ram;
+14 -1
View File
@@ -259,7 +259,9 @@ namespace ANSCENTER
ANS_OCR = 1005,
ANS_ALPR =1006,
ANS_CV = 1007,
ANS_TS = 1008
ANS_TS = 1008,
ANSMIND = 1009,
ANSGW = 1010
};
enum ANSLICENSESTATUS {
ACTIVE = 0,
@@ -523,6 +525,10 @@ namespace ANSCENTER
[[nodiscard]] static bool isPathSafe(const std::string& pathStr);
public:
[[nodiscard]] static bool LicenseVerification(std::string licenseKey, int productId, std::string registrationName);
// Returns this machine's hardware id (same value as ANSLSHelper::GetCurrentHardwareId)
// but as a static call requiring no handle/instance — convenient for LabVIEW and other
// callers that just need the HWID. Returns "" on failure.
[[nodiscard]] static std::string GetHardwareId();
[[nodiscard]] static EngineType CheckHardwareInformation();
[[nodiscard]] static SystemHealthInfo GetSystemHealthInformation();
@@ -564,6 +570,13 @@ extern "C" __declspec(dllexport) void SetLicenseServiceURL(ANSCENTER::ANSLSHelpe
extern "C" __declspec(dllexport) int GetLicenseData(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, LStrHandle licenseData);
extern "C" __declspec(dllexport) int LoadLicenseTemplate(ANSCENTER::ANSLSHelper * *Handle, const char* templateFilePath);
extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper * *Handle, LStrHandle currentHwID);
// Handle-free static hardware id, two flavours:
// - GetHardwareId: LabVIEW native string handle (LStrHandle) output. Returns 1 on success, 0 on failure.
// - GetHardwareIdBuffer: caller-provided C buffer; works from plain C/C++ too (no LabVIEW manager).
// Writes a null-terminated string into 'buffer' (capacity 'bufferSize') and returns the number of
// characters written (excluding the null terminator), or 0 on failure / insufficient buffer.
extern "C" __declspec(dllexport) int GetHardwareId(LStrHandle currentHwID);
extern "C" __declspec(dllexport) int GetHardwareIdBuffer(char* buffer, int bufferSize);
extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey);
extern "C" __declspec(dllexport) int GenerateKeyWithExpiredDate(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int expiredYear, int expiredMonth, int expiredDate, LStrHandle generatedKey);
extern "C" __declspec(dllexport) int CheckLicenseKey(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, const char* registrationName, int productId);// , LStrHandle strEnableFeature); // Check if the license key is valid
+1 -1
View File
@@ -54,7 +54,7 @@ target_link_libraries(ANSLicensingSystem PRIVATE
shlwapi.lib winhttp.lib
pdh.lib psapi.lib
wbemuuid.lib oleaut32.lib ole32.lib
dxgi.lib
dxgi.lib advapi32.lib
dnsapi.lib
${WIN_COMMON_LIBS}
)
+37
View File
@@ -109,6 +109,43 @@ extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper
return 0;
}
}
extern "C" __declspec(dllexport) int GetHardwareId(LStrHandle currentHwID) {
try {
std::string st = ANSCENTER::ANSLicenseHelper::GetHardwareId();
int size = (int)st.length();
if (size > 0) {
MgErr error;
error = DSSetHandleSize(currentHwID, sizeof(int32) + size * sizeof(uChar));
if (error == noErr)
{
(*currentHwID)->cnt = size;
memcpy((*currentHwID)->str, st.c_str(), size);
return 1;
}
else return 0;
}
else return 0;
}
catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int GetHardwareIdBuffer(char* buffer, int bufferSize) {
try {
if (!buffer || bufferSize <= 0) return 0;
std::string st = ANSCENTER::ANSLicenseHelper::GetHardwareId();
int size = (int)st.length();
if (size <= 0) return 0;
// Need room for the string plus the null terminator.
if (size + 1 > bufferSize) return 0;
memcpy(buffer, st.c_str(), size);
buffer[size] = '\0';
return size;
}
catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey) {
try {
if (!Handle || !(*Handle) || !registrationName) return 0;
+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
+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
+768
View File
@@ -0,0 +1,768 @@
#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 "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
// Deterministic nonces are for TESTS ONLY: a real package draws them from
// the platform CSPRNG, because a repeated (key, nonce) pair is fatal.
uint8_t nonceSeed = 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;
}
// A content key per package. Tests pass a seed; a real packer must use the
// platform CSPRNG here.
Key contentKey{};
for (size_t i = 0; i < contentKey.size(); ++i)
contentKey[i] = uint8_t(req.nonceSeed * 31 + i * 7 + 11);
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::array<uint8_t, 4> manifestBase{uint8_t(req.nonceSeed + 1), 2, 3, 4};
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;
std::vector<std::array<uint8_t, 4>> bases;
for (size_t i = 0; i < req.models.size(); ++i) {
std::array<uint8_t, 4> base{uint8_t(req.nonceSeed + 10 + i), 5, 6, 7};
std::string blob;
sealChunked(req.models[i].bytes, req.models[i].id, base, blob);
blobs.push_back(std::move(blob));
bases.push_back(base);
}
// 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.
std::array<uint8_t, aead::kNonceBytes> wrapNonce{};
for (size_t i = 0; i < wrapNonce.size(); ++i)
wrapNonce[i] = uint8_t(req.nonceSeed + 100 + i);
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.
Key esk{};
for (size_t i = 0; i < esk.size(); ++i)
esk[i] = uint8_t(req.nonceSeed * 13 + i * 5 + 3);
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
+43
View File
@@ -0,0 +1,43 @@
# 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()
set_target_properties(ANSModelPackage PROPERTIES POSITION_INDEPENDENT_CODE ON)
+17 -14
View File
@@ -8,9 +8,11 @@ const unsigned char BitStream3::hiMask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0x
BitStream3::BitStream3() :
ownMemory(false),
buffer(NULL),
bufSize(0),
bitSize(0),
currentBit(0),
currentByte(0),
buffer(NULL)
currentByte(0)
{
}
@@ -41,6 +43,7 @@ void BitStream3::Attach(void * buf, int bitCount)
{
buffer = (unsigned char *)buf;
bitSize = bitCount;
bufSize = (bitCount + 7) >> 3;
currentBit = 0;
currentByte = 0;
}
@@ -53,20 +56,20 @@ int BitStream3::Write(const unsigned char * buf, int bitCount)
{
if ((gap & 7) != 0)
{
buffer[currentByte] = (unsigned char)((buffer[currentByte] << gap) | (buf[0] >> (bitCount < 8 ? bitCount - gap : 8 - gap)));
setByte(currentByte, (unsigned char)((byteAt(currentByte) << gap) | (buf[0] >> (bitCount < 8 ? bitCount - gap : 8 - gap))));
currentBit += gap; bitCount -= gap; currentByte++;
for (; bitCount >= 8; i++, currentByte++, currentBit += 8, bitCount -= 8)
buffer[currentByte] = (unsigned char)((buf[i] << gap) | (buf[i + 1] >> (8 - gap)));
setByte(currentByte, (unsigned char)((buf[i] << gap) | (buf[i + 1] >> (8 - gap))));
}
else
for (; bitCount >= 8; currentByte++, i++, currentBit += 8, bitCount -= 8)
buffer[currentByte] = buf[i];
setByte(currentByte, buf[i]);
}
if (bitCount > 0)
{
buffer[currentByte] = (unsigned char)((buffer[currentByte] << bitCount) | (buf[i] & ((1 << bitCount) - 1)));
setByte(currentByte, (unsigned char)((byteAt(currentByte) << bitCount) | (buf[i] & ((1 << bitCount) - 1))));
currentBit += bitCount;
}
@@ -86,20 +89,20 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount)
if ((gap & 7) != 0)
{
for (; bitCount >= 8 + 8 - gap; bitCount -= 8, currentByte++, currentBit += 8, i++)
readBuf[i] = (unsigned char)((buffer[currentByte] << gap) | (buffer[currentByte + 1] >> (8 - gap)));
readBuf[i] = (unsigned char)((byteAt(currentByte) << gap) | (byteAt(currentByte + 1) >> (8 - gap)));
// byte align
// byte align
if (bitCount > 0)
{
if (bitCount < 8 - gap)
{
readBuf[i] = (unsigned char)((buffer[currentByte] >> (8 - gap - bitCount)) & ((1 << bitCount) - 1));
readBuf[i] = (unsigned char)((byteAt(currentByte) >> (8 - gap - bitCount)) & ((1 << bitCount) - 1));
currentBit += bitCount;
bitCount = 0;
}
else
{
readBuf[i] = (unsigned char)(buffer[currentByte] & ((1 << (8 - gap)) - 1));
readBuf[i] = (unsigned char)(byteAt(currentByte) & ((1 << (8 - gap)) - 1));
bitCount -= (8 - gap);
currentBit += (8 - gap);
@@ -111,15 +114,15 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount)
}
else
for (; bitCount >= 8; bitCount -= 8, currentByte++, currentBit += 8, i++)
readBuf[i] = buffer[currentByte];
readBuf[i] = byteAt(currentByte);
// already aligned
if (bitCount > 0)
{
if (currentBit + 8 <= bitSize) // not last byte
readBuf[i] |= (unsigned char)((buffer[currentByte] >> (8 - bitCount)) & ((1 << bitCount) - 1));
else // last byte
readBuf[i] = (unsigned char)((buffer[currentByte] >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1));
readBuf[i] |= (unsigned char)((byteAt(currentByte) >> (8 - bitCount)) & ((1 << bitCount) - 1));
else // last byte
readBuf[i] = (unsigned char)((byteAt(currentByte) >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1));
currentBit += bitCount;
}
+14
View File
@@ -16,6 +16,20 @@ public:
unsigned char * GetBuffer();
protected:
// Bounds-safe accessors: every Write/Read goes through these so a caller
// that under-sizes the stream can no longer corrupt the heap (the 125-vs-147
// bug in GetCurrentHardwareId overflowed 3 bytes for years). Out-of-range
// reads return 0 — identical to the base32 padding zeros the hardware-id
// Match path has always effectively read there.
unsigned char byteAt(int i) const
{
return (buffer && i >= 0 && i < bufSize) ? buffer[i] : 0;
}
void setByte(int i, unsigned char v)
{
if (buffer && i >= 0 && i < bufSize) buffer[i] = v;
}
bool ownMemory;
unsigned char * buffer;
int bufSize, bitSize;
+139 -6
View File
@@ -21,9 +21,10 @@
#include <net/if.h>
#ifdef __linux__
# include <sys/ioctl.h>
# include <sys/stat.h> // stat() — physical-NIC test in linuxIsPhysicalNic
# include <netinet/in.h>
# include <unistd.h>
# include <string.h>
# include <string.h> // strncmp/strlen (same test)
// Match Linux to FreeBSD
# define AF_LINK AF_PACKET
@@ -82,6 +83,47 @@ void GetCPUIDPropertyList(std::list<std::string>* propList)
propList->push_back(buf);
}
#ifdef __linux__
// A PHYSICAL network interface, i.e. one backed by a real hardware device.
//
// Why this exists: the hardware id is built from up to 6 enumerated MACs, and
// on Linux getifaddrs() also returns VIRTUAL interfaces — docker0 and the
// veth* pairs a container creates, tun*/wg* from a VPN, br-*/virbr* bridges.
// Container veths get RANDOM MACs and come and go with the containers, so an
// unfiltered enumeration can hand back a different set on every call: the id
// changes and an activated licence silently stops matching its machine. Boxes
// running Docker (the Coder feature) hit this routinely.
//
// The test mirrors what the Windows branch already does via WMI ("PNPDeviceId
// LIKE %PCI%" — physical adapters only). /sys/class/net/<name>/device is a
// symlink to the backing device and exists ONLY for hardware-backed
// interfaces; virtual ones have no such link. That is the kernel's own answer,
// so it stays correct as new virtual-interface flavours appear — unlike a name
// denylist, which is kept only as a cheap second line of defence.
static bool linuxIsPhysicalNic(const char* ifname)
{
if (ifname == NULL || ifname[0] == '\0') return false;
// Names that are virtual by construction. Not exhaustive on purpose — the
// /sys check below is the real test; this only catches oddities where /sys
// is unavailable (unusual containers, minimal namespaces).
static const char* kVirtualPrefixes[] = {
"lo", "docker", "veth", "br-", "virbr", "tun", "tap", "wg",
"vmnet", "vboxnet", "zt", "ppp", "dummy", "kube", "cni", "flannel",
"lxc", "lxd", "nerdctl", "podman", "cali", "weave"
};
for (const char* prefix : kVirtualPrefixes) {
if (strncmp(ifname, prefix, strlen(prefix)) == 0) return false;
}
std::string path = std::string("/sys/class/net/") + ifname + "/device";
struct stat st;
// stat() (not lstat) so the symlink is followed — we want "the target
// exists", i.e. a real backing device.
return stat(path.c_str(), &st) == 0;
}
#endif // __linux__
void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{
// Head of the interface address linked list
@@ -99,6 +141,14 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
return;
}
#ifdef __linux__
// Collected separately so physical NICs can be preferred wholesale. Taking
// them inline would let a veth that merely appears EARLIER in the list
// consume one of the maxCount slots and push a real NIC out.
std::list<std::string> physical;
std::list<std::string> fallback;
#endif
while (current != NULL && maxCount > 0) {
const sockaddr_in* interfaceAddress = castToIP4(current->ifa_addr);
@@ -127,8 +177,18 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{
char buf[18];
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
#ifdef __linux__
// Bucket now, choose after the walk (see the lists above).
// maxCount is NOT decremented here: the cap applies to the
// SELECTED set, not to whatever happened to be enumerated.
if (linuxIsPhysicalNic(current->ifa_name))
physical.push_back(buf);
else
fallback.push_back(buf);
#else
propList->push_back(buf);
maxCount--;
#endif
}
}
@@ -137,6 +197,21 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
freeifaddrs(ifap);
ifap = NULL;
#ifdef __linux__
// Physical NICs only — that is the whole point. FAIL-SAFE: if the filter
// found none (no /sys, an exotic NIC driver, a network namespace with only
// virtual links) fall back to the unfiltered set rather than returning
// nothing. An empty list would drop ALL MAC entropy from the id, and with
// the system-UUID and disk-serial slots already constant on Linux that
// would leave near-identical ids across different machines — far worse
// than an id that occasionally drifts.
std::list<std::string>& chosen = physical.empty() ? fallback : physical;
for (std::list<std::string>::const_iterator it = chosen.begin();
it != chosen.end() && maxCount > 0; ++it, --maxCount) {
propList->push_back(*it);
}
#endif
}
void GetHDDPropertyList(std::list<std::string>* propList, int maxCount)
@@ -220,7 +295,13 @@ const char* HardwareId::GetCurrentHardwareId()
WmiHelper wmi;
#endif
BitStream3 bits(125);
// 147 bits are actually written below (3 version + 9 x 16-bit hashes). The
// previous size of 125 allocated only 16 bytes and the last two hash slots
// overflowed the heap block by 3 bytes (asymptomatic on the Windows heap,
// SIGSEGV under macOS malloc — found when ANSGateway enabled this code on
// POSIX). Only the first 16 bytes feed the base32 encode, so sizing the
// stream correctly changes no id on any platform.
BitStream3 bits(147);
list<string> propList;
unsigned char version = 1;
unsigned short hash1, hash2, hash3, hash4;
@@ -256,6 +337,8 @@ const char* HardwareId::GetCurrentHardwareId()
bits.WriteUInt16(hash1);
bool uuidFound = (hash1 != h0);
// CPU IDs - get up to 4 for better resilience
propList.clear();
@@ -270,6 +353,8 @@ const char* HardwareId::GetCurrentHardwareId()
cpu = NormalizeHardwareString(cpu);
}
int cpuCount = (int)propList.size();
hash1 = (propList.size() > 0) ? HashString(propList.front().c_str()) : h0;
hash2 = (propList.size() > 1) ? HashString(propList.back().c_str()) : h0;
@@ -298,6 +383,8 @@ const char* HardwareId::GetCurrentHardwareId()
}
propList.sort();
int macCount = (int)propList.size();
// Store up to 4 MAC hashes
auto macIter = propList.begin();
hash1 = (propList.size() > 0 && macIter != propList.end()) ? HashString((macIter++)->c_str()) : h0;
@@ -325,6 +412,8 @@ const char* HardwareId::GetCurrentHardwareId()
}
propList.sort();
int hddCount = (int)propList.size();
auto hddIter = propList.begin();
hash1 = (propList.size() > 0 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0;
hash2 = (propList.size() > 1 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0;
@@ -351,11 +440,34 @@ const char* HardwareId::GetCurrentHardwareId()
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();
}
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
WmiHelper wmi;
#endif
@@ -383,8 +495,14 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
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;
bits.Attach(buf.data(), 125);
bits.Attach(buf.data(), 160);
unsigned char version = 0;
bits.Read(&version, 3);
@@ -398,6 +516,7 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
// Use a points-based matching system for resilience
int matchPoints = 0;
bool uuidMatched = false;
const int REQUIRED_POINTS = 2; // Need at least 2 components to match
// Read UUID hash
@@ -430,6 +549,7 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
}
}
matchPoints += 2; // UUID/Memory match gives 2 points
uuidMatched = true;
}
else
{
@@ -437,9 +557,11 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
#ifdef _WIN32
wmi.GetPropertyList("SELECT * FROM Win32_ComputerSystemProduct", "UUID", &propList);
#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)
&& (s != "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
@@ -448,6 +570,8 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
if (hash1 != h0 && hash1 == HashString(s.c_str()))
{
matchPoints += 2; // UUID match is worth 2 points (most stable)
uuidMatched = true;
break;
}
}
}
@@ -558,5 +682,14 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
// - UUID (2) + MAC (1) = 3 points ✓
// - UUID (2) + HDD (1) = 3 points ✓
// - 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;
}
+33 -8
View File
@@ -2,6 +2,7 @@
#include <memory>
#include <vector>
#include <cstdio>
#include "except.h"
#include "template.h"
@@ -172,13 +173,15 @@ public:
if (!licenseKeyValidationData.empty())
validator.SetValidationData("LicenseKeyValidationData", licenseKeyValidationData.data(), (activationTemplate->GetValidationDataSize() + 7) >> 3);
try
try
{
validator.SetKey(activationKey.c_str());
}
catch (Exception * ex)
{
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;
if (ownActivationTemplate) delete activationTemplate;
return false;
@@ -186,6 +189,7 @@ public:
if (!validator.IsKeyValid())
{
fprintf(stderr, "[anslicensing] activation key does not validate against this license key + hardware id\n");
activationStatus = STATUS_INVALID_ACTIVATION_KEY;
if (ownActivationTemplate) delete activationTemplate;
return false;
@@ -222,6 +226,7 @@ public:
{
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;
if (ownActivationTemplate) delete activationTemplate;
return false;
@@ -239,14 +244,18 @@ public:
if (diffSeconds < 0)
{
m_licenseValidityDays = licenseTemplate->GetLicenseGracePeriod() - (int)(((time_t)(-diffSeconds) + 86400 - 1) / 86400);
if (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;
if (ownActivationTemplate) delete activationTemplate;
return false;
} else
m_licenseValidityDays = licenseTemplate->GetLicenseGracePeriod() + (int)(((time_t)diffSeconds + 86400 - 1) / 86400);
@@ -465,6 +474,9 @@ public:
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 {
activationKey = generator.GenerateKey();
}
@@ -472,19 +484,29 @@ public:
{
if ((ex->GetCode() == STATUS_NET_ERROR) && (generator.GetSigningServiceStatusCode() == 406))
{
fprintf(stderr, "[anslicensing] licensing service returned HTTP 406 - payment required\n");
m_licenseValidationResult.SetPaymentRequired(true);
ex->Destroy();
}
else
{
fprintf(stderr, "[anslicensing] activation request failed (service HTTP status %d) - check network access to the licensing service\n",
generator.GetSigningServiceStatusCode());
throw;
}
}
if (ValidateActivationKey(activationTemplate.get(), false))
{
fprintf(stderr, "[anslicensing] activation key acquired and validated (service HTTP status %d)\n",
generator.GetSigningServiceStatusCode());
activationStatus = STATUS_SUCCESS;
}
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");
}
m_licenseValidationResult.m_license = std::make_unique<LicenseImpl>();
@@ -531,10 +553,12 @@ public:
const char * GetCurrentHardwareIdCallback()
{
if (parentUsesWideChar)
if (parentUsesWideChar)
{
static string hwid = w2s(GetCurrentHardwareIdW(parentLicensingClient));
return hwid.c_str();
// per-instance storage: a function-local static would freeze the
// first client's HWID for the lifetime of the process
currentHwidCache = w2s(GetCurrentHardwareIdW(parentLicensingClient));
return currentHwidCache.c_str();
}
else
{
@@ -587,6 +611,7 @@ private:
string licenseKey;
string activationKey;
string hardwareId;
string currentHwidCache;
string licensingServiceUrl;
vector<unsigned char> licenseKeyValidationData;
+46 -24
View File
@@ -2,6 +2,7 @@
#define _WIN32_DCOM
#include <iostream>
#include <cstdio>
#include <comdef.h>
#include <Wbemidl.h>
#include <list>
@@ -45,17 +46,19 @@ public:
);
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &wbemLocator);
if (FAILED(hres))
{
if (ownCoInitialize)
CoUninitialize();
throw new exception("failed to create IWbemLocator", hres);
// Fail soft: leave wbemServices NULL so queries return empty results.
// Throwing here would escape callers (e.g. hardware id generation)
// that have no exception handling and crash the host application.
ReportFailure("CoCreateInstance(IWbemLocator)", hres);
wbemLocator = NULL;
return;
}
// Connect to the root\cimv2 namespace with the
@@ -76,12 +79,11 @@ public:
if (FAILED(hres))
{
ReportFailure("ConnectServer(ROOT\\CIMV2)", hres);
wbemLocator->Release();
if (ownCoInitialize)
CoUninitialize();
throw new exception("could not connect to WMI", hres);
wbemLocator = NULL;
wbemServices = NULL;
return;
}
// Set the IWbemServices proxy so that impersonation
@@ -99,10 +101,12 @@ public:
if (FAILED(hres))
{
wbemServices->Release();
wbemLocator->Release();
if (ownCoInitialize) CoUninitialize();
throw new exception("could not connecto to WMI", hres);
ReportFailure("CoSetProxyBlanket", hres);
wbemServices->Release();
wbemLocator->Release();
wbemServices = NULL;
wbemLocator = NULL;
return;
}
}
@@ -115,32 +119,44 @@ public:
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;
// For example, query for all the running processes
IEnumWbemClassObject* pEnumerator = NULL;
hres = wbemServices->ExecQuery(
bstr_t("WQL"),
bstr_t("WQL"),
bstr_t(wqlQuery),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
throw new exception("WMI query failed");
ReportFailure(wqlQuery, hres);
return;
}
else
{
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
int numElements = 0;
while (1)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1,
hres = pEnumerator->Next(WBEM_INFINITE, 1,
&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)
{
break;
@@ -203,6 +219,12 @@ public:
}
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;
IWbemServices * wbemServices;
IWbemLocator * wbemLocator;
+79
View File
@@ -0,0 +1,79 @@
# ANSAWSS3 — return codes and retry behaviour
Reference for callers of the `*ANSAWSHandle` exports in `ANSUtilities.dll`
(LabVIEW, C++, .NET). Source of truth: `modules/ANSUtilities/ANSUtilities.h`
(`kCall*` constants) and `modules/ANSUtilities/dllmain.cpp`.
## The one rule
**`rc == 1` means success. Everything else is a failure.** Success is the only
positive value, so `rc == 1` and `rc != 1` are both always correct tests. The
negative values exist so a caller can tell *why* it failed, and therefore
whether retrying is worthwhile.
## Upload functions
Applies to every `Upload*ANSAWSHandle`, `Upload*ANSAWSHandle_CPP` and
`Upload*ANSAWSHandle_V2` export (JPEG, binary, text, file-stream, multipart,
with or without prefix).
| Code | Meaning | Retry? |
|-----:|---------|--------|
| `1` | Uploaded. `uploadedFilePath` holds the object URL. | — |
| `0` | A required pointer argument was `NULL`. | **No** — caller bug |
| `-1` | Handle is not in the registry: never created, already released, or a stale value. | **No** — re-create the handle |
| `-2` | Failed, reason not classified. Should not occur; kept as a fallback. | Yes |
| `-3` | A `std::exception` escaped. | Yes |
| `-4` | An unknown exception escaped. | Yes |
| `-20` | **Permanent.** Bad argument (empty bucket/filename, part size < 5 MB, unreadable file), invalid licence or unlock code, or S3 answered with a 4xx — wrong bucket, no permission, bad key. | **No** — fix the configuration; retrying burns time and changes nothing |
| `-21` | **Not connected.** `Connect()` has not succeeded yet, or the link went down and the background retry thread is still working on it. | **Yes, later** — back off a few seconds |
| `-22` | **Transient.** Network error, timeout, or HTTP 5xx, still failing after the DLL's own internal retries. | **Yes** |
A practical LabVIEW policy:
* `rc = 1` → done.
* `rc = -20` or `0` → log it and stop; it will never succeed as-is.
* `rc = -21` → queue the image and try again in a few seconds.
* anything else negative → retry, with backoff.
Note that `-22` already means the DLL retried internally (up to 20 attempts
within a 30 s budget) before giving up, so an outer retry should not be tight.
## Connect
`ConnectANSAWSHandle` / `_V2`:
| Code | Meaning |
|-----:|---------|
| `1` | Connected; ready for uploads once `SetAuthentication` is called. |
| `0` | A required argument was `NULL`, the handle is unknown, the licence is invalid, or the endpoint resolves but refuses the connection. |
| `2` | The endpoint could not be resolved at all (no internet). A background thread keeps retrying — 3 s, doubling to a 30 s ceiling — until it connects or the handle is released. **You do not need to call `Connect` again.** Uploads return `-21` in the meantime. |
## Other functions
`CreateANSAWSHandle`, `ReleaseANSAWSHandle`, `SetAuthenticationANSAWSHandle`,
`SetProxyANSAWSHandle`, the `ListBucket*` / `CreateBucket*` / `DeleteBucket*`
family, `DownloadFileStreamANSAWSHandle` and `GeneratePresignedURLANSAWSHandle`
return `1` on success and `0` on failure. `GeneratePresignedURLANSAWSHandle`
(the `LStrHandle` variant) additionally uses `-1`, `-2`, `-3`, `-4` with the
same meanings as the table above.
## How connections behave
* `SetAuthentication` opens and keeps 8 pooled HTTPS connections; uploads borrow
and return them, so the TLS handshake is not repeated per image.
* A connection whose **transport** failed is closed, never returned to the pool.
A connection that merely received an HTTP error status is healthy and is kept.
* A pooled connection idle longer than 15 s is closed and replaced rather than
handed out, since middleboxes silently drop idle flows.
* The first request on a reused connection uses a short idle timeout (a quarter
of the configured one, 5 s by default); a fresh connection gets the full
timeout. This bounds what a dead socket costs.
* One upload call is capped at 30 s of wall clock across all internal retries.
## Diagnostics
`ANSAWSS3.log` and DebugView (`AWSS3` / `ANSAWS` tags) record the decision
points: pool evictions, discarded connections, the permanent/transient
classification with the S3 error code, deadline expiry, and background
reconnect attempts.
+1 -1
View File
@@ -540,7 +540,7 @@ namespace ANSCENTER {
") returned null — the DLL may be corrupt.");
s_ort_api = api;
Ort::Global<void>::api_ = api;
Ort::InitApi(api);
std::cout << "[EPLoader] ORT loaded successfully." << std::endl;
std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl;
+124 -12
View File
@@ -141,7 +141,7 @@ namespace ANSCENTER {
{
try {
// Make sure the Ort API pointer is initialised in THIS DLL.
if (Ort::Global<void>::api_ == nullptr) {
if (Ort::detail::Global::Api() == nullptr) {
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
}
@@ -221,8 +221,45 @@ namespace ANSCENTER {
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()
{
// 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) {
delete ort_session;
ort_session = nullptr;
@@ -574,10 +611,10 @@ namespace ANSCENTER {
const auto& epInfo = EPLoader::Current();
ANS_DBG("OrtHandler", "initialize_handler: EPLoader type=%d dir=%s",
static_cast<int>(epInfo.type), epInfo.libraryDir.c_str());
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ORT] api_ = " << (void*)Ort::Global<void>::api_ << std::endl;
std::cout << "[ORT] api_ = " << (void*)Ort::detail::Global::Api() << std::endl;
EngineType engine = (static_cast<int>(m_engineType) == -1)
? epInfo.type : m_engineType;
@@ -727,20 +764,26 @@ namespace ANSCENTER {
// AddExternalInitializersFromFilesInMemory() so ORT never
// memory-maps the file itself.
// ----------------------------------------------------------------
std::filesystem::path modelFsPath(onnx_path); // wchar_t*
std::filesystem::path modelDir = modelFsPath.parent_path();
// A model in memory has no directory, no sibling .onnx_data and
// 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();
if (!modelDir.empty() && std::filesystem::is_directory(modelDir)) {
std::filesystem::current_path(modelDir);
std::cout << "[ORT] CWD -> " << modelDir.string() << std::endl;
if (!fromMemory()) {
modelFsPath = std::filesystem::path(onnx_path); // wchar_t*
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 -----------------
// Keep the buffer alive across session creation (must outlive the
// Ort::Session constructor call).
std::vector<char> extDataBuffer;
{
if (!fromMemory()) {
// Build the expected external-data filename:
// <model_stem>.onnx_data (e.g. anssam3.onnx_data)
std::filesystem::path extDataPath =
@@ -806,7 +849,7 @@ namespace ANSCENTER {
// This avoids ORT opening/mapping ANY files during CreateSession.
std::vector<char> modelBuffer;
bool useModelBuffer = false;
if (!extDataBuffer.empty()) {
if (!extDataBuffer.empty() && !fromMemory()) {
// External data was pre-loaded, so also load the .onnx itself
try {
auto modelFileSize = std::filesystem::file_size(modelFsPath);
@@ -829,7 +872,10 @@ namespace ANSCENTER {
// --- Attempt session creation (with CUDA → CPU fallback) --------
auto createSession = [&](Ort::SessionOptions& opts, const char* label) {
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,
modelBuffer.data(), modelBuffer.size(), opts);
} else {
@@ -838,7 +884,11 @@ namespace ANSCENTER {
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 {
createSession(session_options, "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
// ====================================================================
@@ -1900,4 +2004,12 @@ namespace ANSCENTER {
return std::fabs(width() * height());
}
// Explicit instantiations of every BoundingBoxType member must live
// AFTER the template definitions above so the compiler actually emits
// symbols (the directives in ONNXEngine.h sit before the definitions
// and ClangCL, unlike MSVC, refuses to emit symbols from there).
template struct ANSCENTER::types::BoundingBoxType<int, float>;
template struct ANSCENTER::types::BoundingBoxType<float, float>;
template struct ANSCENTER::types::BoundingBoxType<double, double>;
} // namespace ANSCENTER
+74 -3
View File
@@ -73,9 +73,10 @@ namespace ANSCENTER {
}
};
template class BoundingBoxType<int, float>;
template class BoundingBoxType<float, float>;
template class BoundingBoxType<double, double>;
// Explicit instantiations now live at the end of ONNXEngine.cpp
// (after the template member definitions) so the compiler actually
// emits symbols. ClangCL refuses to emit them from a directive that
// precedes the definitions, whereas MSVC silently tolerated it.
typedef BoundingBoxType<int, float> Boxi;
typedef BoundingBoxType<float, float> Boxf;
@@ -467,6 +468,25 @@ namespace ANSCENTER {
const LITEORT_CHAR* onnx_path = nullptr; // ← points into onnx_path_w
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:
const unsigned int num_threads;
@@ -498,6 +518,23 @@ namespace ANSCENTER {
const OrtHandlerOptions& options,
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();
BasicOrtHandler(const BasicOrtHandler&) = delete;
@@ -673,6 +710,40 @@ namespace ANSCENTER {
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
// ====================================================================
+1 -1
View File
@@ -331,7 +331,7 @@ namespace ANSCENTER
{
// Initialize ORT API
const auto& epInfo = EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
m_env = new Ort::Env(ORT_LOGGING_LEVEL_ERROR, "ONNXSAM3");
@@ -4,6 +4,7 @@
#include <sstream>
#include "Utility.h"
#include "TRTCompat.h"
#include "GpuMemoryGuard.h"
// ============================================================================
// Crash-safe wrappers for TensorRT operations that can crash the process.
@@ -264,6 +265,8 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
// It is full path
std::string engineName = serializeEngineOptions(m_options, onnxModelPath);
std::string engineDir = m_options.engineFileDir;
m_engineFilePath = engineName;
m_rebuiltFromOnnx = false;
if (FileExist(engineName)) {
if (m_verbose) { std::cout << "Engine file found: " << engineName << std::endl; }
@@ -313,7 +316,7 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
return false;
}
}
if (!FileExist(onnxModelPath)) {
if (!hasOnnxBytes() && !FileExist(onnxModelPath)) {
// ONNX model does not exist, try to find alternative precision engine
std::cout << "Searching for alternative precision engine..." << std::endl;
@@ -334,6 +337,7 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
if (FileExist(alternativeEngineName)) {
std::cout << "Found alternative precision engine: " << alternativeEngineName << std::endl;
m_engineFilePath = alternativeEngineName;
return loadNetwork(alternativeEngineName, subVals, divVals, normalize);
}
else {
@@ -357,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 << "Loading existing FP32 engine..." << std::endl;
m_options.precision = ANSCENTER::Precision::FP32;
m_engineFilePath = fp32EngineName;
return loadNetwork(fp32EngineName, subVals, divVals, normalize);
}
}
// ONNX model exists, generate engine
m_rebuiltFromOnnx = true;
std::cout << "========================================" << std::endl;
std::cout << "Engine not found, generating from ONNX model" << std::endl;
std::cout << "========================================" << std::endl;
@@ -384,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 tempParser = std::unique_ptr<nvonnxparser::IParser>(nvonnxparser::createParser(*tempNetwork, m_logger));
std::ifstream onnxFile(onnxModelPath, std::ios::binary | std::ios::ate);
std::streamsize onnxSize = onnxFile.tellg();
onnxFile.seekg(0, std::ios::beg);
std::vector<char> onnxBuffer(onnxSize);
if (!onnxFile.read(onnxBuffer.data(), onnxSize)) {
std::cout << "Error: Failed to read ONNX file" << std::endl;
std::vector<char> onnxBuffer;
if (!readOnnxBytes(onnxModelPath, onnxBuffer)) {
std::cout << "Error: Failed to read ONNX model" << std::endl;
return false;
}
@@ -805,13 +808,23 @@ bool Engine<T>::loadNetwork(std::string trtModelPath, const std::array<float, 3>
// (cudaMalloc failure during inference) when too many tasks are loaded.
{
size_t freeVRAM = 0, totalVRAM = 0;
cudaError_t memErr = cudaMemGetInfo(&freeVRAM, &totalVRAM);
constexpr size_t kMinFreeBytes = 256ULL * 1024 * 1024; // 256 MiB minimum
if (memErr != cudaSuccess) {
// cudaMemGetInfo failed — CUDA context may not be initialized on this thread.
// Don't reject: let TRT try to deserialize (it may succeed).
} else if (freeVRAM < kMinFreeBytes) {
m_lastLoadFailedVRAM = true; // signal to buildLoadNetwork: engine file is NOT corrupt
bool haveMem = ANSCENTER::GpuMemoryGuard::Query(freeVRAM, totalVRAM);
// (1) Admission control: refuse to load this engine on the GPU if it
// does not fit while keeping the GpuMemoryGuard safety margin free.
// Prevents VRAM over-subscription across concurrent engines (FR/OD/LPR/
// liveness) that would otherwise OOM and permanently poison the CUDA
// context. The engine file is valid — m_lastLoadFailedVRAM tells the
// caller to preserve it (no rebuild) and just skip GPU load for now.
if (haveMem && !ANSCENTER::GpuMemoryGuard::CanLoad(buffer.size())) {
m_lastLoadFailedVRAM = true;
std::cerr << "[GpuMemGuard] REFUSING engine load on GPU "
<< m_options.deviceIndex << ": need ~"
<< (buffer.size() / (1024 * 1024)) << " MiB + "
<< (ANSCENTER::GpuMemoryGuard::MarginFor(totalVRAM) / (1024 * 1024))
<< " MiB margin, only " << (freeVRAM / (1024 * 1024))
<< " MiB free of " << (totalVRAM / (1024 * 1024))
<< " MiB total. Reduce concurrent engines/cameras or batch size."
<< std::endl;
return false;
}
if (m_verbose) {
@@ -1425,7 +1438,15 @@ trt_cache_create_context:
// ============================================================================
if (m_verbose) std::cout << "\nInfo: Starting warm-up inference..." << std::endl;
warmUp(m_verbose ? 10 : 1);
if (!warmUp(m_verbose ? 10 : 1)) {
// A failed warmup means this engine cannot run inference (wrong-device
// buffers, poisoned context, ...). Report failure so pool creation
// rejects the slot instead of silently adding a broken engine that
// corrupts every subsequent inference dispatched to it.
std::cout << "Error: Warm-up inference FAILED on GPU[" << m_options.deviceIndex
<< "] -- engine rejected (loadNetwork returns false)" << std::endl;
return false;
}
if (m_verbose) std::cout << "Info: Warm-up complete" << std::endl;
} // end of trt_cache_create_context scope
@@ -1441,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;
return true;
}
if (!FileExist(onnxModelPath)) {
if (!hasOnnxBytes() && !FileExist(onnxModelPath)) {
std::cout << "Error: ONNX model file does not exist: " << onnxModelPath << std::endl;
return false;
}
@@ -1478,16 +1499,14 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
return false;
}
// Read the onnx file into memory
std::ifstream file(onnxModelPath, std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (!file.read(buffer.data(), size)) {
std::cout << "Error: Unable to read ONNX file" << std::endl;
// Get the onnx bytes: from the in-memory buffer of a sealed package, or
// from the file on disk.
std::vector<char> buffer;
if (!readOnnxBytes(onnxModelPath, buffer)) {
std::cout << "Error: Unable to read ONNX model" << std::endl;
return false;
}
const std::streamsize size = static_cast<std::streamsize>(buffer.size());
std::cout << "ONNX model size: " << size / (1024 * 1024) << " MiB" << std::endl;
@@ -1798,6 +1817,25 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
// Set hardware compatibility
config->setHardwareCompatibilityLevel(nvinfer1::HardwareCompatibilityLevel::kNONE);
// -- Blackwell (SM 12.x) aux-stream workaround ----------------------------
// On sm_120 (RTX 50-series), TRT 10.15's Myelin runtime faults when a second
// concurrent execution context runs with auto-selected aux streams:
// "Internal Error: MyelinCheckException ... l2cm_layer_internal.cpp:174"
// followed by CUDA error 700 (illegal memory access) poisoning the context.
// Aux streams only provide intra-inference parallelism (irrelevant for the
// small per-slot models we run); throughput comes from the engine pool's
// one-context-per-slot design, which is unaffected. Force 0 aux streams on
// Blackwell until NVIDIA fixes the Myelin multi-stream path.
// NOTE: baked in at build time -- stale cached .engine files must be deleted
// for this to take effect.
#if NV_TENSORRT_MAJOR >= 10
if (prop.major >= 12) {
config->setMaxAuxStreams(0);
std::cout << "Aux streams disabled on SM " << prop.major << "." << prop.minor
<< " (Blackwell Myelin multi-stream workaround)" << std::endl;
}
#endif
// -- TensorRT 10+ tactic DRAM pool ----------------------------------------
// Separate scratch pool for kernel-selection during build. Without this,
// tactic evaluation competes with the workspace allocation and tactics
@@ -2428,16 +2466,14 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
auto tempParser = std::unique_ptr<nvonnxparser::IParser>(
nvonnxparser::createParser(*tempNetwork, m_logger));
std::ifstream onnxFile(onnxModelPath, std::ios::binary | std::ios::ate);
if (!onnxFile.good()) {
std::vector<char> onnxBuffer;
const bool onnxReadable = readOnnxBytes(onnxModelPath, onnxBuffer);
if (!onnxReadable) {
ANS_DBG("TRT_Build", "buildWithRetry WARN cannot open ONNX for pre-parse path=%s",
onnxModelPath.c_str());
}
if (onnxFile.good()) {
std::streamsize onnxSize = onnxFile.tellg();
onnxFile.seekg(0, std::ios::beg);
std::vector<char> onnxBuffer(onnxSize);
if (onnxFile.read(onnxBuffer.data(), onnxSize)) {
if (onnxReadable) {
{
unsigned long sehRetryParse = 0;
bool retryParsed = parseOnnxModelSafe(tempParser.get(),
onnxBuffer.data(), onnxBuffer.size(), &sehRetryParse);
@@ -2465,9 +2501,6 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
(int)retryParsed,
retryParsed ? tempNetwork->getNbInputs() : -1);
}
} else {
ANS_DBG("TRT_Build", "buildWithRetry WARN ONNX read failed bytes=%zd",
(long long)onnxSize);
}
}
} else {
@@ -606,6 +606,25 @@ bool Engine<T>::runInferenceFromPool(
bool kickedGrowth = false;
auto _poolAcquireStart = std::chrono::steady_clock::now();
// -- DEVICE AFFINITY: find which GPU the caller's inputs live on ------
// Dispatching to a slot on the same GPU avoids the pinned-host migration
// copy (~1 ms per 1080p frame) that runInference performs for foreign
// inputs. Queried outside the slot mutex — it's a cheap driver lookup.
int inputDevice = -1;
for (const auto& batch : inputs) {
for (const auto& img : batch) {
if (!img.data) continue;
cudaPointerAttributes attr{};
if (cudaPointerGetAttributes(&attr, img.data) == cudaSuccess &&
attr.type == cudaMemoryTypeDevice) {
inputDevice = attr.device;
}
cudaGetLastError(); // clear any error the query left behind
break; // first image is representative
}
if (inputDevice >= 0) break;
}
{
std::unique_lock<std::mutex> lock(m_slotMutex);
@@ -616,13 +635,31 @@ bool Engine<T>::runInferenceFromPool(
const size_t n = m_slots.size();
if (n > 0) {
const size_t start = m_nextSlotHint.load() % n;
for (size_t i = 0; i < n; ++i) {
auto& s = m_slots[(start + i) % n];
if (!s.busy && s.engine) { // alive and idle
s.busy = true;
slot = &s;
m_nextSlotHint = (start + i + 1) % n;
break;
// Pass A: idle slot on the inputs' own GPU (zero-copy dispatch)
if (inputDevice >= 0) {
for (size_t i = 0; i < n; ++i) {
auto& s = m_slots[(start + i) % n];
if (!s.busy && s.engine && s.deviceIndex == inputDevice) {
s.busy = true;
slot = &s;
m_nextSlotHint = (start + i + 1) % n;
break;
}
}
}
// Pass B: any idle slot (round-robin) — runInference migrates
// the inputs across GPUs when needed.
if (!slot) {
for (size_t i = 0; i < n; ++i) {
auto& s = m_slots[(start + i) % n];
if (!s.busy && s.engine) { // alive and idle
s.busy = true;
slot = &s;
m_nextSlotHint = (start + i + 1) % n;
break;
}
}
}
}
@@ -88,8 +88,20 @@ public:
const std::array<float, 3>& subVals,
const std::array<float, 3>& divVals,
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
// only needs a lightweight, non-shared engine (e.g., OptimizeModelStr).
// Bypass the pool cache entirely:
@@ -99,7 +111,7 @@ public:
// Note: maxSlotsPerGpu==1 is now the normal "1 slot per GPU" multi-GPU
// round-robin mode, so it goes through the pool path below.
if (maxSlotsPerGpu == 0) {
auto engine = std::make_shared<Engine<T>>(options);
auto engine = newEngine();
bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
return ok ? engine : nullptr;
}
@@ -141,13 +153,13 @@ public:
}
// 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);
if (!ok) {
// Step 1: Force-evict all pools with refcount=0 to reclaim VRAM
int evicted = forceEvictPending();
if (evicted > 0) {
engine = std::make_shared<Engine<T>>(options);
engine = newEngine();
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.
// Lightweight mode: tasks queue for a single shared slot — slower but works.
if (!ok) {
engine = std::make_shared<Engine<T>>(options);
engine = newEngine();
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
}
@@ -183,7 +195,7 @@ public:
}
// Final retry — try lightweight again after delay
engine = std::make_shared<Engine<T>>(options);
engine = newEngine();
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
}
@@ -130,13 +130,31 @@ static inline cudaError_t cudaStreamSynchronize_Safe(cudaStream_t stream) {
}
template <typename T>
void Engine<T>::warmUp(int iterations) {
bool Engine<T>::warmUp(int iterations) {
if (m_verbose) {
std::cout << "\n========================================" << std::endl;
std::cout << "Engine Warmup" << std::endl;
std::cout << "========================================" << std::endl;
}
// Ensure this thread's CUDA device matches the engine BEFORE the dummy-input
// uploads below. warmUp() does GPU work (GpuMat uploads) ahead of
// runInference()'s own device guard, so it must not rely on the caller
// having left the right device current (multi-GPU pool creation touches
// several devices on one thread).
{
int currentDev = -1;
cudaGetDevice(&currentDev);
if (currentDev != m_options.deviceIndex) {
std::cout << "Warning [Warmup]: thread was on GPU[" << currentDev
<< "], engine expects GPU[" << m_options.deviceIndex
<< "] -- switching device" << std::endl;
cudaSetDevice(m_options.deviceIndex);
}
}
bool allBatchesOk = true;
// Determine batch sizes to warm up
std::vector<int> batchSizes;
@@ -212,7 +230,9 @@ void Engine<T>::warmUp(int iterations) {
auto firstTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
if (!firstSuccess) {
if (m_verbose) std::cout << " ✗ ERROR: First iteration failed for batch=" << batchSize << std::endl;
std::cout << " ✗ ERROR: Warmup first iteration failed for batch=" << batchSize
<< " on GPU[" << m_options.deviceIndex << "]" << std::endl;
allBatchesOk = false;
continue;
}
@@ -231,7 +251,9 @@ void Engine<T>::warmUp(int iterations) {
for (int i = 1; i < iters; ++i) {
bool success = runInference(dummyInputs, dummyOutputs);
if (!success) {
if (m_verbose) std::cout << " ✗ ERROR: Iteration " << i << " failed" << std::endl;
std::cout << " ✗ ERROR: Warmup iteration " << i << " failed for batch="
<< batchSize << " on GPU[" << m_options.deviceIndex << "]" << std::endl;
allBatchesOk = false;
break;
}
}
@@ -262,6 +284,7 @@ void Engine<T>::warmUp(int iterations) {
std::cout << "Kernels pre-compiled for all batch sizes." << std::endl;
std::cout << "========================================\n" << std::endl;
}
return allBatchesOk;
}
template <typename T>
bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& inputs,std::vector<std::vector<std::vector<T>>>& featureVectors) {
@@ -778,6 +801,118 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
}
}
// Pass 1.5: INPUT DEVICE MIGRATION (multi-GPU pool dispatch).
//
// The caller prepares input GpuMats on ITS device (e.g. camera preprocessing
// on GPU 0), but round-robin dispatch may select a slot on another GPU.
// Kernels cannot touch cross-device memory without peer access: preprocessing
// a device-A GpuMat with a device-B stream raises an async illegal memory
// access that permanently poisons every CUDA context in the process
// (sticky error 700). Detect foreign inputs and migrate them onto this
// engine's device via a UVA copy (cudaMemcpyDefault routes over PCIe).
std::vector<std::vector<cv::cuda::GpuMat>> migratedInputs;
const std::vector<std::vector<cv::cuda::GpuMat>>* activeInputs = &inputs;
{
bool anyForeign = false;
for (size_t i = 0; i < numInputs && !anyForeign; ++i) {
for (const auto& img : inputs[i]) {
if (!img.data) continue;
cudaPointerAttributes attr{};
if (cudaPointerGetAttributes(&attr, img.data) == cudaSuccess &&
attr.type == cudaMemoryTypeDevice &&
attr.device != m_options.deviceIndex) {
anyForeign = true;
}
cudaGetLastError(); // clear any error the attribute query left behind
break; // first image per tensor is representative
}
}
if (anyForeign) {
migratedInputs.resize(numInputs);
for (size_t i = 0; i < numInputs; ++i) {
migratedInputs[i].reserve(inputs[i].size());
for (const auto& src : inputs[i]) {
cudaPointerAttributes attr{};
const bool foreign = src.data &&
cudaPointerGetAttributes(&attr, src.data) == cudaSuccess &&
attr.type == cudaMemoryTypeDevice &&
attr.device != m_options.deviceIndex;
cudaGetLastError();
if (!foreign) { migratedInputs[i].push_back(src); continue; }
// Two-hop copy through pinned host staging. A direct
// device-to-device cudaMemcpy2D across GPUs is not supported
// (2D copies have no peer path, and GeForce has no P2P), so:
// hop 1: strided D2H from the foreign GPU into pinned host
// hop 2: strided H2D onto this engine's GPU
// Synchronous copies keep the single staging buffer safe to
// reuse across images; ~1 ms per 1080p frame over PCIe 4.
struct PinnedStaging { // thread-local, grow-only
void* ptr = nullptr;
size_t cap = 0;
~PinnedStaging() { if (ptr) cudaFreeHost(ptr); }
};
thread_local PinnedStaging tl_staging;
const size_t rowBytes = static_cast<size_t>(src.cols) * src.elemSize();
const size_t needed = rowBytes * static_cast<size_t>(src.rows);
if (needed > tl_staging.cap) {
if (tl_staging.ptr) cudaFreeHost(tl_staging.ptr);
tl_staging.ptr = nullptr; tl_staging.cap = 0;
if (cudaMallocHost(&tl_staging.ptr, needed) != cudaSuccess) {
std::cout << "Error [MultiGPU]: pinned staging alloc failed ("
<< needed << " bytes)" << std::endl;
return false;
}
tl_staging.cap = needed;
}
// Hop 1: D2H. UVA lets us read the foreign pointer from this
// device's context; if the runtime rejects it, retry with the
// source device current.
cudaError_t mErr = cudaMemcpy2D(
tl_staging.ptr, rowBytes, src.data, src.step,
rowBytes, src.rows, cudaMemcpyDeviceToHost);
if (mErr != cudaSuccess) {
cudaGetLastError();
cudaSetDevice(attr.device);
mErr = cudaMemcpy2D(
tl_staging.ptr, rowBytes, src.data, src.step,
rowBytes, src.rows, cudaMemcpyDeviceToHost);
cudaSetDevice(m_options.deviceIndex);
}
if (mErr != cudaSuccess) {
std::cout << "Error [MultiGPU]: migration hop1 (D2H from GPU["
<< attr.device << "]) failed: " << cudaGetErrorString(mErr) << std::endl;
return false;
}
// Hop 2: H2D onto this engine's GPU.
cv::cuda::GpuMat dst(src.rows, src.cols, src.type());
mErr = cudaMemcpy2D(
dst.data, dst.step, tl_staging.ptr, rowBytes,
rowBytes, src.rows, cudaMemcpyHostToDevice);
if (mErr != cudaSuccess) {
std::cout << "Error [MultiGPU]: migration hop2 (H2D to GPU["
<< m_options.deviceIndex << "]) failed: "
<< cudaGetErrorString(mErr) << std::endl;
return false;
}
migratedInputs[i].push_back(dst);
}
}
activeInputs = &migratedInputs;
static std::atomic<bool> s_migrationLogged{false};
if (!s_migrationLogged.exchange(true)) {
std::cout << "Info [MultiGPU]: inputs resided on another GPU -- migrated to GPU["
<< m_options.deviceIndex << "] via UVA copy (logged once; recurs per"
<< " cross-device dispatch)" << std::endl;
}
}
}
// Pass 2: Preprocess + D2D copies — all on m_inferenceStream (no null stream).
//
// All OpenCV CUDA ops (convertTo, subtract, divide, split) in blobFromGpuMats
@@ -794,7 +929,7 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
preprocessedBuffers.reserve(numInputs);
for (size_t i = 0; i < numInputs; ++i) {
const auto& batchInput = inputs[i];
const auto& batchInput = (*activeInputs)[i];
// Preprocess on m_inferenceStream (not the null stream).
preprocessedBuffers.push_back(
@@ -811,8 +946,23 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
m_inferenceStream);
if (copyErr != cudaSuccess) {
// Rich diagnostics — this fires on multi-GPU slot warmup failures
// (Blackwell debugging). Identify WHICH precondition broke:
// wrong current device, empty blob (failed allocation), null dest
// buffer, or a pre-existing sticky async error.
int curDev = -1; cudaGetDevice(&curDev);
std::cout << "Error: Failed to copy input " << i
<< " to inference buffer: " << cudaGetErrorString(copyErr) << std::endl;
<< " to inference buffer: " << cudaGetErrorString(copyErr) << "\n"
<< " [diag] engine deviceIndex = " << m_options.deviceIndex
<< ", thread current device = " << curDev << "\n"
<< " [diag] src blob ptr = " << static_cast<const void*>(blobMat.data)
<< " (" << blobMat.rows << "x" << blobMat.cols
<< ", elemSize " << blobMat.elemSize() << ")\n"
<< " [diag] dst buffer ptr = " << m_buffers[i]
<< ", copySize = " << copySize << " bytes\n"
<< " [diag] inference stream = " << static_cast<const void*>(m_inferenceStream)
<< ", pending lastError = " << cudaGetErrorString(cudaGetLastError())
<< std::endl;
return false;
}
}
@@ -4,6 +4,48 @@
#include <NvInferVersion.h> // also defines TRT version macros
#include <cudnn_version.h> // CUDNN_MAJOR/MINOR/PATCHLEVEL
#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>
void Engine<T>::transformOutput(std::vector<std::vector<std::vector<T>>> &input, std::vector<std::vector<T>> &output) {
@@ -207,6 +249,21 @@ std::string Engine<T>::serializeEngineOptions(const ANSCENTER::Options& options,
+ std::to_string(CUDNN_MAJOR) + "."
+ std::to_string(CUDNN_MINOR);
// -- Blackwell aux-stream workaround marker --------------------------------
// On SM 12.x the builder forces maxAuxStreams=0 (see EngineBuildLoadNetwork.inl:
// TRT 10.15 Myelin faults with concurrent contexts + aux streams). Tag those
// engines so caches built *before* the workaround (with aux streams baked in)
// miss and rebuild automatically instead of loading and crashing.
#if NV_TENSORRT_MAJOR >= 10
{
cudaDeviceProp prop{};
if (cudaGetDeviceProperties(&prop, options.deviceIndex) == cudaSuccess
&& prop.major >= 12) {
engineName += ".aux0";
}
}
#endif
return engineName;
}
@@ -231,6 +288,32 @@ cv::cuda::GpuMat Engine<T>::blobFromGpuMats(const std::vector<cv::cuda::GpuMat>
thread_local cv::cuda::GpuMat tl_blob;
thread_local cv::cuda::GpuMat tl_floatImg;
thread_local int tl_blobMaxElems = 0;
thread_local int tl_blobDevice = -1;
// DEVICE AFFINITY: the cached GpuMats live on ONE device. When the same
// thread serves engines on different GPUs (multi-GPU pool creation warms
// probe on GPU A, then slots on GPU B), reusing a device-A blob with a
// device-B stream causes invalid-argument launches and an async illegal
// memory access that permanently poisons both CUDA contexts (sticky 700).
// Detect the device switch and rebuild the cache on the current device.
{
int curDev = -1;
cudaGetDevice(&curDev);
if (curDev != tl_blobDevice) {
if (tl_blobDevice >= 0) {
// Free on the owning device — the async allocator frees via the
// CURRENT device's null-stream pool, so switch, release, switch back.
cudaSetDevice(tl_blobDevice);
tl_blob.release();
tl_floatImg.release();
cudaSetDevice(curDev);
ANS_DBG("TRT_Preproc", "blobFromGpuMats: device switch GPU[%d]->GPU[%d], preproc cache rebuilt",
tl_blobDevice, curDev);
}
tl_blobMaxElems = 0;
tl_blobDevice = curDev;
}
}
if (totalElems > tl_blobMaxElems) {
tl_blob = cv::cuda::GpuMat(1, totalElems, CV_32FC1);
+123
View File
@@ -0,0 +1,123 @@
#pragma once
// ============================================================================
// GpuMemoryGuard — process-wide GPU VRAM safeguard.
//
// Purpose: prevent the out-of-memory cascade that happens when many TensorRT /
// CUDA engines (FR-SCRFD, OD-YOLO, LPR, liveness, ORT-CUDA, OpenCV-CUDA) share
// one GPU and collectively exhaust VRAM. The first cudaMalloc/stream OOM
// poisons the CUDA context ("permanently corrupted after N failures"), after
// which every GPU op fails and the service appears dead until restart.
//
// Three coordinated layers (all process-wide, thread-safe, lock-free):
// (1) CanLoad(need) — admission control: refuse to load an engine on GPU
// if it won't fit while keeping a safety margin free.
// (2) UnderPressure() — pressure guard: skip a frame's GPU work when free
// VRAM is below the margin (or breaker is open).
// (3) NoteOom()/Breaker— circuit breaker: after a caught OOM, open a short
// cooldown so callers back off instead of hammering
// the fragile context (which is what corrupts it).
//
// The margin scales with the device: total/8, clamped to [192 MiB, 768 MiB].
// 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 <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
namespace ANSCENTER {
class GpuMemoryGuard {
public:
// ---- configuration ----------------------------------------------------
// Effective margin for a device with 'totalB' VRAM. An explicit override
// (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 ------------------------------------
// True if 'needBytes' can be committed on the current device while keeping
// the safety margin free. If VRAM can't be queried, returns true (never
// block on a failed query — let the normal path handle it).
static bool CanLoad(size_t needBytes) {
size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return true;
return freeB >= needBytes + MarginFor(totalB);
}
// ---- (2) pressure guard at inference ----------------------------------
// True if this frame should SKIP GPU work: free VRAM below margin, or the
// circuit breaker is currently open.
static bool UnderPressure() {
if (BreakerOpen()) return true;
size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return false;
return freeB < MarginFor(totalB);
}
// ---- (3) OOM-safe alloc + circuit breaker -----------------------------
static bool IsOom(cudaError_t e) { return e == cudaErrorMemoryAllocation; }
// Open the breaker for a short cooldown after a (recoverable) OOM so all
// callers back off together instead of repeatedly faulting the context.
static void NoteOom() {
s_breakerUntilMs.store(NowMs() + kCooldownMs, std::memory_order_relaxed);
}
static bool BreakerOpen() {
return NowMs() < s_breakerUntilMs.load(std::memory_order_relaxed);
}
// Lightweight current-device VRAM query. Clears the non-fatal sticky error
// that a failed query can leave on the context.
static bool Query(size_t& freeB, size_t& totalB) {
cudaError_t e = cudaMemGetInfo(&freeB, &totalB);
cudaGetLastError();
return e == cudaSuccess;
}
private:
static int64_t NowMs() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
// 0 = no override, use device-scaled margin.
static size_t InitMarginOverride() {
if (const char* e = std::getenv("ANSCENTER_GPU_MARGIN_MB")) {
long mb = std::atol(e);
if (mb > 0) return static_cast<size_t>(mb) * 1024ULL * 1024ULL;
}
return 0;
}
static constexpr int64_t kCooldownMs = 1500; // breaker cooldown after OOM
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 };
};
} // namespace ANSCENTER
+372 -19
View File
@@ -4,6 +4,7 @@
#include "ANSOVFaceDetector.h"
#include "SCRFDFaceDetector.h"
#include "ANSFaceRecognizer.h"
#include "ONNXFaceAttributes.h"
#include <faiss/IndexIDMap.h>
#include <faiss/IndexFlat.h>
#include <boost/uuid/uuid.hpp>
@@ -374,6 +375,27 @@ namespace ANSCENTER {
bool ANSFacialRecognition::LoadEngine() {
try {
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)
UnloadEngine();
@@ -463,6 +485,16 @@ namespace ANSCENTER {
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
if (!Reload()) {
LogThreadSafe("ANSFacialRecognition::LoadEngine",
@@ -634,6 +666,19 @@ namespace ANSCENTER {
"Built new index with " + std::to_string(newFaissIndex->ntotal) +
" 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)
// Inference is blocked only during this swap.
@@ -805,9 +850,12 @@ namespace ANSCENTER {
}
bool ANSFacialRecognition::InitializeRecognizer() {
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
_recognizer = std::make_unique<ANSFaceRecognizer>();
auto recognizer = std::make_unique<ANSFaceRecognizer>();
recognizer->SetPreferredModel(_requestedRecModel);
_recognizer = std::move(recognizer);
_recognizer->SetMaxSlotsPerGpu(m_maxSlotsPerGpu);
// Configure model
@@ -848,7 +896,13 @@ namespace ANSCENTER {
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;
}
catch (const std::exception& e) {
@@ -911,8 +965,50 @@ namespace ANSCENTER {
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) {
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");
if (!FileExist(ageGenderModelPath)) {
@@ -936,18 +1032,20 @@ namespace ANSCENTER {
_ageGenderDetector.reset();
// Create new detector
_ageGenderDetector = std::make_unique<AgeGenderDetection>(ageGenderModelPath, true);
auto detector = std::make_unique<AgeGenderDetection>(ageGenderModelPath, true);
// Compile model for target device
ov::CompiledModel compiledModel = core->compile_model(
_ageGenderDetector->read(*core),
detector->read(*core),
deviceName
);
// Create inference request
_ageGenderDetector->request = compiledModel.create_infer_request();
_ageGenderDetector->inTensor = _ageGenderDetector->request.get_input_tensor();
_ageGenderDetector->inTensor.set_shape(_ageGenderDetector->inShape);
detector->request = compiledModel.create_infer_request();
detector->inTensor = detector->request.get_input_tensor();
detector->inTensor.set_shape(detector->inShape);
_ageGenderDetector = std::move(detector);
}
return true;
@@ -966,6 +1064,30 @@ namespace ANSCENTER {
}
bool ANSFacialRecognition::InitializeEmotionModel(const std::string& deviceName) {
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");
if (!FileExist(emotionModelPath)) {
@@ -989,18 +1111,20 @@ namespace ANSCENTER {
_emotionsDetector.reset();
// Create new detector
_emotionsDetector = std::make_unique<EmotionsDetection>(emotionModelPath, true);
auto detector = std::make_unique<EmotionsDetection>(emotionModelPath, true);
// Compile model for target device
ov::CompiledModel compiledModel = core->compile_model(
_emotionsDetector->read(*core),
detector->read(*core),
deviceName
);
// Create inference request
_emotionsDetector->request = compiledModel.create_infer_request();
_emotionsDetector->inTensor = _emotionsDetector->request.get_input_tensor();
_emotionsDetector->inTensor.set_shape(_emotionsDetector->inShape);
detector->request = compiledModel.create_infer_request();
detector->inTensor = detector->request.get_input_tensor();
detector->inTensor.set_shape(detector->inShape);
_emotionsDetector = std::move(detector);
}
return true;
@@ -1071,7 +1195,209 @@ namespace ANSCENTER {
return false;
}
}
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(
float knownPersonThreshold,
bool enableAgeGender,
@@ -1353,6 +1679,17 @@ namespace ANSCENTER {
// Get largest face (CPU operation)
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)
std::vector<float> embedding;
{
@@ -1363,8 +1700,10 @@ namespace ANSCENTER {
// Validate embedding
if (embedding.empty() || embedding.size() != _config._recOutputDim) {
LogThreadSafe("ANSFacialRecognition::InsertFace",
"Invalid embedding generated for user ID " + std::to_string(userId) +
" (size: " + std::to_string(embedding.size()) + ")");
"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 0;
}
@@ -1461,6 +1800,16 @@ namespace ANSCENTER {
// Get largest face
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)
std::vector<float> embedding;
{
@@ -1470,7 +1819,10 @@ namespace ANSCENTER {
if (embedding.empty() || embedding.size() != _config._recOutputDim) {
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;
}
@@ -2737,9 +3089,10 @@ namespace ANSCENTER {
}
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) {
camDataPtr->currentAttributeInterval = std::min(10, _attributeInterval * 2);
camDataPtr->currentAttributeInterval = _attributeInterval * 2;
} else if (camDataPtr->stableFrameCount > 10) {
camDataPtr->currentAttributeInterval = _attributeInterval;
} else {
+64 -5
View File
@@ -97,6 +97,26 @@ namespace ANSCENTER
bool UpdateFaceQueue(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> Inference(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 InitializeEmotionModel(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);
Object GetLargestObject(const std::vector<Object>& objects);
bool AreFacesSimilar(const FaceResultObject& face1, const FaceResultObject& face2);
@@ -172,7 +197,7 @@ namespace ANSCENTER
int _faceThresholdSize; // number of frames to consider for face recognition
bool _enableFaceQueue;
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;
@@ -214,9 +239,18 @@ namespace ANSCENTER
std::unique_ptr<ANSFRBase>_recognizer = nullptr;
std::unique_ptr<ANSFDBase>_detector = 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<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 FACE_THRESHOLD_SIZE = 5;
// 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
// 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 previousTrackIdCount = 0; // trackId count from last frame
@@ -258,7 +292,7 @@ namespace ANSCENTER
attributeFrameCounter = 0;
cachedAttributes.clear();
trackIdentities.clear();
currentAttributeInterval = 5;
currentAttributeInterval = 20;
stableFrameCount = 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 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);
+12 -13
View File
@@ -638,30 +638,29 @@ namespace ANSCENTER {
detectedNode.put("Age", "Adults");
else
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()) {
const auto& [emotionName, emotionConfidence] = face.getMainEmotion();
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;
if (emotionName == "Sad" || emotionName == "sad")
if (emotionName == "sad")
adjustedEmotionName = "Attentive";
else if (emotionName == "Happy" || emotionName == "happy")
else if (emotionName == "happy")
adjustedEmotionName = "Happy";
else if (emotionName == "Neutral" || emotionName == "neutral")
else if (emotionName == "neutral")
adjustedEmotionName = "Neutral";
else if (emotionName == "Angry" || emotionName == "angry")
else if (emotionName == "anger")
adjustedEmotionName = "Attentive";
else if (emotionName == "Surprised" || emotionName == "surprised")
else if (emotionName == "surprise")
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) << "%)";
detectedNode.put("Emotion", emotionStr.str());
+44 -4
View File
@@ -107,6 +107,42 @@ namespace ANSCENTER {
float age;
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 ANSF_API Face {
public:
@@ -200,7 +236,7 @@ namespace ANSCENTER {
//std::vector<HeadPoseResults> getAllResults();
};
class ANSF_API AgeGenderDetection :public BaseDetection {
class ANSF_API AgeGenderDetection : public BaseDetection, public IAgeGenderEstimator {
public:
std::recursive_mutex _mutex;
std::string outputAge;
@@ -211,7 +247,9 @@ namespace ANSCENTER {
bool doRawOutputMessages);
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 enqueue(const cv::Mat& face);
//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:
std::recursive_mutex _mutex;
//size_t enquedFaces;
@@ -230,7 +268,9 @@ namespace ANSCENTER {
bool doRawOutputMessages);
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 enqueue(const cv::Mat& face);
//std::map<std::string, float> getResult(int idx);
+163 -37
View File
@@ -1,5 +1,7 @@
#include "ANSFaceRecognizer.h"
#include "engine.h"
#include <cctype>
#include <filesystem>
//#define DEBUG_TIME
#ifdef DEBUG_TIME
@@ -17,7 +19,99 @@
#define FR_END_TIMER(name, label)
#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 {
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() {
ov::Core core;
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::CPU));
if (engineType == EngineType::NVIDIA_GPU) {
// 1. Load ONNX model
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(onnxfile)) {
// 1. Load ONNX model (adaface by default, arcface fallback)
std::string onnxfile = ResolveRecognizerModelFile();
if (!onnxfile.empty()) {
_modelFilePath = onnxfile;
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading ansfacerecognizer weight",
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::Initialize. Model ansfacerecognizer.onnx does not exist",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::Initialize. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) exists",
_modelFolder, __FILE__, __LINE__);
return false;
}
@@ -97,8 +191,10 @@ namespace ANSCENTER {
// comfortably on 8 GB GPUs while freeing ~1.5 GB VRAM vs max=32
// — most scenes have ≤4 faces visible, so throughput cost is
// near-zero (amortized per-face latency drops too at lower batch).
m_options.optBatchSize = 4;
m_options.maxBatchSize = 4;
// VramCappedBatch drops this to 1 on sub-8 GB cards.
const int frBatch = VramCappedBatch(4, _modelConfig.gpuDeviceIndex);
m_options.optBatchSize = frBatch;
m_options.maxBatchSize = frBatch;
m_options.maxInputHeight = GPU_FACE_HEIGHT;
m_options.minInputHeight = GPU_FACE_HEIGHT;
@@ -127,6 +223,13 @@ namespace ANSCENTER {
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)
if (!m_gpuStream) {
m_gpuStream = std::make_unique<cv::cuda::Stream>();
@@ -135,24 +238,28 @@ namespace ANSCENTER {
}
else {
#ifdef USE_ONNX_ENGINE
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(faceidModel)) {
std::string faceidModel = ResolveRecognizerModelFile();
if (!faceidModel.empty()) {
_modelFilePath = faceidModel;
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading ANSFaceRecognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::Initialize. Model ansfacerecognizer.onnx does not exist",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::Initialize. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) exists",
_modelFolder, __FILE__, __LINE__);
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());
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) {
_logger.LogFatal("ANSFaceRecognizer::Initialize",
"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;
}
#else
@@ -201,29 +308,29 @@ namespace ANSCENTER {
if (!result) return false;
if (engineType == EngineType::NVIDIA_GPU) {
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(onnxfile)) {
std::string onnxfile = ResolveRecognizerModelFile();
if (!onnxfile.empty()) {
_modelFilePath = onnxfile;
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading ansfacerecognizer weight",
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::LoadModel. ansfacerecognizer.onnx not found",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::LoadModel. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) found",
_modelFolder, __FILE__, __LINE__);
return false;
}
}
else {
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
std::string faceidModel = ResolveRecognizerModelFile();
#ifdef USE_ONNX_ENGINE
if (std::filesystem::exists(faceidModel)) {
if (!faceidModel.empty()) {
_modelFilePath = faceidModel;
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading ansfacerecognizer weight",
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::LoadModel. ansfacerecognizer.onnx not found",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::LoadModel. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) found",
_modelFolder, __FILE__, __LINE__);
return false;
}
#else
@@ -259,9 +366,10 @@ namespace ANSCENTER {
// engine built/looked-up here has a different profile than the one
// Initialize() expects, causing either a stale `.b32` rebuild every
// launch (wasting 30-60s) or a load-time profile mismatch.
// Keep in sync with lines 100-114.
m_options.optBatchSize = 4;
m_options.maxBatchSize = 4;
// Keep in sync with the Initialize() block (same VramCappedBatch call).
const int frBatch = VramCappedBatch(4, _modelConfig.gpuDeviceIndex);
m_options.optBatchSize = frBatch;
m_options.maxBatchSize = frBatch;
m_options.maxInputHeight = GPU_FACE_HEIGHT;
m_options.minInputHeight = GPU_FACE_HEIGHT;
@@ -490,8 +598,12 @@ namespace ANSCENTER {
if (engineType == EngineType::NVIDIA_GPU) {
// Initialize TensorRT engine via shared pool
if (!m_trtEngine) {
m_options.optBatchSize = 8;
m_options.maxBatchSize = 32;
// Note this path still uses the legacy 8/32 profile on 8 GB+
// 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.deviceIndex = _modelConfig.gpuDeviceIndex;
m_options.engineFileDir = _modelFolder;
@@ -751,7 +863,7 @@ namespace ANSCENTER {
}
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
cv::Mat srcImg = inputImage;
if (srcImg.channels() == 1) {
@@ -763,8 +875,13 @@ namespace ANSCENTER {
} else {
cpuResized = srcImg;
}
// ArcFace needs RGB; AdaFace consumes the Mat's native BGR.
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_gpuStream->waitForCompletion();
@@ -880,14 +997,19 @@ namespace ANSCENTER {
cv::cuda::resize(d_img, d_resized, targetSize, 0, 0, cv::INTER_LINEAR, *m_gpuStream);
d_img = d_resized;
}
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));
// ArcFace needs RGB; AdaFace consumes native BGR.
if (m_swapRB) {
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 {
const auto& roi = faceROIs[i];
if (roi.empty()) continue;
// CPU preprocessing: resize + BGR→RGB before upload
// CPU preprocessing: resize + channel order before upload
cv::Mat srcImg = roi;
if (srcImg.channels() == 1) {
cv::cvtColor(srcImg, srcImg, cv::COLOR_GRAY2BGR);
@@ -899,7 +1021,11 @@ namespace ANSCENTER {
cpuResized = srcImg;
}
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;
d_rgb.upload(cpuRGB, *m_gpuStream);
batchGpu.emplace_back(std::move(d_rgb));
+18
View File
@@ -53,6 +53,12 @@ namespace ANSCENTER {
int GetEmbeddingSize() const { return FACE_EMBEDDING_SIZE; }
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) {
_modelConfig.unknownPersonThreshold = knownPersonThreshold;
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
@@ -68,6 +74,11 @@ namespace ANSCENTER {
private:
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)
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;
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
std::unique_ptr<GlintArcFace> faceRecognizer = nullptr;
const int CPU_FACE_WIDTH = 112;
+1
View File
@@ -4,6 +4,7 @@ set(ANSFR_SOURCES
ANSFR.cpp
ANSFRCommon.cpp
ANSFaceRecognizer.cpp
ONNXFaceAttributes.cpp
ANSGpuFrameRegistry.cpp
GpuNV12SlotPool.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) {
try {
// 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,
std::vector<std::string>& userNames, std::vector<std::vector<int>>& faceIdsByUser);
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();
int GetEmbeddings(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) {
ANS_DBG("ANSFR","CheckFaceEmbedding: HandlePtr=%p, *Handle=%p, bufLen=%u",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), bufferLength);
//return 1; // by pass
try {
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);
@@ -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(
ANSCENTER::ANSFacialRecognition** Handle,
cv::Mat** cvImage,
+1 -1
View File
@@ -22,7 +22,7 @@ namespace ANSCENTER
{1005, "ANSOCR"},
{1006, "ANSALPR"},
{1007, "ANSCV"},
{1008, "ANSSRT"}
{1008, "ANSTS"}
};
ansllmLicenceValid = false;
for (const auto& [productId, productName] : licenseChecks) {
@@ -162,7 +162,7 @@ bool PaddleOCRV5Engine::Initialize(const std::string& detModelPath,
// Ensure this DLL's copy of Ort::Global<void>::api_ is initialized.
// BasicOrtHandler sets it in ONNXEngine.dll, but each DLL has its own
// inline-static copy. Without this, inference calls from ANSOCR.dll crash.
if (Ort::Global<void>::api_ == nullptr) {
if (Ort::detail::Global::Api() == nullptr) {
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
}
+72
View File
@@ -20,6 +20,7 @@
#include <chrono>
#include "ANSMOT.h"
#include "onnxruntime_cxx_api.h"
#include "ANSModelSource.h"
#define USEONNXOV
#define RETAINDETECTEDFRAMES 80
@@ -754,6 +755,11 @@ namespace ANSCENTER
virtual void AddEmbedding(const std::string& className, const std::vector<float>& embedding) =0;
virtual bool Destroy()=0;
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();
// Utility functions
@@ -761,6 +767,16 @@ namespace ANSCENTER
float CosineSimilarity(const std::vector<float>& a, const std::vector<float>& b, bool normalized);
};
class ANSENGINE_API ANSODBase {
public:
// Explicit non-copy/non-move: _modelLoading (std::atomic) deletes the
// implicit copy ops; spelling them out here gives ClangCL a clean
// anchor and documents that subclasses are never copied/moved (they
// are heap-allocated and passed by shared_ptr<...>).
ANSODBase(const ANSODBase&) = delete;
ANSODBase& operator=(const ANSODBase&) = delete;
ANSODBase(ANSODBase&&) = delete;
ANSODBase& operator=(ANSODBase&&) = delete;
ANSODBase() = default;
protected:
bool _licenseValid{ false };
bool _isInitialized{ false };
@@ -782,6 +798,55 @@ namespace ANSCENTER
void CheckLicense();
void LoadClassesFromString();
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 | darknet
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;
// Guard: true while Initialize/LoadModel is in progress on any thread.
// Inference callers can check this to fail-fast instead of blocking.
@@ -1085,6 +1150,13 @@ namespace ANSCENTER
std::vector<Object> RunInferenceInTrackingMode(const cv::Mat& input, const std::string& camera_id, std::vector<int> trackingObjectIds);// Get detected objects
};
class ANSENGINE_API ANSFDBase {
public:
// Explicit non-copy/non-move (see ANSODBase for rationale).
ANSFDBase(const ANSFDBase&) = delete;
ANSFDBase& operator=(const ANSFDBase&) = delete;
ANSFDBase(ANSFDBase&&) = delete;
ANSFDBase& operator=(ANSFDBase&&) = delete;
ANSFDBase() = default;
protected:
bool _licenseValid{ false };
bool _isInitialized{ false };
@@ -1316,7 +1316,7 @@ namespace ANSCENTER
{
try {
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(ANSCENTER::EPLoader::GetOrtApiRaw()));
std::cout << "[ANSFDBase] EP ready: "<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
+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
#include <set>
#include <cstring>
#include <cmath>
#include <json.hpp>
#include "ANSODEngine.h"
#include "ANSLicense.h" // ANS_DBG macro
#include "ANSModelOpen.h"
#include "OpenVINODeviceConfig.h"
#include "ANSYOLOOD.h"
#include "ANSTENSORRTOD.h"
@@ -61,6 +63,93 @@ namespace ANSCENTER
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)
{
try {
@@ -69,6 +158,7 @@ namespace ANSCENTER
_modelConfigFile = "";
_modelFolder.clear();
_modelConfigFile.clear();
WipeModelPackage(); // whatever was loaded before, package or not
// 0. Check if the modelZipFilePath exist?
if (!FileExist(modelZipFilePath)) {
if (modelZipFilePath.empty()) {// Support motion detector that does not need model file
@@ -82,6 +172,10 @@ namespace ANSCENTER
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)
std::string outputFolder;
std::vector<std::string> passwordArray;
@@ -126,6 +220,7 @@ namespace ANSCENTER
_modelFolder.clear();
_modelConfigFile.clear();
_modelConfig = modelConfig;
WipeModelPackage(); // whatever was loaded before, package or not
CheckLicense();
if (!_licenseValid) {
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)
std::string outputFolder;
std::vector<std::string> passwordArray;
@@ -952,6 +1053,10 @@ namespace ANSCENTER
ANSODBase::~ANSODBase() {
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()
if (!_cameras.empty())
{
+8 -8
View File
@@ -55,13 +55,18 @@ namespace ANSCENTER
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)
{
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);
try {
inputImageShape_ = targetInputShape;
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXCL] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -198,12 +203,7 @@ namespace ANSCENTER
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
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
session_ = MakeSession(env_, model, sessionOptions_);
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 << "[ANSONNXCL] Initialized successfully. Model: " << modelPath << std::endl;
std::cout << "[ANSONNXCL] Initialized successfully. Model: " << ModelSourceName(model) << std::endl;
// ── Warmup ──────────────────────────────────────────────────────────
DEBUG_PRINT("Starting model warmup...");
+10
View File
@@ -2,6 +2,7 @@
#define ANSONNXCL_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
#include <memory>
@@ -25,6 +26,13 @@ namespace ANSCENTER {
// ────────────────────────────────────────────────────────────────────────
class ANSENGINE_API ANSONNXCL :public ANSODBase {
public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXCL(const ANSONNXCL&) = delete;
ANSONNXCL& operator=(const ANSONNXCL&) = delete;
ANSONNXCL(ANSONNXCL&&) = delete;
ANSONNXCL& operator=(ANSONNXCL&&) = delete;
ANSONNXCL() = default;
virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override;
virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override;
virtual bool LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap)override;
@@ -58,6 +66,8 @@ namespace ANSCENTER {
const std::string& strategy = "resize");
void warmupModel();
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);
// outputTensors is non-const because GetTensorMutableData() (the
// ORT API that doesn't hang on AMD DirectML) requires a non-const
+31 -7
View File
@@ -686,13 +686,18 @@ namespace ANSCENTER {
}
}
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);
try {
deviceId_ = deviceId;
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXOBB] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -834,12 +839,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
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
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
@@ -1394,6 +1394,19 @@ namespace ANSCENTER {
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_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)) {
ModelType modelType;
std::vector<int> inputShape;
@@ -1454,6 +1467,17 @@ namespace ANSCENTER {
// if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true;
_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
if (FileExist(_modelConfigFile)) {
ModelType modelType;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXOBB_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
namespace ANSCENTER {
@@ -73,6 +74,8 @@ namespace ANSCENTER {
void warmupModel();
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);
// outputTensors is non-const because GetTensorMutableData() requires
// a non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+31 -7
View File
@@ -270,13 +270,18 @@ namespace ANSCENTER {
}
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);
try {
deviceId_ = deviceId;
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXPOSE] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -418,12 +423,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
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
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
@@ -1282,6 +1282,19 @@ namespace ANSCENTER {
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_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)) {
ModelType modelType;
std::vector<int> inputShape;
@@ -1343,6 +1356,17 @@ namespace ANSCENTER {
// if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true;
_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
if (FileExist(_modelConfigFile)) {
ModelType modelType;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXPOSE_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
namespace ANSCENTER {
@@ -40,6 +41,8 @@ namespace ANSCENTER {
float kptThreshold = 0.5);
void warmupModel();
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);
// outputTensors is non-const because GetTensorMutableData() requires
// a non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+7
View File
@@ -108,6 +108,13 @@ namespace ANSCENTER {
// ====================================================================
class ANSENGINE_API ANSONNXRTMPOSE : public ANSODBase {
public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXRTMPOSE(const ANSONNXRTMPOSE&) = delete;
ANSONNXRTMPOSE& operator=(const ANSONNXRTMPOSE&) = delete;
ANSONNXRTMPOSE(ANSONNXRTMPOSE&&) = delete;
ANSONNXRTMPOSE& operator=(ANSONNXRTMPOSE&&) = delete;
ANSONNXRTMPOSE() = default;
bool Initialize(std::string licenseKey, ModelConfig modelConfig,
const std::string& modelZipFilePath,
const std::string& modelZipPassword,
+1 -1
View File
@@ -204,7 +204,7 @@ namespace ANSCENTER
}
try {
m_engine = std::make_unique<ONNXSAM3>(modelFolder, EngineType::NVIDIA_GPU);
m_engine = std::make_unique<class ONNXSAM3>(modelFolder, EngineType::NVIDIA_GPU);
m_tokenLength = m_engine->getTokenLength();
_logger.LogDebug("ANSONNXSAM3::InitEngine",
+11 -1
View File
@@ -16,6 +16,13 @@ namespace ANSCENTER
class ANSENGINE_API ANSONNXSAM3 : public ANSODBase
{
public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXSAM3(const ANSONNXSAM3&) = delete;
ANSONNXSAM3& operator=(const ANSONNXSAM3&) = delete;
ANSONNXSAM3(ANSONNXSAM3&&) = delete;
ANSONNXSAM3& operator=(ANSONNXSAM3&&) = delete;
ANSONNXSAM3() = default;
virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig,
const std::string& modelZipFilePath, const std::string& modelZipPassword,
std::string& labelMap) override;
@@ -45,7 +52,10 @@ namespace ANSCENTER
bool _fp16 = false;
// ONNX Runtime engine
std::unique_ptr<ONNXSAM3> m_engine;
// Elaborated `class ONNXSAM3` disambiguates from the
// ANSCENTER::ModelType::ONNXSAM3 enumerator (ANSLicense.h) which
// shares the same unqualified name in this namespace.
std::unique_ptr<class ONNXSAM3> m_engine;
// Tokenizer (loaded from merges.txt)
std::unique_ptr<ANSCLIPTokenizer> m_tokenizer;
+31 -7
View File
@@ -267,13 +267,18 @@ namespace ANSCENTER {
}
}
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);
try {
deviceId_ = deviceId;
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXSEG] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -415,12 +420,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
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
session = MakeSession(env, model, sessionOptions);
numInputNodes = session.GetInputCount();
numOutputNodes = session.GetOutputCount();
@@ -1172,6 +1172,19 @@ namespace ANSCENTER {
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_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)) {
ModelType modelType;
std::vector<int> inputShape;
@@ -1233,6 +1246,17 @@ namespace ANSCENTER {
// if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true;
_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
if (FileExist(_modelConfigFile)) {
ModelType modelType;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXSEG_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
namespace ANSCENTER {
@@ -50,6 +51,8 @@ namespace ANSCENTER {
void warmupModel();
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);
// outputs is non-const because GetTensorMutableData() requires a
// non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+281 -31
View File
@@ -24,6 +24,7 @@ namespace ANSCENTER {
else {
inputImageShape = cv::Size(640, 640);
}
detectDarknetLayout();
}
ONNXYOLO::ONNXYOLO(const std::string& _onnx_path, EngineType engineType,
@@ -39,6 +40,77 @@ namespace ANSCENTER {
else {
inputImageShape = cv::Size(640, 640);
}
detectDarknetLayout();
}
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);
}
detectDarknetLayout();
}
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);
}
detectDarknetLayout();
}
// ------------------------------------------------------------------
// detectDarknetLayout — recognise a Darknet/YOLO export (hank-ai
// darknet_onnx_export) from its outputs, once, at load time:
// confs [B,N,nc] (objectness x class probability)
// boxes [B,N,1,4] or [B,N,4] (x1,y1,x2,y2 normalised 0..1)
// Matched by name first, then by shape. Must be decided before the
// segmentation check in detect(), which would otherwise take the 4-D
// boxes tensor for proto masks.
// ------------------------------------------------------------------
void ONNXYOLO::detectDarknetLayout()
{
isDarknetLayout = false;
if (num_outputs != 2 || output_node_dims.size() != 2) return;
if (output_node_names_.size() == 2) {
const bool byName =
(output_node_names_[0] == "confs" && output_node_names_[1] == "boxes") ||
(output_node_names_[0] == "boxes" && output_node_names_[1] == "confs");
if (byName) { isDarknetLayout = true; return; }
}
// By shape: exactly one output ends in 4 (the boxes), and both
// carry the same number of candidates N in dim 1.
auto isBoxes = [](const std::vector<int64_t>& d) {
return (d.size() == 4 && d[2] == 1 && d[3] == 4) ||
(d.size() == 3 && d[2] == 4);
};
const auto& d0 = output_node_dims[0];
const auto& d1 = output_node_dims[1];
if (isBoxes(d0) == isBoxes(d1)) return;
const auto& boxes = isBoxes(d0) ? d0 : d1;
const auto& confs = isBoxes(d0) ? d1 : d0;
if (confs.size() != 3 || confs[2] < 1) return;
if (confs[1] <= 0 || confs[1] != boxes[1]) return;
isDarknetLayout = true;
}
// ------------------------------------------------------------------
@@ -112,6 +184,11 @@ namespace ANSCENTER {
// Classification: direct resize (no letterbox padding) — matches ANSONNXCL
cv::resize(bgrMat, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
0, 0, cv::INTER_LINEAR);
} else if (isDarknetLayout) {
// Darknet: stretch the whole frame to the network size (no
// letterbox) — Darknet trains and predicts on a stretched square
cv::resize(bgrMat, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
0, 0, cv::INTER_LINEAR);
} else {
// Detection/Seg/Pose/OBB: Ultralytics letterbox
letterBox(bgrMat, canvas, inputImageShape);
@@ -129,14 +206,14 @@ namespace ANSCENTER {
// future ORT shape-mismatch failure (e.g. /model.12/Concat for
// non-stride-32 inputs) points at the offending HxW directly.
// One line per session — does not flood DebugView.
static thread_local const ONNXYOLO* s_loggedFirst = nullptr;
static thread_local const class ONNXYOLO* s_loggedFirst = nullptr;
if (s_loggedFirst != this) {
s_loggedFirst = this;
ANS_DBG("ONNXYOLO",
"transformInto[first]: inputImageShape=(%dx%d) canvas=%dx%d cls=%d src=%dx%d dyn=%d",
"transformInto[first]: inputImageShape=(%dx%d) canvas=%dx%d cls=%d src=%dx%d dyn=%d darknet=%d",
inputImageShape.width, inputImageShape.height, width, height,
isClassification ? 1 : 0, mat.cols, mat.rows,
isDynamicInputShape ? 1 : 0);
isDynamicInputShape ? 1 : 0, isDarknetLayout ? 1 : 0);
}
outDims = { 1, 3, height, width };
@@ -191,6 +268,10 @@ namespace ANSCENTER {
// Classification: direct resize (no letterbox)
cv::resize(bgrImg, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
0, 0, cv::INTER_LINEAR);
} else if (isDarknetLayout) {
// Darknet: stretch resize (no letterbox)
cv::resize(bgrImg, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
0, 0, cv::INTER_LINEAR);
} else {
letterBox(bgrImg, canvas, inputImageShape);
}
@@ -257,6 +338,7 @@ namespace ANSCENTER {
// detect — full pipeline with auto task detection
//
// Decision logic:
// darknet layout (load time) → darknet detection
// 2 outputs (second 4D) → segmentation
// 1 output, 2D → classification
// 1 output, 3D end2end dim2=6 → detection
@@ -335,6 +417,12 @@ namespace ANSCENTER {
const size_t numOutputs = outputTensors.size();
// ── Darknet: confs + boxes (checked before seg — boxes is 4-D) ──
if (isDarknetLayout) {
return postprocessDarknet(image.size(), outputTensors, classNames,
confThreshold, iouThreshold);
}
// ── Segmentation: 2 outputs (detections + proto masks) ──────────
if (numOutputs >= 2) {
const auto protoShape = outputTensors[1].GetTensorTypeAndShapeInfo().GetShape();
@@ -1361,6 +1449,118 @@ namespace ANSCENTER {
return results;
}
// ====================================================================
// DARKNET — postprocess
//
// hank-ai darknet_onnx_export (v5.1+): confs [B,N,nc] is already
// objectness x class probability; boxes [B,N,1,4] holds corners
// x1,y1,x2,y2 normalised 0..1 to the (stretched) network input. The
// graph has no NMS. Best class per candidate, threshold, map to the
// original image (a stretch, so x*origW / y*origH), clip, then
// class-aware NMS.
// ====================================================================
std::vector<Object> ONNXYOLO::postprocessDarknet(
const cv::Size& originalImageSize,
std::vector<Ort::Value>& outputTensors,
const std::vector<std::string>& classNames,
float confThreshold, float iouThreshold, int maxDet)
{
if (outputTensors.size() < 2) return {};
// Match by name first, then by shape (the boxes tensor ends in 4)
int confsIdx = -1, boxesIdx = -1;
for (size_t t = 0; t < outputTensors.size() && t < output_node_names_.size(); ++t) {
if (output_node_names_[t] == "confs") confsIdx = static_cast<int>(t);
else if (output_node_names_[t] == "boxes") boxesIdx = static_cast<int>(t);
}
if (confsIdx < 0 || boxesIdx < 0) {
confsIdx = boxesIdx = -1;
for (size_t t = 0; t < outputTensors.size(); ++t) {
const auto sh = outputTensors[t].GetTensorTypeAndShapeInfo().GetShape();
if (!sh.empty() && sh.back() == 4 && boxesIdx < 0) boxesIdx = static_cast<int>(t);
else if (confsIdx < 0) confsIdx = static_cast<int>(t);
}
}
if (confsIdx < 0 || boxesIdx < 0) return {};
const auto confsShape = outputTensors[confsIdx].GetTensorTypeAndShapeInfo().GetShape();
const auto boxesInfo = outputTensors[boxesIdx].GetTensorTypeAndShapeInfo();
if (confsShape.size() != 3) return {};
const int numBoxes = static_cast<int>(confsShape[1]);
const int numClasses = static_cast<int>(confsShape[2]);
if (numBoxes <= 0 || numClasses <= 0) return {};
if (boxesInfo.GetElementCount() != static_cast<size_t>(numBoxes) * 4) return {};
const float* confs = outputTensors[confsIdx].GetTensorMutableData<float>();
const float* boxes = outputTensors[boxesIdx].GetTensorMutableData<float>();
const float origW = static_cast<float>(originalImageSize.width);
const float origH = static_cast<float>(originalImageSize.height);
struct Candidate { float x1, y1, x2, y2, conf; int classId; };
std::vector<Candidate> candidates;
for (int i = 0; i < numBoxes; ++i) {
const float* scoresPtr = confs + static_cast<size_t>(i) * numClasses;
float maxScore = scoresPtr[0];
int bestClass = 0;
for (int c = 1; c < numClasses; ++c) {
if (scoresPtr[c] > maxScore) { maxScore = scoresPtr[c]; bestClass = c; }
}
if (maxScore <= confThreshold) continue;
const float* b = boxes + static_cast<size_t>(i) * 4;
float x1 = clamp(b[0] * origW, 0.f, origW);
float y1 = clamp(b[1] * origH, 0.f, origH);
float x2 = clamp(b[2] * origW, 0.f, origW);
float y2 = clamp(b[3] * origH, 0.f, origH);
if (x2 - x1 < 1.f || y2 - y1 < 1.f) continue;
candidates.push_back({ x1, y1, x2, y2, maxScore, bestClass });
}
if (candidates.empty()) return {};
// Class-aware NMS (boxes are already in image pixels, so compare
// class ids rather than offsetting by a fixed MAX_WH)
std::vector<int> sortedIdx(candidates.size());
std::iota(sortedIdx.begin(), sortedIdx.end(), 0);
std::sort(sortedIdx.begin(), sortedIdx.end(),
[&](int a, int b) { return candidates[a].conf > candidates[b].conf; });
std::vector<bool> suppressed(sortedIdx.size(), false);
std::vector<Object> results;
results.reserve(std::min(static_cast<int>(sortedIdx.size()), maxDet));
for (size_t i = 0; i < sortedIdx.size() && static_cast<int>(results.size()) < maxDet; ++i) {
if (suppressed[i]) continue;
const auto& cur = candidates[sortedIdx[i]];
const float curArea = (cur.x2 - cur.x1) * (cur.y2 - cur.y1);
for (size_t j = i + 1; j < sortedIdx.size(); ++j) {
if (suppressed[j]) continue;
const auto& o = candidates[sortedIdx[j]];
if (o.classId != cur.classId) continue;
float iw = std::min(cur.x2, o.x2) - std::max(cur.x1, o.x1);
float ih = std::min(cur.y2, o.y2) - std::max(cur.y1, o.y1);
if (iw <= 0.f || ih <= 0.f) continue;
float inter = iw * ih;
float ua = curArea + (o.x2 - o.x1) * (o.y2 - o.y1) - inter;
if (ua > 0.f && inter / ua > iouThreshold) suppressed[j] = true;
}
Object obj;
obj.classId = cur.classId;
obj.confidence = cur.conf;
obj.box = cv::Rect(static_cast<int>(cur.x1), static_cast<int>(cur.y1),
static_cast<int>(cur.x2 - cur.x1), static_cast<int>(cur.y2 - cur.y1));
if (cur.classId >= 0 && cur.classId < static_cast<int>(classNames.size()))
obj.className = classNames[cur.classId];
results.push_back(std::move(obj));
}
return results;
}
// ====================================================================
// CLASSIFICATION — postprocess
// ====================================================================
@@ -1538,6 +1738,11 @@ namespace ANSCENTER {
}
// Dispatch to correct postprocess method
if (isDarknetLayout) {
results[i] = postprocessDarknet(originalSizes[i], perImageOutputs, classNames,
confThreshold, iouThreshold);
continue;
}
if (numOutputs >= 2) {
const auto protoShape = outputTensors[1].GetTensorTypeAndShapeInfo().GetShape();
if (protoShape.size() == 4) {
@@ -1675,12 +1880,28 @@ namespace ANSCENTER {
bool ANSONNXYOLO::InitOrtEngine() {
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);
// The manifest's family outranks what the outputs look like.
if (_packageFamily == "darknet") m_ortEngine->setDarknetLayout(true);
return true;
}
if (!FileExist(_modelFilePath)) {
_logger.LogError("ANSONNXYOLO::InitOrtEngine",
"Model file does not exist: " + _modelFilePath, __FILE__, __LINE__);
return false;
}
m_ortEngine = std::make_shared<ONNXYOLO>(_modelFilePath);
m_ortEngine = std::make_shared<class ONNXYOLO>(_modelFilePath);
return true;
}
catch (const std::exception& e) {
@@ -1691,12 +1912,23 @@ namespace ANSCENTER {
bool ANSONNXYOLO::InitOrtEngine(ANSCENTER::EngineType engineType) {
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);
if (_packageFamily == "darknet") m_ortEngine->setDarknetLayout(true);
return true;
}
if (!FileExist(_modelFilePath)) {
_logger.LogError("ANSONNXYOLO::InitOrtEngine",
"Model file does not exist: " + _modelFilePath, __FILE__, __LINE__);
return false;
}
m_ortEngine = std::make_shared<ONNXYOLO>(_modelFilePath, engineType);
m_ortEngine = std::make_shared<class ONNXYOLO>(_modelFilePath, engineType);
return true;
}
catch (const std::exception& e) {
@@ -1846,21 +2078,30 @@ namespace ANSCENTER {
NUM_KPS = _modelConfig.numKPS;
KPS_THRESHOLD = _modelConfig.kpsThreshold;
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
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];
// A package has already supplied the classes and the geometry;
// there is no folder to look in.
if (!_fromPackage) {
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
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();
}
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
// The defaults above have just overwritten what the package
// said its input size was; put it back.
ApplyPackageInputSize();
}
labelMap.clear();
@@ -1920,21 +2161,30 @@ namespace ANSCENTER {
NUM_KPS = _modelConfig.numKPS;
KPS_THRESHOLD = _modelConfig.kpsThreshold;
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
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];
// A package has already supplied the classes and the geometry;
// there is no folder to look in.
if (!_fromPackage) {
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
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();
}
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
// The defaults above have just overwritten what the package
// said its input size was; put it back.
ApplyPackageInputSize();
}
if (this->_loadEngineOnCreation) {
@@ -2244,7 +2494,7 @@ namespace ANSCENTER {
int numKps;
std::vector<std::string> classes;
bool trackerEnabled, stabilizationEnabled;
std::shared_ptr<ONNXYOLO> engine;
std::shared_ptr<class ONNXYOLO> engine;
{
ANS_DBG("ONNXYOLO", "DetectObjects: cam=%s acquiring mutex...", camera_id.c_str());
auto lk = TryLockWithTimeout("ANSONNXYOLO::DetectObjects");
@@ -2446,7 +2696,7 @@ namespace ANSCENTER {
int numKps;
std::vector<std::string> classes;
bool trackerEnabled, stabilizationEnabled;
std::shared_ptr<ONNXYOLO> engine;
std::shared_ptr<class ONNXYOLO> engine;
{
auto lk = TryLockWithTimeout("ANSONNXYOLO::DetectObjectsBatch");
if (!lk.owns_lock()) return {};
+46 -1
View File
@@ -19,6 +19,7 @@ namespace ANSCENTER {
// - Segmentation: [B,300,38]+protos or [B, nc+36, N]+protos
// - Pose : [B,300,6+nk*3] or [B, nc+4+nk*3, N] legacy
// - Classification: [B, nc]
// - Darknet detect: confs [B,N,nc] + boxes [B,N,1,4] (stretch resize)
//
// Preprocessing follows the exact Ultralytics LetterBox transform:
// - center=True, stride=32, pad_value=114
@@ -33,6 +34,16 @@ namespace ANSCENTER {
explicit ONNXYOLO(const std::string& _onnx_path,
EngineType engineType,
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;
/// Run inference on a single image.
@@ -85,6 +96,14 @@ namespace ANSCENTER {
/// True if the ONNX model has dynamic spatial dimensions.
bool hasDynamicInputShape() const { return isDynamicInputShape; }
/// Darknet/YOLO layout (hank-ai darknet_onnx_export): outputs
/// confs [B,N,nc] + boxes [B,N,1,4], stretch resize, no letterbox.
/// Detected from the output names/shapes at construction; the
/// wrapper also forces it when an .ansmodel package says its
/// family is "darknet".
void setDarknetLayout(bool darknet) { isDarknetLayout = darknet; }
bool hasDarknetLayout() const { return isDarknetLayout; }
private:
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
@@ -145,6 +164,13 @@ namespace ANSCENTER {
const std::vector<std::string>& classNames,
float confThreshold, float iouThreshold, int numKPS, int maxDet = 300);
// ── Darknet postprocess (confs + boxes, no NMS in the graph) ────
std::vector<Object> postprocessDarknet(
const cv::Size& originalImageSize,
std::vector<Ort::Value>& outputTensors,
const std::vector<std::string>& classNames,
float confThreshold, float iouThreshold, int maxDet = 300);
// ── Classification postprocess ──────────────────────────────────
std::vector<Object> postprocessClassify(
std::vector<Ort::Value>& outputTensors,
@@ -179,6 +205,12 @@ namespace ANSCENTER {
cv::Size inputImageShape;
bool isDynamicInputShape{ false };
// Darknet layout: decided once at load time (see setDarknetLayout).
// Selects the stretch resize in transformInto / transformBatchInto
// and postprocessDarknet in detect / detectBatch.
bool isDarknetLayout{ false };
void detectDarknetLayout();
// Per-call preprocess that writes into caller-provided buffer + dims
// instead of the BasicOrtHandler members (input_values_handler /
// input_node_dims). detect() / detectBatch() use these so multiple
@@ -205,6 +237,16 @@ namespace ANSCENTER {
// ====================================================================
class ANSENGINE_API ANSONNXYOLO : public ANSODBase {
public:
// Explicit non-copy/non-move. The class has atomic + shared_ptr members
// that already implicitly delete these, but __declspec(dllexport)
// makes ClangCL eagerly instantiate the implicit copy/move ctor
// bodies; an explicit user-declared =delete short-circuits that.
ANSONNXYOLO(const ANSONNXYOLO&) = delete;
ANSONNXYOLO& operator=(const ANSONNXYOLO&) = delete;
ANSONNXYOLO(ANSONNXYOLO&&) = delete;
ANSONNXYOLO& operator=(ANSONNXYOLO&&) = delete;
ANSONNXYOLO() = default;
bool Initialize(std::string licenseKey, ModelConfig modelConfig,
const std::string& modelZipFilePath,
const std::string& modelZipPassword,
@@ -254,7 +296,10 @@ namespace ANSCENTER {
// caught a DML E_FAIL and rebuilt the session — that was the
// 0xc0000005 access violation observed in ANSODEngine.dll under
// AMD/DirectML when multiple AI tasks ran in parallel.
std::shared_ptr<ONNXYOLO> m_ortEngine;
// Elaborated `class ONNXYOLO` disambiguates from the
// ANSCENTER::ModelType::ONNXYOLO enumerator (ANSLicense.h) which
// shares the same unqualified name in this namespace.
std::shared_ptr<class ONNXYOLO> m_ortEngine;
// ────────────────────────────────────────────────────────────────
// DML stay-on-GPU recovery state.
+230 -34
View File
@@ -11,12 +11,37 @@
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
// ====================================================================
bool ANSRTYOLO::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
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)) {
_logger.LogFatal("ANSRTYOLO::OptimizeModel", "Raw model file path does not exist", __FILE__, __LINE__);
return false;
@@ -96,21 +121,33 @@ namespace ANSCENTER {
m_options.engineFileDir = _modelFolder;
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
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];
}
if (_fromPackage) {
// The package already carries the classes and the geometry,
// and there is no folder to look in. All that is needed here
// is where the built plan lives.
_modelFilePath = PackagePlanBasePath(modelZipFilePath);
m_options.engineFileDir = GetParentFolder(_modelFilePath);
// The defaults above have just overwritten the input size the
// package asked for; put it back.
ApplyPackageInputSize();
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
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) {
@@ -119,7 +156,9 @@ namespace ANSCENTER {
static_cast<int>(m_options.precision), m_options.maxBatchSize };
m_trtEngine = EnginePoolManager<float>::instance().acquire(
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);
}
if (!m_trtEngine) {
@@ -130,6 +169,7 @@ namespace ANSCENTER {
}
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
DetectDarknetLayout();
m_trtEngine->warmUp();
}
_modelLoadValid = true;
@@ -221,7 +261,9 @@ namespace ANSCENTER {
static_cast<int>(m_options.precision), m_options.maxBatchSize };
m_trtEngine = EnginePoolManager<float>::instance().acquire(
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);
}
if (!m_trtEngine) {
@@ -232,6 +274,7 @@ namespace ANSCENTER {
}
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
DetectDarknetLayout();
m_trtEngine->warmUp();
}
_modelLoadValid = true;
@@ -295,21 +338,33 @@ namespace ANSCENTER {
m_options.engineFileDir = _modelFolder;
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
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];
}
if (_fromPackage) {
// The package already carries the classes and the geometry,
// and there is no folder to look in. All that is needed here
// is where the built plan lives.
_modelFilePath = PackagePlanBasePath(modelZipFilePath);
m_options.engineFileDir = GetParentFolder(_modelFilePath);
// The defaults above have just overwritten the input size the
// package asked for; put it back.
ApplyPackageInputSize();
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
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();
@@ -322,7 +377,9 @@ namespace ANSCENTER {
static_cast<int>(m_options.precision), m_options.maxBatchSize };
m_trtEngine = EnginePoolManager<float>::instance().acquire(
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);
}
if (!m_trtEngine) {
@@ -333,6 +390,7 @@ namespace ANSCENTER {
}
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
DetectDarknetLayout();
m_trtEngine->warmUp();
}
_modelLoadValid = true;
@@ -456,6 +514,9 @@ namespace ANSCENTER {
if (srcImg.rows != inputH || srcImg.cols != inputW) {
if (isClassification) {
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
} else if (_isDarknet) {
// Darknet: stretch the whole frame (no letterbox)
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
} else {
cpuResized = Engine<float>::cpuResizeKeepAspectRatioPadRightBottom(srcImg, inputH, inputW);
}
@@ -866,12 +927,15 @@ namespace ANSCENTER {
const float scaleW = inputW / static_cast<float>(srcImg.cols);
const float scaleH = inputH / static_cast<float>(srcImg.rows);
outMetadata.ratios[i] = isClassification ? 1.f : 1.f / std::min(scaleW, scaleH);
outMetadata.ratios[i] = (isClassification || _isDarknet) ? 1.f : 1.f / std::min(scaleW, scaleH);
cv::Mat cpuResized;
if (srcImg.rows != inputH || srcImg.cols != inputW) {
if (isClassification) {
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
} else if (_isDarknet) {
// Darknet: stretch the whole frame (no letterbox)
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
} else {
cpuResized = Engine<float>::cpuResizeKeepAspectRatioPadRightBottom(srcImg, inputH, inputW);
}
@@ -1759,6 +1823,117 @@ namespace ANSCENTER {
}
}
// ====================================================================
// Darknet — layout detection + postprocess
//
// hank-ai darknet_onnx_export (v5.1+): confs [B,N,nc] is already
// objectness x class probability; boxes [B,N,1,4] holds corners
// x1,y1,x2,y2 normalised 0..1 to the (stretched) network input. The
// graph has no NMS. Best class per candidate, threshold, map to the
// original image (a stretch, so x*imgWidth / y*imgHeight), clip, then
// class-aware NMS. TensorRT exposes no output names here, so the
// boxes tensor is the one whose last dim is 4.
// ====================================================================
void ANSRTYOLO::DetectDarknetLayout() {
_isDarknet = false;
if (_fromPackage && _packageFamily == "darknet") {
_isDarknet = true;
return;
}
if (!m_trtEngine) return;
const auto& outputDims = m_trtEngine->getOutputDims();
if (outputDims.size() != 2) return;
auto isBoxes = [](const nvinfer1::Dims& d) {
return (d.nbDims == 4 && d.d[2] == 1 && d.d[3] == 4) ||
(d.nbDims == 3 && d.d[2] == 4);
};
const bool b0 = isBoxes(outputDims[0]);
const bool b1 = isBoxes(outputDims[1]);
if (b0 == b1) return;
const auto& boxes = b0 ? outputDims[0] : outputDims[1];
const auto& confs = b0 ? outputDims[1] : outputDims[0];
if (confs.nbDims != 3 || confs.d[2] < 1) return;
if (confs.d[1] <= 0 || confs.d[1] != boxes.d[1]) return;
_isDarknet = true;
}
std::vector<Object> ANSRTYOLO::PostprocessDarknet(
std::vector<std::vector<float>>& featureVectors,
const std::string& camera_id, const ImageMetadata& meta) {
try {
const auto& outputDims = m_trtEngine->getOutputDims();
if (outputDims.size() < 2 || featureVectors.size() < 2) return {};
int boxesIdx = -1, confsIdx = -1;
for (size_t t = 0; t < outputDims.size(); ++t) {
const auto& d = outputDims[t];
if (d.nbDims > 0 && d.d[d.nbDims - 1] == 4 && boxesIdx < 0) boxesIdx = static_cast<int>(t);
else if (confsIdx < 0) confsIdx = static_cast<int>(t);
}
if (boxesIdx < 0 || confsIdx < 0) return {};
if (outputDims[confsIdx].nbDims != 3) return {};
const int numClasses = outputDims[confsIdx].d[2];
if (numClasses <= 0) return {};
const auto& confsVec = featureVectors[confsIdx];
const auto& boxesVec = featureVectors[boxesIdx];
// Number of candidates from the data (dim 1 may be dynamic)
const int numBoxes = static_cast<int>(confsVec.size() / numClasses);
if (numBoxes <= 0 || boxesVec.size() < static_cast<size_t>(numBoxes) * 4) return {};
std::vector<cv::Rect> bboxes;
std::vector<float> scores;
std::vector<int> labels;
std::vector<int> indices;
for (int i = 0; i < numBoxes; i++) {
const float* scoresPtr = confsVec.data() + static_cast<size_t>(i) * numClasses;
const float* maxSPtr = std::max_element(scoresPtr, scoresPtr + numClasses);
float score = *maxSPtr;
if (score > _modelConfig.detectionScoreThreshold) {
const float* b = boxesVec.data() + static_cast<size_t>(i) * 4;
float x0 = std::clamp(b[0] * meta.imgWidth, 0.f, meta.imgWidth);
float y0 = std::clamp(b[1] * meta.imgHeight, 0.f, meta.imgHeight);
float x1 = std::clamp(b[2] * meta.imgWidth, 0.f, meta.imgWidth);
float y1 = std::clamp(b[3] * meta.imgHeight, 0.f, meta.imgHeight);
if (x1 - x0 < 1.f || y1 - y0 < 1.f) continue;
cv::Rect_<float> bbox;
bbox.x = x0; bbox.y = y0;
bbox.width = x1 - x0; bbox.height = y1 - y0;
bboxes.push_back(bbox);
labels.push_back(static_cast<int>(maxSPtr - scoresPtr));
scores.push_back(score);
}
}
cv::dnn::NMSBoxesBatched(bboxes, scores, labels, PROBABILITY_THRESHOLD, NMS_THRESHOLD, indices);
std::vector<Object> objects;
int classNameSize = static_cast<int>(_classes.size());
for (auto& chosenIdx : indices) {
if (static_cast<int>(objects.size()) >= TOP_K) break;
if (scores[chosenIdx] > _modelConfig.detectionScoreThreshold) {
Object obj{};
obj.confidence = scores[chosenIdx];
obj.classId = labels[chosenIdx];
obj.box = bboxes[chosenIdx];
if (!_classes.empty()) {
obj.className = (obj.classId < classNameSize)
? _classes[obj.classId] : _classes[classNameSize - 1];
}
else { obj.className = "Unknown"; }
obj.cameraId = camera_id;
objects.push_back(obj);
}
}
return objects;
}
catch (std::exception& e) {
_logger.LogFatal("ANSRTYOLO::PostprocessDarknet", e.what(), __FILE__, __LINE__);
return {};
}
}
// ====================================================================
// DetectObjects — single image with auto-detection of task type
// ====================================================================
@@ -1801,8 +1976,11 @@ namespace ANSCENTER {
const int inputW = inputDims[0].d[2];
const int inputH = inputDims[0].d[1];
// Darknet stretches rather than letterboxes: the direct
// (no-padding) resize kernel is the classification one.
auto nv12 = m_nv12Helper.tryNV12(inputImage, inferenceGpu, inputW, inputH,
NV12PreprocessHelper::defaultYOLOLauncher(),
_isDarknet ? NV12PreprocessHelper::classificationLauncher()
: NV12PreprocessHelper::defaultYOLOLauncher(),
_logger, "ANSRTYOLO");
if (nv12.succeeded) {
meta.imgWidth = nv12.metaWidth;
@@ -1857,7 +2035,14 @@ namespace ANSCENTER {
const auto& outputDims = m_trtEngine->getOutputDims();
const size_t numOutputs = outputDims.size();
if (numOutputs >= 2) {
// Darknet (confs + boxes) — checked before seg, whose test
// (2 outputs) it would otherwise pass
if (_isDarknet) {
std::vector<std::vector<float>> featureVector2d;
Engine<float>::transformOutput(featureVectors, featureVector2d);
results = PostprocessDarknet(featureVector2d, camera_id, meta);
}
else if (numOutputs >= 2) {
std::vector<std::vector<float>> featureVector2d;
Engine<float>::transformOutput(featureVectors, featureVector2d);
double msTransform = dbg ? elapsed() : 0;
@@ -2157,13 +2342,16 @@ namespace ANSCENTER {
int nc = static_cast<int>(_classes.size());
enum class TaskType { DetLegacy, DetE2E, OBBLegacy, OBBE2E,
SegLegacy, SegE2E, PoseLegacy, PoseE2E, Classify };
SegLegacy, SegE2E, PoseLegacy, PoseE2E, Classify, Darknet };
TaskType taskType = TaskType::DetLegacy; // default
// E2E: dim1 > dim2 (e.g. [B,300,6]); Legacy: dim1 < dim2 (e.g. [B,84,8400])
const bool isEndToEnd = (dim1 > dim2) || (dim2 <= 20);
if (numOutputs >= 2) {
if (_isDarknet) {
taskType = TaskType::Darknet;
}
else if (numOutputs >= 2) {
taskType = isEndToEnd ? TaskType::SegE2E : TaskType::SegLegacy;
}
else if (outputDims[0].nbDims <= 2) {
@@ -2244,6 +2432,14 @@ namespace ANSCENTER {
}
break;
}
case TaskType::Darknet: {
std::vector<std::vector<float>> fv2d(batchOutput.begin(), batchOutput.end());
postFutures.push_back(std::async(std::launch::async,
[this, fv = std::move(fv2d), cid = camera_id, m = imgMeta]() mutable {
return PostprocessDarknet(fv, cid, m);
}));
break;
}
case TaskType::Classify: {
std::vector<float> fv = batchOutput.empty() ? std::vector<float>{} : batchOutput[0];
postFutures.push_back(std::async(std::launch::async,
+18
View File
@@ -22,6 +22,7 @@ namespace ANSCENTER {
// - Segmentation: legacy [B, nc+36, N]+protos or end2end [B, 300, 38]+protos
// - Pose : legacy [B, nc+4+nk*3, N] or end2end [B, 300, 6+nk*3]
// - Classification: [B, nc]
// - Darknet detect: confs [B,N,nc] + boxes [B,N,1,4] (stretch resize)
//
// Uses Engine<float> with GPU preprocessing (cv::cuda::GpuMat),
// supports multi-GPU pool via SetMaxSlotsPerGpu.
@@ -52,6 +53,11 @@ namespace ANSCENTER {
~ANSRTYOLO();
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;
bool _modelLoadValid{ false };
bool _fp16{ false };
@@ -119,6 +125,18 @@ namespace ANSCENTER {
std::vector<float>& featureVector,
const std::string& camera_id, const ImageMetadata& meta);
// ── Darknet postprocessor (confs + boxes, no NMS in the graph) ───
std::vector<Object> PostprocessDarknet(
std::vector<std::vector<float>>& featureVectors,
const std::string& camera_id, const ImageMetadata& meta);
// Darknet/YOLO layout (hank-ai darknet_onnx_export): package family
// "darknet", or outputs shaped confs [B,N,nc] + boxes [B,N,1,4].
// Decided once, after the engine is loaded; selects the stretch
// resize (no letterbox) and PostprocessDarknet.
bool _isDarknet{ false };
void DetectDarknetLayout();
// ── OBB NMS helpers (Prob-IoU based) ─────────────────────────────
struct OrientedBox {
float x, y, width, height, angle;
+123 -7
View File
@@ -10,6 +10,12 @@ namespace ANSCENTER {
if (!ANSODBase::OptimizeModel(fp16, optimizedModelFolder)) {
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)) {
optimizedModelFolder = GetParentFolder(_modelFilePath);
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;
if (_modelConfig.modelConfThreshold < 0.2)
_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
if (FileExist(_modelConfigFile)) {
ModelType modelType;
@@ -168,6 +181,15 @@ namespace ANSCENTER {
_modelConfig.modelMNSThreshold = 0.5;
if (_modelConfig.modelConfThreshold < 0.2)
_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
if (FileExist(_modelConfigFile)) {
ModelType modelType;
@@ -278,10 +300,15 @@ namespace ANSCENTER {
}
}
bool YOLO12OD::loadModel(const std::string& modelPath, bool useGPU)
{
return loadModel(ModelSource::File(modelPath), useGPU);
}
bool YOLO12OD::loadModel(const ModelSource& model, bool useGPU)
{
try {
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[YOLO12OD] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -418,12 +445,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
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
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
@@ -530,6 +552,30 @@ namespace ANSCENTER {
const float* rawOutput = outputTensors[0].GetTensorMutableData<float>();
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
const size_t num_features = outputShape[1];
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::lock_guard<std::recursive_mutex> lock(_mutex);
try {
+12
View File
@@ -2,6 +2,7 @@
#define ANSYOL12OOD_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include <onnxruntime_cxx_api.h>
#include <algorithm>
#include <fstream>
@@ -46,6 +47,8 @@ namespace ANSCENTER {
float m_imgHeight = 0;
protected:
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);
//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);
@@ -56,6 +59,15 @@ namespace ANSCENTER {
std::vector<Ort::Value>& outputTensors,
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:
template <typename T>
typename std::enable_if<std::is_arithmetic<T>::value, T>::type
+7 -3
View File
@@ -205,10 +205,15 @@ namespace ANSCENTER
}
}
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 {
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[YOLOOD] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -358,8 +363,7 @@ namespace ANSCENTER
}
// ── Load model ──────────────────────────────────────────────────────
std::wstring w_modelPath = String2WString(modelPath.c_str());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
// ── Input shape ─────────────────────────────────────────────────────
+3
View File
@@ -2,6 +2,7 @@
#define ANSYOLOOD_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include <onnxruntime_cxx_api.h>
//#define USEOPENCVDNN
namespace ANSCENTER {
@@ -38,6 +39,8 @@ namespace ANSCENTER {
Ort::SessionOptions sessionOptions{ nullptr };
Ort::Session session{ nullptr };
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);
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);
+3
View File
@@ -113,6 +113,9 @@ target_include_directories(ANSODEngine PRIVATE
target_link_libraries(ANSODEngine
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 opencv
PRIVATE ANSMOT
@@ -2,6 +2,7 @@
#include "ANSGpuFrameRegistry.h"
#include "ANSEngineCommon.h"
#include "ANSLicense.h" // ANS_DBG macro
#include "GpuMemoryGuard.h"
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/cudawarping.hpp>
#include <opencv2/core/cuda_stream_accessor.hpp>
@@ -168,6 +169,12 @@ namespace ANSCENTER {
}
++m_cudaFailStreak;
// (3) If this failure is a GPU OOM, open the process-wide circuit
// breaker so every engine (FR/OD/OCR/LPR) backs off via UnderPressure()
// before the streak reaches CUDA_FAIL_LIMIT and the context is lost.
if (GpuMemoryGuard::IsOom(err) || GpuMemoryGuard::IsOom(pending)) {
GpuMemoryGuard::NoteOom();
}
if (m_cudaFailStreak >= CUDA_FAIL_LIMIT && !m_cudaContextDead) {
m_cudaContextDead = true;
logger.LogFatal(std::string(tag) + "::NV12Helper",
+20
View File
@@ -2,6 +2,7 @@
#include "ANSGpuFrameRegistry.h"
#include "NV12PreprocessHelper.h" // tl_currentGpuFrame()
#include "Utility.h"
#include "GpuMemoryGuard.h"
#include <chrono>
//#define FNS_DEBUG
namespace ANSCENTER {
@@ -638,6 +639,17 @@ namespace ANSCENTER {
// Phase 2: CUDA preprocessing + inference (mutex released — pool dispatches to idle GPU slot)
std::vector<std::vector<cv::cuda::GpuMat>> inputs;
bool usedNV12 = false;
// (2) Pressure guard: if free GPU VRAM is below the safety margin (or the
// OOM circuit breaker is open), SKIP this frame's GPU work instead of
// allocating and triggering an OOM that permanently poisons the context.
if (ANSCENTER::GpuMemoryGuard::UnderPressure()) {
this->_logger.LogWarn("ANSSCRFDFD::Detect",
"GPU under memory pressure — skipping SCRFD frame to avoid OOM",
__FILE__, __LINE__);
return {};
}
try {
// Clear any sticky CUDA error from transient graph-capture failures
cudaError_t priorErr = cudaGetLastError();
@@ -706,6 +718,14 @@ namespace ANSCENTER {
m_nv12Helper.tickInference();
}
catch (const std::exception& e) {
// (3) On a GPU OOM, open the circuit breaker so subsequent frames
// back off (UnderPressure) instead of repeatedly faulting the
// already-fragile CUDA context — that repetition is what corrupts it.
const std::string msg = e.what();
if (msg.find("out of memory") != std::string::npos ||
msg.find("Gpu API call") != std::string::npos) {
ANSCENTER::GpuMemoryGuard::NoteOom();
}
this->_logger.LogError(
"ANSSCRFDFD::Detect",
std::string("CUDA preprocessing failed: ") + e.what(),
+30 -2
View File
@@ -8,6 +8,7 @@
#include <climits> // INT_MIN
#include "ANSLicense.h" // ANS_DBG macro for DebugView
#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).
// Defined here, declared extern in EngineBuildLoadNetwork.inl.
@@ -433,6 +434,13 @@ extern "C" ANSODENGINE_API std::string CreateANSODHandle(ANSCENTER::ANSODBase**
// handler. Unlisted modelType values are left untouched for the switch below.
// See CreateANSODHandleEx for the full rationale — three correctness bugs
// were fixed in that dispatcher and must be kept in sync across copies.
// A sealed package is an ONNX model whatever legacy type the caller names.
// YOLOV4 = 1 goes to YOLOOD, which opens only a zip of .cfg/.weights and
// cannot read an .ansmodel; a Darknet/YOLO package (family "darknet") runs
// in the YOLO engines, which recognise its confs/boxes head. Routed as
// detection so the switch below picks TensorRT or ONNX for the hardware.
if (modelType == 1 && ANSCENTER::modelpkg::IsPackagePath(modelFilePath ? modelFilePath : ""))
modelType = 3;
const bool onNvidia = (engineType == ANSCENTER::EngineType::NVIDIA_GPU);
switch (modelType) {
// ── Detection family: YOLOv8 / V11 / V12 / generic TRT / V10-RTOD ──
@@ -815,6 +823,13 @@ extern "C" ANSODENGINE_API int CreateANSODHandleEx(ANSCENTER::ANSODBase** Handl
// passing modelType=20 for classification ended up with a YOLO head.
// (3) modelType == 22 / 24 (TRT pose / TRT seg) on a non-NVIDIA box fell
// back to ONNXYOLO instead of the correct ONNXPOSE / ONNXSEG handler.
// A sealed package is an ONNX model whatever legacy type the caller names.
// YOLOV4 = 1 goes to YOLOOD, which opens only a zip of .cfg/.weights and
// cannot read an .ansmodel; a Darknet/YOLO package (family "darknet") runs
// in the YOLO engines, which recognise its confs/boxes head. Routed as
// detection so the switch below picks TensorRT or ONNX for the hardware.
if (modelType == 1 && ANSCENTER::modelpkg::IsPackagePath(modelFilePath ? modelFilePath : ""))
modelType = 3;
const bool onNvidia = (engineType == ANSCENTER::EngineType::NVIDIA_GPU);
switch (modelType) {
// ── Detection family: YOLOv8 / V11 / V12 / generic TRT / V10-RTOD ──
@@ -1576,11 +1591,21 @@ ANSODENGINE_API int OptimizeModelStr(const char* modelFilePath, const char* mode
modelType, modelDetectionType, fp16, modelFilePath ? modelFilePath : "(null)");
try {
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
// temporary Engine<float> on the stack — no shared state with running
// inference tasks. The global mutex was blocking ALL running tasks'
// result delivery for the entire duration of TRT engine building.
ANSCENTER::EngineType engineType = ANSCENTER::ANSLicenseHelper::CheckHardwareInformation();
// Same rule as CreateANSODHandleEx: a sealed Darknet/YOLO package named
// as YOLOV4 = 1 is built in the YOLO engines, not YOLOOD.
if (modelType == 1 && sealedPackage) modelType = 3;
// Route detection / pose / segmentation / OBB / classification to the best
@@ -1628,7 +1653,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 (modelDetectionType == 0) {
@@ -1778,7 +1803,10 @@ ANSODENGINE_API int OptimizeModelStr(const char* modelFilePath, const char* mode
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;
}
catch (const std::exception& e) {
+30 -1
View File
@@ -180,6 +180,25 @@ public:
[[nodiscard]] const std::vector<nvinfer1::Dims> &getOutputDims() const override { return m_outputDims; };
[[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.
/// In pool mode this returns the device of the first alive slot;
/// in single-engine mode it returns m_options.deviceIndex.
@@ -236,11 +255,16 @@ public:
// retries until build succeeds or all candidates are exhausted.
// 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);
// 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 clearGpuBuffers();
int getBindingIndexByName(const std::string& name);
void UpdateOptions(const ANSCENTER::Options& options) { m_options = options; }
void warmUp(int iterations = 5);
// Returns false if any warmup inference failed — callers must treat the
// engine as unusable (a slot that fails warmup must NOT join the pool).
bool warmUp(int iterations = 5);
void setVerbose(bool v) { m_verbose = v; }
void setDisableGraphs(bool v) { m_disableGraphs = v; }
@@ -391,6 +415,9 @@ private:
bool m_usingCachedEngine = false;
bool m_skipEngineCache = false; // true = bypass TRTEngineCache (for OptimizeModelStr)
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_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
@@ -563,6 +590,8 @@ template <typename T> Engine<T>::Engine() {
m_lastSetInputDims.clear();
}
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
// wrapped in try/catch so that a failure in one step does not skip the
+333 -123
View File
@@ -33,7 +33,7 @@ namespace ANSCENTER
{1005, "ANSOCR"},
{1006, "ANSALPR"},
{1007, "ANSCV"},
{1008, "ANSSRT"}
{1008, "ANSTS"}
};
ansawss3LicenceValid = false;
for (const auto& [productId, productName] : licenseChecks) {
@@ -168,6 +168,78 @@ namespace ANSCENTER
return out.str();
}
// ── Per-thread call status ───────────────────────────────────────────────
// Written by every upload entry point, read by the C exports in dllmain so
// the caller learns WHY a call failed (and therefore whether to retry)
// without any change to the exported signatures. Thread-local because a
// single ANSAWSS3 handle is routinely shared by many worker threads — a
// per-instance field would report another worker's outcome.
static thread_local int t_callStatus = ANSAWSS3::kCallPermanentError;
void ANSAWSS3::SetCallStatus(int status) { t_callStatus = status; }
int ANSAWSS3::LastCallStatus() { return t_callStatus; }
// ────────────────────────────────────────────────────────────────────────
// SummarizeChilkatRequestError: one line out of a request-level Chilkat log.
//
// The raw log is ~2 KB of call-stack scaffolding per failure. Written once
// per retry, per worker, it buries the one thing an operator needs: what S3
// actually said. S3 returns its reason as XML in the response body, which
// Chilkat echoes into the log:
// <Error><Code>NoSuchBucket</Code><Message>The specified bucket does
// not exist</Message>...
// Pull that out, prefix the status code, and drop the rest.
// ────────────────────────────────────────────────────────────────────────
static std::string SummarizeChilkatRequestError(const char* raw, int statusCode) {
std::ostringstream out;
if (statusCode > 0) out << "HTTP " << statusCode;
auto tagValue = [](const std::string& s, const char* tag) -> std::string {
const std::string open = std::string("<") + tag + ">";
const std::string close = std::string("</") + tag + ">";
const size_t a = s.find(open);
if (a == std::string::npos) return {};
const size_t b = s.find(close, a + open.size());
if (b == std::string::npos) return {};
return s.substr(a + open.size(), b - a - open.size());
};
if (raw && *raw) {
const std::string s(raw);
const std::string code = tagValue(s, "Code");
const std::string msg = tagValue(s, "Message");
if (!code.empty()) {
if (statusCode > 0) out << " ";
out << code;
if (!msg.empty()) out << ": " << msg;
}
else {
// No S3 error document — fall back to the deepest concrete line
// (same idea as SummarizeChilkatError, without the connect
// specific fields).
std::istringstream iss(s);
std::string ln, leaf;
while (std::getline(iss, ln)) {
const size_t a = ln.find_first_not_of(" \t\r\n");
if (a == std::string::npos) continue;
const size_t b = ln.find_last_not_of(" \t\r\n");
ln = ln.substr(a, b - a + 1);
if (ln.rfind("--", 0) == 0) continue; // close marker
if (!ln.empty() && ln.back() == ':') continue; // stack label
if (ln == "Failed.") continue;
leaf = ln;
}
if (!leaf.empty()) {
if (statusCode > 0) out << " — ";
out << leaf;
}
}
}
std::string result = out.str();
return result.empty() ? "(no detail in Chilkat log)" : result;
}
// Private helper function to extract file name from a path
// Helper function to extract filename from path
std::string ANSAWSS3::ExtractFileName(const std::string& filePath) {
@@ -302,12 +374,23 @@ namespace ANSCENTER
return true;
}
// Background connect loop, started by Connect() when the endpoint could not
// be reached at all (return code 2).
//
// This loop NEVER gives up on its own. The caller — a LabVIEW VI, an
// unattended AIBOX — calls Connect() exactly once at startup, so if this
// thread returned while still unconnected the handle would stay dead for
// the life of the process and every upload would fail instantly. Backoff
// grows 3 s -> 30 s so a long outage costs little, and the loop only exits
// once connected or when StopRetry() is called.
void ANSAWSS3::RetryLoop() {
int attempt = 0;
int backoffSec = 3;
constexpr int kMaxBackoffSec = 30;
while (!_stopRetry) {
// Wait 3 seconds (in 1-second increments so we can respond to stop quickly)
for (int i = 0; i < 3 && !_stopRetry; ++i) {
// Sleep in 1-second increments so we can respond to stop quickly.
for (int i = 0; i < backoffSec && !_stopRetry; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
if (_stopRetry) break;
@@ -327,8 +410,12 @@ namespace ANSCENTER
}
if (!resolved) {
_logger.LogDebug("ANSAWSS3::RetryLoop",
"Retry #" + std::to_string(attempt) + " - no internet, retrying in 3s",
"Retry #" + std::to_string(attempt) + " - no internet, retrying in " +
std::to_string(backoffSec) + "s",
__FILE__, __LINE__);
ANS_DBG("AWSS3", "RetryLoop #%d: DNS still failing, next try in %ds",
attempt, backoffSec);
backoffSec = (std::min)(kMaxBackoffSec, backoffSec * 2);
continue;
}
}
@@ -341,19 +428,24 @@ namespace ANSCENTER
_logger.LogDebug("ANSAWSS3::RetryLoop",
"Connected successfully after " + std::to_string(attempt) + " retries",
__FILE__, __LINE__);
_retryInProgress = false;
return;
}
else {
// Internet is available but connection failed (bad auth/URL)
// Stop retrying — no point, the parameters are wrong
_logger.LogError("ANSAWSS3::RetryLoop",
"Internet available but connection failed (check authentication or URL). Stopping retry.",
__FILE__, __LINE__);
ANS_DBG("AWSS3", "RetryLoop: connected after %d attempt(s)", attempt);
_retryInProgress = false;
return;
}
}
// DNS resolves but the connection failed — bad credentials, wrong
// URL, or the endpoint is refusing us. Keep trying anyway: the
// caller has no other way back, and this state is often temporary
// (endpoint rebooting, TLS interception clearing, clock skew being
// corrected). Backoff keeps the cost negligible.
_logger.LogError("ANSAWSS3::RetryLoop",
"Retry #" + std::to_string(attempt) +
" - endpoint resolves but connection failed (check authentication or URL). "
"Retrying in " + std::to_string(backoffSec) + "s",
__FILE__, __LINE__);
ANS_DBG("AWSS3", "RetryLoop #%d: connect failed, next try in %ds", attempt, backoffSec);
backoffSec = (std::min)(kMaxBackoffSec, backoffSec * 2);
}
_retryInProgress = false;
}
@@ -366,9 +458,16 @@ namespace ANSCENTER
std::unique_ptr<S3Connection> ANSAWSS3::CreateConnection(bool logOnFailure) {
auto conn = std::make_unique<S3Connection>();
// Stamp the config generation this connection is built against so a
// later Connect / SetAuthentication / SetServerProxy can invalidate it
// even if it lands in the pool after that call cleared it.
conn->configGen = _configGen.load();
// Apply request timeouts BEFORE Connect() so ConnectTimeoutMs takes
// effect on this very handshake. IdleTimeoutMs is sticky and applies
// to every subsequent request on this CkRest.
// to every subsequent request on this CkRest. A brand-new connection
// gets the FULL idle timeout — it is known-good, so it should be given
// every chance to complete; only reuse gets the short leash.
conn->rest.put_ConnectTimeoutMs(_connectTimeoutMs.load());
conn->rest.put_IdleTimeoutMs(_idleTimeoutMs.load());
@@ -423,26 +522,82 @@ namespace ANSCENTER
return conn;
}
// Take a connection from the pool, skipping any that are stale (idle too
// long) or built against superseded config. Falls through to a fresh
// connection when nothing reusable is left.
std::unique_ptr<S3Connection> ANSAWSS3::AcquireConnection() {
{
std::lock_guard<std::mutex> lk(_poolMutex);
if (!_pool.empty()) {
auto conn = std::move(_pool.back());
const uint64_t gen = _configGen.load();
const auto now = std::chrono::steady_clock::now();
for (;;) {
std::unique_ptr<S3Connection> conn;
{
std::lock_guard<std::mutex> lk(_poolMutex);
if (_pool.empty()) break;
conn = std::move(_pool.back());
_pool.pop_back();
return conn;
}
if (conn->configGen != gen) {
ANS_DBG("AWSS3", "Acquire: dropping pooled conn from older config gen %llu (now %llu)",
(unsigned long long)conn->configGen, (unsigned long long)gen);
DiscardConnection(std::move(conn)); // pool lock released first
continue;
}
const long long idleMs = std::chrono::duration_cast<std::chrono::milliseconds>(
now - conn->lastUsed).count();
if (idleMs > kMaxPooledIdleMs) {
ANS_DBG("AWSS3", "Acquire: dropping pooled conn idle for %lldms (> %dms)",
idleMs, kMaxPooledIdleMs);
DiscardConnection(std::move(conn));
continue;
}
// Clear per-request state left behind by whoever used this
// connection last. Multipart uploads set uploadId / partNumber
// query params; a plain PUT that inherited them would target the
// wrong resource. Content-Encoding is set by UploadTextData only,
// and a JPEG that inherited "gzip" would be stored with metadata
// saying it is compressed — browsers would then fail to display it.
// NOTE: do NOT call ClearAllHeaders here — it wipes Chilkat's SigV4
// state (see commit fcc985e). Headers that every upload sets
// explicitly (Content-Type, Expect) are overwritten by AddHeader
// and need no clearing.
conn->rest.ClearAllQueryParams();
conn->rest.RemoveHeader("Content-Encoding");
// Short leash on reuse: if this socket died while idle, fail fast
// and let the retry run on a fresh connection.
conn->rest.put_IdleTimeoutMs(_reusedIdleTimeoutMs.load());
return conn;
}
// Pool empty — create a new connection (no lock held during network I/O)
// Pool empty (or everything in it was stale) — create a new connection.
std::lock_guard<std::mutex> cfgLk(_configMutex);
return CreateConnection();
}
void ANSAWSS3::ReleaseConnection(std::unique_ptr<S3Connection> conn) {
if (!conn) return;
// Mark as fresh and restore the full idle timeout; AcquireConnection
// re-applies the short one when it hands the connection out again.
conn->lastUsed = std::chrono::steady_clock::now();
conn->rest.put_IdleTimeoutMs(_idleTimeoutMs.load());
std::lock_guard<std::mutex> lk(_poolMutex);
_pool.push_back(std::move(conn));
}
// Close a suspect connection instead of pooling it. Disconnect is given a
// small budget only — the point is to release the socket, not to wait on a
// peer that has already stopped answering.
void ANSAWSS3::DiscardConnection(std::unique_ptr<S3Connection> conn) {
if (!conn) return;
conn->rest.Disconnect(100);
ANS_DBG("AWSS3", "Discarded connection (not returned to pool)");
// unique_ptr destructor tears the CkRest down here.
}
// Pre-populate the pool with `count` ready-to-use connections. Each
// connection performs a TLS handshake; doing `count` of them in parallel
// (rather than serially as AcquireConnection would on a cold pool) cuts
@@ -509,6 +664,16 @@ namespace ANSCENTER
_connectTimeoutMs.store(connectMs);
_idleTimeoutMs.store(idleMs);
// Reuse timeout scales with the configured idle timeout (a quarter of
// it, clamped to a sane floor) so a deployment on a slow link that
// raises idleMs does not end up with an over-aggressive leash on
// pooled sockets.
// Never longer than the configured idle timeout itself (idleMs can be
// as low as 1000), and never so short that a healthy but busy server
// gets cut off.
const int reusedMs = (std::min)(idleMs, (std::max)(2000, idleMs / 4));
_reusedIdleTimeoutMs.store(reusedMs);
// Propagate to existing pooled connections so already-warm workers
// start using the new values on their next request.
{
@@ -522,16 +687,68 @@ namespace ANSCENTER
}
_logger.LogDebug("ANSAWSS3::SetTimeouts",
"connect=" + std::to_string(connectMs) + "ms, idle=" + std::to_string(idleMs) + "ms",
"connect=" + std::to_string(connectMs) + "ms, idle=" + std::to_string(idleMs) +
"ms, idle-on-reuse=" + std::to_string(reusedMs) + "ms",
__FILE__, __LINE__);
return true;
}
// Shared precondition check for every upload entry point. The distinction
// matters to the caller: a licence problem needs an operator, whereas "not
// connected" clears itself once the background RetryLoop gets through — so
// the two report different status codes.
bool ANSAWSS3::CheckUploadPreconditions(const std::string& opName) {
if (!_isLicenseValid || !_isUnlockCodeValid) {
_logger.LogError(opName,
!_isLicenseValid ? "Invalid license" : "Invalid unlock code",
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError);
return false;
}
if (!_bConnected) {
const bool retrying = _retryInProgress.load();
_logger.LogError(opName,
retrying ? "Not connected (waiting for internet, retrying in background)"
: "Not connected (no internet or connection not established)",
__FILE__, __LINE__);
ANS_DBG("AWSS3", "%s: not connected (background retry %s) — caller should retry later",
opName.c_str(), retrying ? "active" : "inactive");
SetCallStatus(kCallNotConnected);
return false;
}
return true;
}
// See the declaration comment in ANSUtilities.h for why a failed Chilkat
// request is not automatically a network problem.
ANSAWSS3::AttemptResult ANSAWSS3::ClassifyFailedRequest(
std::unique_ptr<S3Connection> conn, std::string& lastError) {
const int statusCode = conn ? conn->rest.get_ResponseStatusCode() : 0;
lastError = SummarizeChilkatRequestError(
conn ? conn->rest.lastErrorText() : nullptr, statusCode);
// The server answered, and its answer will not change on a retry:
// wrong bucket, no permission, bad key. 408 and 429 are the exceptions
// — those explicitly ask the client to try again.
if (statusCode >= 400 && statusCode <= 499 &&
statusCode != 408 && statusCode != 429) {
ANS_DBG("AWSS3", "Request failed permanently: %s", lastError.c_str());
ReleaseConnection(std::move(conn)); // the socket itself is fine
return AttemptResult::Permanent;
}
ANS_DBG("AWSS3", "Request failed transiently: %s", lastError.c_str());
DiscardConnection(std::move(conn)); // suspect socket — do not pool
return AttemptResult::Transient;
}
// Drives a retry loop for a single upload operation. See the declaration
// comment in ANSUtilities.h for the AttemptResult contract.
//
// Retry policy (shared across all upload entry points):
// - kUploadMaxAttempts attempts total
// - kUploadMaxAttempts attempts total, AND kUploadDeadlineMs wall-clock
// (whichever comes first — the attempt count alone does not bound how
// long the caller blocks, since one attempt can sit on a timeout)
// - kUploadRetryDelayMs between attempts (no delay after the last one)
// - std::exception thrown inside attemptFn is treated as Transient
// - Any other exception is treated as Permanent (bail immediately)
@@ -540,6 +757,11 @@ namespace ANSCENTER
const std::function<AttemptResult(std::string& lastError)>& attemptFn) {
std::string lastError;
const auto started = std::chrono::steady_clock::now();
auto elapsedMs = [&started]() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - started).count();
};
for (int attempt = 1; attempt <= kUploadMaxAttempts; ++attempt) {
AttemptResult result = AttemptResult::Transient;
@@ -556,6 +778,7 @@ namespace ANSCENTER
}
if (result == AttemptResult::Success) {
SetCallStatus(kCallSuccess);
return true;
}
@@ -563,14 +786,29 @@ namespace ANSCENTER
_logger.LogError(opName,
"Permanent failure (no retry): " + lastError,
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError);
return false;
}
// Transient — retry if both budgets allow it.
if (elapsedMs() >= kUploadDeadlineMs) {
_logger.LogError(opName,
"Upload gave up after " + std::to_string(attempt) + " attempt(s) / " +
std::to_string(elapsedMs()) + "ms (deadline " +
std::to_string(kUploadDeadlineMs) + "ms). Last error: " + lastError,
__FILE__, __LINE__);
ANS_DBG("AWSS3", "%s: deadline exceeded after %d attempt(s), %lldms",
opName.c_str(), attempt, elapsedMs());
// Ran out of time on transient errors — the caller should retry.
SetCallStatus(kCallTransientError);
return false;
}
// Transient — retry if attempts remain.
if (attempt < kUploadMaxAttempts) {
_logger.LogDebug(opName,
"Attempt " + std::to_string(attempt) + "/" +
std::to_string(kUploadMaxAttempts) + " failed: " + lastError +
std::to_string(kUploadMaxAttempts) + " failed after " +
std::to_string(elapsedMs()) + "ms: " + lastError +
" — retrying in " + std::to_string(kUploadRetryDelayMs) + "ms",
__FILE__, __LINE__);
std::this_thread::sleep_for(
@@ -580,8 +818,10 @@ namespace ANSCENTER
_logger.LogError(opName,
"Upload failed after " + std::to_string(kUploadMaxAttempts) +
" attempts. Last error: " + lastError,
" attempts / " + std::to_string(elapsedMs()) + "ms. Last error: " + lastError,
__FILE__, __LINE__);
// Every attempt hit a transient error — worth retrying from the caller.
SetCallStatus(kCallTransientError);
return false;
}
@@ -652,7 +892,10 @@ namespace ANSCENTER
// Simplified proxy validation logic
_bProxy = !proxyHost.empty() && proxyPort > 0;
// Clear pool so new connections pick up proxy config
// Clear pool so new connections pick up proxy config. Bumping the
// generation also invalidates any connection still in flight
// elsewhere, which would otherwise be pooled after this clear.
_configGen.fetch_add(1);
{
std::lock_guard<std::mutex> lk(_poolMutex);
_pool.clear();
@@ -701,7 +944,9 @@ namespace ANSCENTER
_bTls = bTls;
_bAutoReconnect = autoReconnect;
// Clear pool so new connections use updated config
// Clear pool so new connections use updated config (see the
// generation note in SetServerProxy).
_configGen.fetch_add(1);
{
std::lock_guard<std::mutex> lk(_poolMutex);
_pool.clear();
@@ -770,7 +1015,13 @@ namespace ANSCENTER
_secretKey = secretKey;
_authReady = true;
// Clear pool so new connections pick up new credentials.
// Clear pool so new connections pick up new credentials. The
// generation bump matters most here: the background RetryLoop
// can complete a connect at any moment, and a connection it
// built before _authReady was set carries no SigV4 state — if
// it landed in the pool after this clear, every upload using it
// would come back 403 (a permanent failure, so not retried).
_configGen.fetch_add(1);
std::lock_guard<std::mutex> lk(_poolMutex);
_pool.clear();
}
@@ -809,7 +1060,7 @@ namespace ANSCENTER
if (!conn->rest.FullRequestNoBodySb("GET", "/", sbResponse)) {
_logger.LogError("ANSAWSS3::ListBuckets - Request failed",
conn->rest.lastErrorText(), __FILE__, __LINE__);
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return {};
}
@@ -909,7 +1160,7 @@ namespace ANSCENTER
if (!success) {
_logger.LogError("ANSAWSS3::ListBucketObjects - Request failed on page " + std::to_string(pageCount),
conn->rest.lastErrorText(), __FILE__, __LINE__);
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return objectList; // Return what we have so far
}
@@ -1071,7 +1322,7 @@ namespace ANSCENTER
if (!success) {
_logger.LogError("ANSAWSS3::ListBucketObjectsWithPrefix - Request failed on page " + std::to_string(pageCount),
conn->rest.lastErrorText(), __FILE__, __LINE__);
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return objectList; // Return what we have so far
}
@@ -1227,7 +1478,7 @@ namespace ANSCENTER
if (conn->rest.get_LastMethodSuccess() != true) {
_logger.LogError("ANSAWSS3::CreateBucket - Request failed",
conn->rest.lastErrorText(), __FILE__, __LINE__);
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return false;
}
@@ -1314,7 +1565,7 @@ namespace ANSCENTER
_logger.LogError("ANSAWSS3::DeleteBucket",
"Failed to set AWS authentication: " + std::string(conn->rest.lastErrorText()),
__FILE__, __LINE__);
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return false;
}
@@ -1440,7 +1691,7 @@ namespace ANSCENTER
if (conn->rest.get_LastMethodSuccess() != true) {
_logger.LogError("ANSAWSS3::CreateFolder - Request failed",
conn->rest.lastErrorText(), __FILE__, __LINE__);
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return false;
}
@@ -1608,7 +1859,7 @@ namespace ANSCENTER
if (conn->rest.get_LastMethodSuccess() != true) {
_logger.LogError("ANSAWSS3::GetBucketRegion - Request failed",
conn->rest.lastErrorText(), __FILE__, __LINE__);
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return "";
}
@@ -1679,15 +1930,11 @@ namespace ANSCENTER
// _logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__);
// Early validation (permanent — do NOT retry)
if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) {
_logger.LogError(kOp,
!_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)",
__FILE__, __LINE__);
return false;
}
if (!CheckUploadPreconditions(kOp)) return false;
if (bucketName.empty() || textFilePath.empty()) {
_logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -1697,6 +1944,7 @@ namespace ANSCENTER
_logger.LogError(kOp,
"Failed to extract filename from path: " + textFilePath,
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help
return false;
}
const std::string contentType = GetContentType(textFilePath);
@@ -1740,9 +1988,7 @@ namespace ANSCENTER
conn->rest.fullRequestString("PUT", objectPath.c_str(), fileContents);
if (!conn->rest.get_LastMethodSuccess()) {
lastError = conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
return ClassifyFailedRequest(std::move(conn), lastError);
}
int statusCode = conn->rest.get_ResponseStatusCode();
@@ -1772,15 +2018,11 @@ namespace ANSCENTER
// _logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__);
// Early validation (permanent — do NOT retry)
if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) {
_logger.LogError(kOp,
!_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)",
__FILE__, __LINE__);
return false;
}
if (!CheckUploadPreconditions(kOp)) return false;
if (bucketName.empty() || dataFilePath.empty()) {
_logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -1789,6 +2031,7 @@ namespace ANSCENTER
_logger.LogError(kOp,
"Failed to extract filename from path: " + dataFilePath,
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help
return false;
}
const std::string contentType = GetContentType(dataFilePath);
@@ -1821,9 +2064,7 @@ namespace ANSCENTER
CkStringBuilder sbResponse;
if (!conn->rest.FullRequestBd("PUT", objectPath.c_str(), binData, sbResponse)) {
lastError = conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
return ClassifyFailedRequest(std::move(conn), lastError);
}
int statusCode = conn->rest.get_ResponseStatusCode();
@@ -1852,15 +2093,11 @@ namespace ANSCENTER
//_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__);
// Early validation (permanent — do NOT retry)
if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) {
_logger.LogError(kOp,
!_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)",
__FILE__, __LINE__);
return false;
}
if (!CheckUploadPreconditions(kOp)) return false;
if (bucketName.empty() || dataFilePath.empty()) {
_logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -1872,6 +2109,7 @@ namespace ANSCENTER
_logger.LogError(kOp,
"Failed to extract filename from path: " + dataFilePath,
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help
return false;
}
}
@@ -1923,9 +2161,7 @@ namespace ANSCENTER
CkStringBuilder sbResponse;
if (!conn->rest.FullRequestBd("PUT", objectPath.c_str(), binData, sbResponse)) {
lastError = conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
return ClassifyFailedRequest(std::move(conn), lastError);
}
int statusCode = conn->rest.get_ResponseStatusCode();
@@ -1956,15 +2192,11 @@ namespace ANSCENTER
//_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__);
// Early validation (permanent — do NOT retry)
if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) {
_logger.LogError(kOp,
!_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)",
__FILE__, __LINE__);
return false;
}
if (!CheckUploadPreconditions(kOp)) return false;
if (bucketName.empty() || dataFilePath.empty()) {
_logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -1994,9 +2226,7 @@ namespace ANSCENTER
conn->rest.fullRequestStream("PUT", objectPath.c_str(), fileStream);
if (conn->rest.get_LastMethodSuccess() != true) {
lastError = conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
return ClassifyFailedRequest(std::move(conn), lastError);
}
int statusCode = conn->rest.get_ResponseStatusCode();
@@ -2026,15 +2256,11 @@ namespace ANSCENTER
//_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__);
// Early validation (permanent — do NOT retry)
if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) {
_logger.LogError(kOp,
!_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)",
__FILE__, __LINE__);
return false;
}
if (!CheckUploadPreconditions(kOp)) return false;
if (bucketName.empty() || dataFilePath.empty()) {
_logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -2042,6 +2268,7 @@ namespace ANSCENTER
_logger.LogError(kOp,
"Part size must be at least 5MB (5242880 bytes)",
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -2049,6 +2276,7 @@ namespace ANSCENTER
CkFileAccess fac;
if (!fac.FileExists(dataFilePath.c_str())) {
_logger.LogError(kOp, "File not found: " + dataFilePath, __FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help
return false;
}
}
@@ -2058,6 +2286,7 @@ namespace ANSCENTER
_logger.LogError(kOp,
"Failed to extract filename from path: " + dataFilePath,
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help
return false;
}
@@ -2106,9 +2335,8 @@ namespace ANSCENTER
const char* responseXml = conn->rest.fullRequestNoBody("POST", objectPath.c_str());
if (!conn->rest.get_LastMethodSuccess()) {
lastError = std::string("Initiate failed: ") + conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
{ AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError);
lastError = "Initiate: " + lastError; return r; }
}
int initStatus = conn->rest.get_ResponseStatusCode();
if (initStatus != 200) {
@@ -2160,9 +2388,8 @@ namespace ANSCENTER
const char* responseStr = conn->rest.fullRequestStream("PUT", objectPath.c_str(), fileStream);
if (!conn->rest.get_LastMethodSuccess()) {
lastError = "Part " + std::to_string(partNumber) + " failed: " + conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
{ AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError);
lastError = "Part " + std::to_string(partNumber) + ": " + lastError; return r; }
}
int partStatus = conn->rest.get_ResponseStatusCode();
if (partStatus != 200) {
@@ -2175,7 +2402,7 @@ namespace ANSCENTER
const char* etag = conn->rest.responseHdrByName("ETag");
if (!conn->rest.get_LastMethodSuccess() || etag == nullptr || etag[0] == '\0') {
lastError = "Part " + std::to_string(partNumber) + ": ETag missing from response";
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return AttemptResult::Transient;
}
@@ -2194,9 +2421,8 @@ namespace ANSCENTER
responseXml = conn->rest.fullRequestString("POST", objectPath.c_str(), partsListXml.getXml());
if (!conn->rest.get_LastMethodSuccess()) {
lastError = std::string("Complete failed: ") + conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
{ AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError);
lastError = "Complete: " + lastError; return r; }
}
int completeStatus = conn->rest.get_ResponseStatusCode();
if (completeStatus != 200) {
@@ -2227,15 +2453,11 @@ namespace ANSCENTER
//_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__);
// Early validation (permanent — do NOT retry)
if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) {
_logger.LogError(kOp,
!_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)",
__FILE__, __LINE__);
return false;
}
if (!CheckUploadPreconditions(kOp)) return false;
if (bucketName.empty() || dataFilePath.empty()) {
_logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -2243,6 +2465,7 @@ namespace ANSCENTER
_logger.LogError(kOp,
"Part size must be at least 5MB (5242880 bytes)",
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -2250,6 +2473,7 @@ namespace ANSCENTER
CkFileAccess fac;
if (!fac.FileExists(dataFilePath.c_str())) {
_logger.LogError(kOp, "File not found: " + dataFilePath, __FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help
return false;
}
}
@@ -2261,6 +2485,7 @@ namespace ANSCENTER
_logger.LogError(kOp,
"Failed to extract filename from path: " + dataFilePath,
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help
return false;
}
}
@@ -2323,9 +2548,8 @@ namespace ANSCENTER
const char* responseXml = conn->rest.fullRequestNoBody("POST", objectPath.c_str());
if (!conn->rest.get_LastMethodSuccess()) {
lastError = std::string("Initiate failed: ") + conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
{ AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError);
lastError = "Initiate: " + lastError; return r; }
}
int initStatus = conn->rest.get_ResponseStatusCode();
if (initStatus != 200) {
@@ -2377,9 +2601,8 @@ namespace ANSCENTER
const char* responseStr = conn->rest.fullRequestStream("PUT", objectPath.c_str(), fileStream);
if (!conn->rest.get_LastMethodSuccess()) {
lastError = "Part " + std::to_string(partNumber) + " failed: " + conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
{ AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError);
lastError = "Part " + std::to_string(partNumber) + ": " + lastError; return r; }
}
int partStatus = conn->rest.get_ResponseStatusCode();
if (partStatus != 200) {
@@ -2392,7 +2615,7 @@ namespace ANSCENTER
const char* etag = conn->rest.responseHdrByName("ETag");
if (!conn->rest.get_LastMethodSuccess() || etag == nullptr || etag[0] == '\0') {
lastError = "Part " + std::to_string(partNumber) + ": ETag missing from response";
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return AttemptResult::Transient;
}
@@ -2410,9 +2633,8 @@ namespace ANSCENTER
responseXml = conn->rest.fullRequestString("POST", objectPath.c_str(), partsListXml.getXml());
if (!conn->rest.get_LastMethodSuccess()) {
lastError = std::string("Complete failed: ") + conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
{ AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError);
lastError = "Complete: " + lastError; return r; }
}
int completeStatus = conn->rest.get_ResponseStatusCode();
if (completeStatus != 200) {
@@ -2444,17 +2666,13 @@ namespace ANSCENTER
const std::string kOp = "ANSAWSS3::UploadJpegImage";
// _logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__);
// Early validation checks (permanent — do NOT retry)
if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) {
_logger.LogError(kOp,
!_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)",
__FILE__, __LINE__);
return false;
}
if (!CheckUploadPreconditions(kOp)) return false;
if (bucketName.empty() || bufferLength <= 0) {
_logger.LogError(kOp,
"Bucket name or buffer length is invalid",
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -2486,9 +2704,7 @@ namespace ANSCENTER
CkStringBuilder sbResponse;
if (!conn->rest.FullRequestBd("PUT", objectPath.c_str(), binData, sbResponse)) {
lastError = conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
return ClassifyFailedRequest(std::move(conn), lastError);
}
int statusCode = conn->rest.get_ResponseStatusCode();
@@ -2516,17 +2732,13 @@ namespace ANSCENTER
const std::string kOp = "ANSAWSS3::UploadPrefixJpegImage";
//_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__);
// Early validation checks (permanent failures — do NOT retry)
if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) {
_logger.LogError(kOp,
!_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)",
__FILE__, __LINE__);
return false;
}
if (!CheckUploadPreconditions(kOp)) return false;
if (bucketName.empty() || bufferLength <= 0 || fileName.empty()) {
_logger.LogError(kOp,
"Bucket name, buffer length, or filename is invalid",
__FILE__, __LINE__);
SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help
return false;
}
@@ -2574,9 +2786,7 @@ namespace ANSCENTER
CkStringBuilder sbResponse;
if (!conn->rest.FullRequestBd("PUT", objectPath.c_str(), binData, sbResponse)) {
lastError = conn->rest.lastErrorText();
ReleaseConnection(std::move(conn));
return AttemptResult::Transient;
return ClassifyFailedRequest(std::move(conn), lastError);
}
int statusCode = conn->rest.get_ResponseStatusCode();
@@ -2764,7 +2974,7 @@ namespace ANSCENTER
if (!conn->rest.SendReqNoBody("GET", objectPath.c_str())) {
_logger.LogError("ANSAWSS3::DownloadFile - Request failed",
conn->rest.lastErrorText(), __FILE__, __LINE__);
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return false;
}
@@ -2773,7 +2983,7 @@ namespace ANSCENTER
if (responseStatusCode < 0) {
_logger.LogError("ANSAWSS3::DownloadFile - Failed to read response header",
conn->rest.lastErrorText(), __FILE__, __LINE__);
ReleaseConnection(std::move(conn));
DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket
return false;
}
+174 -9
View File
@@ -40,7 +40,7 @@ namespace ANSCENTER
{1005, "ANSOCR"},
{1006, "ANSALPR"},
{1007, "ANSCV"},
{1008, "ANSSRT"}
{1008, "ANSTS"}
};
ansutilityLicenceValid = false;
for (const auto& [productId, productName] : licenseChecks) {
@@ -1312,7 +1312,7 @@ namespace ANSCENTER
return result;
}
// ====================================================================
// GetANSLoggerErrorEvents
// GetANSLoggerErrorEvents / GetANSLoggerErrorEventsSinceLastCall
//
// Reads every Error-level event written by the "ANSLogger" source in
// the Application channel using the modern winevt API. Events are
@@ -1322,14 +1322,98 @@ namespace ANSCENTER
//
// DebugView: filter "[EVTLOG]".
// ====================================================================
std::string ANSUtilities::GetANSLoggerErrorEvents(bool clearAfterRead) {
// XPath: Provider name "ANSLogger" AND Level 2 (EVENTLOG_ERROR_TYPE).
// Forward direction keeps chronological insertion order so output
// matches a fresh "oldest first" scroll through Event Viewer.
constexpr const wchar_t* kQuery =
L"*[System[Provider[@Name='ANSLogger'] and Level=2]]";
EVT_HANDLE hResults = EvtQuery(nullptr, L"Application", kQuery,
// ---- Watermark helpers (HKCU, no admin needed) ----------------------
static constexpr const wchar_t* kWatermarkRegKey =
L"Software\\ANSCENTER\\ANSUtilities\\EventLogWatermark";
static constexpr const wchar_t* kWatermarkValueName = L"ANSLogger";
static uint64_t ReadAnsLoggerWatermark() {
HKEY hKey = nullptr;
if (RegOpenKeyExW(HKEY_CURRENT_USER, kWatermarkRegKey, 0,
KEY_READ, &hKey) != ERROR_SUCCESS) {
return 0;
}
uint64_t value = 0;
DWORD size = sizeof(value), type = 0;
LONG rc = RegQueryValueExW(hKey, kWatermarkValueName, nullptr,
&type, reinterpret_cast<LPBYTE>(&value),
&size);
RegCloseKey(hKey);
return (rc == ERROR_SUCCESS && type == REG_QWORD) ? value : 0;
}
static void WriteAnsLoggerWatermark(uint64_t value) {
HKEY hKey = nullptr;
if (RegCreateKeyExW(HKEY_CURRENT_USER, kWatermarkRegKey, 0,
nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE,
nullptr, &hKey, nullptr) != ERROR_SUCCESS) {
ANS_DBG("EVTLOG", "watermark RegCreateKey failed err=%lu",
GetLastError());
return;
}
LONG rc = RegSetValueExW(hKey, kWatermarkValueName, 0, REG_QWORD,
reinterpret_cast<const BYTE*>(&value),
sizeof(value));
if (rc != ERROR_SUCCESS) {
ANS_DBG("EVTLOG", "watermark RegSetValue failed err=%ld", rc);
}
RegCloseKey(hKey);
}
// Extracts <EventRecordID>NNN</EventRecordID> from a rendered event XML.
// Returns 0 if not present (shouldn't happen — every Windows event has one).
static uint64_t ExtractRecordIdFromXml(const std::wstring& xml) {
auto open = xml.find(L"<EventRecordID>");
if (open == std::wstring::npos) return 0;
open += 15; // length of "<EventRecordID>"
auto close = xml.find(L"</EventRecordID>", open);
if (close == std::wstring::npos) return 0;
std::wstring numStr = xml.substr(open, close - open);
return std::wcstoull(numStr.c_str(), nullptr, 10);
}
// Returns the RecordID of the most recently written event in `channel`.
// Used to detect channel clears: if our stored watermark > channelMax,
// the channel was cleared and the new event counter restarted at 1, so
// we must reset our watermark or risk skipping all new events.
static uint64_t GetChannelMaxRecordId(const wchar_t* channel) {
EVT_HANDLE hQuery = EvtQuery(nullptr, channel, nullptr,
EvtQueryChannelPath | EvtQueryReverseDirection);
if (!hQuery) return 0;
EVT_HANDLE hEvent = nullptr;
DWORD returned = 0;
uint64_t maxId = 0;
if (EvtNext(hQuery, 1, &hEvent, 1000, 0, &returned) && returned > 0) {
std::vector<wchar_t> buf(4096);
DWORD used = 0, propCount = 0;
BOOL ok = EvtRender(nullptr, hEvent, EvtRenderEventXml,
static_cast<DWORD>(buf.size() * sizeof(wchar_t)),
buf.data(), &used, &propCount);
if (!ok && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
buf.resize(used / sizeof(wchar_t) + 1);
ok = EvtRender(nullptr, hEvent, EvtRenderEventXml,
static_cast<DWORD>(buf.size() * sizeof(wchar_t)),
buf.data(), &used, &propCount);
}
if (ok) maxId = ExtractRecordIdFromXml(std::wstring(buf.data()));
EvtClose(hEvent);
}
EvtClose(hQuery);
return maxId;
}
// ---- Shared implementation -----------------------------------------
// Runs `query` against the Application channel, formats events as
// "<DD/MM/YYYY h:mm:ss tt>: <event data>\n"
// and optionally clears the channel afterwards. If trackWatermark is
// true, also persists the highest EventRecordID seen to HKCU on return.
static std::string ReadAnsLoggerEventsImpl(const wchar_t* query,
bool clearAfterRead,
bool trackWatermark)
{
EVT_HANDLE hResults = EvtQuery(nullptr, L"Application", query,
EvtQueryChannelPath | EvtQueryForwardDirection);
if (!hResults) {
ANS_DBG("EVTLOG", "EvtQuery failed err=%lu", GetLastError());
@@ -1337,6 +1421,7 @@ namespace ANSCENTER
}
std::string out;
uint64_t highestRecordId = 0;
constexpr DWORD kBatch = 64;
EVT_HANDLE events[kBatch];
DWORD returned = 0;
@@ -1367,6 +1452,15 @@ namespace ANSCENTER
}
std::wstring xml(buf.data());
// Track the highest RecordID we've seen this call so we can
// persist it as the new watermark on exit. Done before the
// continue-on-malformed-XML paths below so an unrenderable
// event still advances the watermark.
if (trackWatermark) {
uint64_t rid = ExtractRecordIdFromXml(xml);
if (rid > highestRecordId) highestRecordId = rid;
}
// ---- TimeCreated SystemTime='2026-05-18T11:52:07.123Z' ----
// EvtRender produces either single- or double-quoted attrs
// depending on Windows version, so handle both.
@@ -1462,18 +1556,89 @@ namespace ANSCENTER
}
EvtClose(hResults);
// Persist watermark BEFORE clearing — once EvtClearLog succeeds,
// future events restart at RecordID 1, and we'd never see them
// again if the watermark stayed at e.g. 547.
// Special case: if clear is also going to succeed, the new
// watermark is irrelevant (we'll reset on next call via the
// GetChannelMaxRecordId check), so writing it here is harmless.
if (trackWatermark && highestRecordId > 0) {
WriteAnsLoggerWatermark(highestRecordId);
ANS_DBG("EVTLOG", "watermark updated to %llu", highestRecordId);
}
if (clearAfterRead) {
// Clears the entire Application channel — see header warning.
// DIAGNOSTIC: also print to stderr so callers without DebugView
// can see what's going on. Common failure codes:
// 5 ERROR_ACCESS_DENIED — process is not elevated
// 1314 ERROR_PRIVILEGE_NOT_HELD — token missing SeSecurityPriv
// 87 ERROR_INVALID_PARAMETER — channel name wrong
if (!EvtClearLog(nullptr, L"Application", nullptr, 0)) {
DWORD e = GetLastError();
ANS_DBG("EVTLOG", "EvtClearLog failed err=%lu (need admin)", e);
std::fprintf(stderr,
"[ANSUtilities] EvtClearLog(Application) FAILED, "
"GetLastError=%lu (5=AccessDenied, 1314=PrivNotHeld)\n", e);
} else {
ANS_DBG("EVTLOG", "EvtClearLog: Application channel cleared");
std::fprintf(stderr,
"[ANSUtilities] EvtClearLog(Application) OK\n");
// Clear succeeded → counter resets to 1, so our watermark
// is stale. Reset it now so the next call doesn't skip
// legitimate new events whose RecordID < old watermark.
if (trackWatermark) {
WriteAnsLoggerWatermark(0);
ANS_DBG("EVTLOG", "watermark reset to 0 after clear");
}
}
}
return out;
}
// ---- Public: full read, no skip ------------------------------------
std::string ANSUtilities::GetANSLoggerErrorEvents(bool clearAfterRead) {
// XPath: Provider name "ANSLogger" AND Level 2 (EVENTLOG_ERROR_TYPE).
// Forward direction keeps chronological insertion order so output
// matches a fresh "oldest first" scroll through Event Viewer.
return ReadAnsLoggerEventsImpl(
L"*[System[Provider[@Name='ANSLogger'] and Level=2]]",
clearAfterRead,
/*trackWatermark=*/false);
}
// ---- Public: incremental read using HKCU watermark -----------------
std::string ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(bool clearAfterRead) {
uint64_t watermark = ReadAnsLoggerWatermark();
// Channel-clear detection: if the channel's current max RecordID
// is lower than our stored watermark, the log was cleared (by us
// or by something else like Event Viewer's "Clear Log") and the
// counter restarted at 1. Reset the watermark so we don't skip
// events 1..watermark that are actually fresh.
uint64_t channelMax = GetChannelMaxRecordId(L"Application");
if (channelMax > 0 && watermark > channelMax) {
ANS_DBG("EVTLOG", "watermark reset: stored=%llu channelMax=%llu "
"(channel was cleared externally)",
watermark, channelMax);
watermark = 0;
WriteAnsLoggerWatermark(0);
}
// Embed the watermark in the XPath so Windows does the filtering;
// we never see events <= watermark, which is more efficient than
// post-filtering thousands of events on the client side.
wchar_t queryBuf[256];
swprintf_s(queryBuf, _countof(queryBuf),
L"*[System[Provider[@Name='ANSLogger'] and Level=2 and "
L"(EventRecordID>%llu)]]",
watermark);
ANS_DBG("EVTLOG", "GetANSLoggerErrorEventsSinceLastCall watermark=%llu",
watermark);
return ReadAnsLoggerEventsImpl(queryBuf, clearAfterRead,
/*trackWatermark=*/true);
}
} // namespace ANSCENTER
// ============================================================================
+134 -6
View File
@@ -17,6 +17,7 @@
#include <opencv2/opencv.hpp>
#include <thread>
#include <atomic>
#include <chrono>
#include <functional>
namespace ANSCENTER {
@@ -161,14 +162,40 @@ namespace ANSCENTER {
// Time is converted to the user's local timezone and formatted
// using LOCALE_USER_DEFAULT (matches Event Viewer's display).
//
// If `clearAfterRead` is true, calls EvtClearLog on the entire
// Application channel after reading. WARNING: this wipes every
// event from every source in the Application log, not just
// ANSLogger entries — there is no per-event delete API in
// Windows. Requires Administrator privileges; without elevation
// the clear silently fails (events are still returned).
// clearAfterRead=true behaviour:
// * Calls EvtClearLog on the entire Application channel after
// reading. WARNING: this wipes every event from every source
// in the Application log, not just ANSLogger entries — there
// is no per-event delete API in Windows.
// * REQUIRES THE CALLING PROCESS TO BE ELEVATED (run as
// Administrator). Without elevation, EvtClearLog returns
// ERROR_ACCESS_DENIED (Win32 error 5) and the events remain
// in the log. The read still succeeds either way.
// * Failures are reported on stderr in the form
// "[ANSUtilities] EvtClearLog(Application) FAILED, GetLastError=N"
// and via DebugView under the "EVTLOG" tag. The C++ return
// value does NOT signal clear success — capture stderr or
// check Event Viewer afterwards to confirm.
[[nodiscard]] static std::string GetANSLoggerErrorEvents(bool clearAfterRead = false);
// Same as GetANSLoggerErrorEvents but only returns events that
// have NOT been returned by a previous call from this user
// account. The highest EventRecordID seen is persisted to
// HKCU\Software\ANSCENTER\ANSUtilities\EventLogWatermark\ANSLogger
// (REG_QWORD) and used as a Windows XPath filter
// (EventRecordID > N) on the next call. No admin needed for the
// skip mechanism — only for the optional clearAfterRead.
//
// Channel-clear handling: if Windows resets the Application
// channel's RecordID counter (typical after EvtClearLog), this
// function detects that the channel's current max RecordID is
// less than the stored watermark and resets the watermark to 0
// so no newly-written events are accidentally skipped.
//
// To force a re-read of all events, delete the registry value
// above and call again.
[[nodiscard]] static std::string GetANSLoggerErrorEventsSinceLastCall(bool clearAfterRead = false);
// Scan the local network for AIBOX / ANSVIS servers.
// Runs two strategies in parallel and merges results by IP:
// 1) UDP multicast probe to 230.50.200.150:58839 (with subnet
@@ -188,6 +215,19 @@ namespace ANSCENTER {
CkRest rest;
CkSocket socket;
CkAuthAws authAws;
// When this connection last completed a request successfully. A socket
// that a NAT/firewall silently killed while idle costs a full
// IdleTimeoutMs to discover, so anything idle longer than
// kMaxPooledIdleMs is dropped rather than handed out again.
std::chrono::steady_clock::time_point lastUsed{ std::chrono::steady_clock::now() };
// Value of ANSAWSS3::_configGen when this connection was built.
// A mismatch means it predates a Connect / SetAuthentication /
// SetServerProxy call — e.g. the background RetryLoop finished a
// connect just after SetAuthentication cleared the pool — so its
// endpoint, proxy or SigV4 state is stale and it must not be reused.
uint64_t configGen{ 0 };
};
class ANSULT_API ANSAWSS3 {
@@ -226,6 +266,24 @@ namespace ANSCENTER {
std::atomic<int> _connectTimeoutMs{ 10000 }; // 10 s
std::atomic<int> _idleTimeoutMs { 20000 }; // 20 s
// Shorter idle timeout used for the FIRST request on a connection
// taken from the pool. A pooled socket may have been silently
// dropped by a middlebox since it was last used; without this the
// caller pays the full _idleTimeoutMs (20 s by default) just to
// discover that. A fresh connection still gets the full
// _idleTimeoutMs, so the retry after a stale-socket failure is not
// penalised. Derived from _idleTimeoutMs by SetTimeouts().
std::atomic<int> _reusedIdleTimeoutMs{ 5000 }; // 5 s
// Bumped by Connect / SetAuthentication / SetServerProxy. Stamped
// into every S3Connection at creation; see S3Connection::configGen.
std::atomic<uint64_t> _configGen{ 1 };
// A pooled connection idle longer than this is closed instead of
// reused. Kept below the ~20 s after which S3 (and most NAT
// devices) stop caring about an idle keep-alive flow.
static constexpr int kMaxPooledIdleMs = 15000;
// Proxy settings
std::string _proxyHost;
int _proxyPort{ 0 };
@@ -239,7 +297,14 @@ namespace ANSCENTER {
// Used by TryConnect's first attempt, which is allowed to fail for path-style/MinIO servers.
std::unique_ptr<S3Connection> CreateConnection(bool logOnFailure = true);
std::unique_ptr<S3Connection> AcquireConnection();
// Return a healthy connection to the pool for reuse.
void ReleaseConnection(std::unique_ptr<S3Connection> conn);
// Close and destroy a connection instead of pooling it. Use on any
// TRANSPORT failure (send/recv failed, timeout): that socket is
// suspect, and putting it back means the next worker inherits the
// same stall. An HTTP error status is NOT a transport failure — the
// server answered — so those still go through ReleaseConnection.
void DiscardConnection(std::unique_ptr<S3Connection> conn);
std::string ExtractFileName(const std::string& filePath);
std::string GetContentType(const std::string& filePath);
@@ -253,8 +318,34 @@ namespace ANSCENTER {
// The lambda should write a short human-readable reason into
// `lastError` on any non-Success outcome so it can be logged.
enum class AttemptResult { Success, Transient, Permanent };
// Record the outcome of the current call for the calling thread.
static void SetCallStatus(int status);
// Shared licence / unlock / connected check for the upload entry
// points. Logs the reason and sets the per-thread call status
// (permanent for a licence problem, not-connected for a link that
// has not come up yet). Returns false if the call must not proceed.
bool CheckUploadPreconditions(const std::string& opName);
// Decide what a failed Chilkat request means, and dispose of the
// connection accordingly. Chilkat reports failure for an HTTP 4xx
// delivered during an Expect: 100-continue exchange, so "the call
// returned false" does NOT imply the network failed — a 404
// NoSuchBucket arrives this way. Retrying those wastes the whole
// retry budget and reports the wrong class to the caller.
// 4xx (except 408/429) -> Permanent, socket healthy -> pooled
// anything else -> Transient, socket suspect -> discarded
// Takes ownership of `conn`, and rewrites `lastError` into one
// readable line.
AttemptResult ClassifyFailedRequest(std::unique_ptr<S3Connection> conn,
std::string& lastError);
static constexpr int kUploadMaxAttempts = 20;
static constexpr int kUploadRetryDelayMs = 100;
// Wall-clock budget for one upload call, across all attempts. The
// attempt cap alone is not a bound on time: 20 attempts that each
// hit a 20 s idle timeout would block the caller for ~400 s. A new
// attempt is only started while inside this budget, so the worst
// case is the budget plus one in-flight attempt.
static constexpr int kUploadDeadlineMs = 30000;
bool UploadWithRetry(
const std::string& opName,
const std::function<AttemptResult(std::string& lastError)>& attemptFn);
@@ -268,6 +359,37 @@ namespace ANSCENTER {
bool TryConnect(bool& awsPath);
public:
// ---- Call status codes -------------------------------------------
// Every upload entry point records one of these for the CALLING
// THREAD, so the C exports can tell the caller whether a retry is
// worth attempting without changing any function signature.
//
// These EXTEND the codes the C wrappers already return; they do not
// redefine any of them. Full set seen by a LabVIEW caller:
// 1 success
// 0 null / invalid argument passed to the export
// -1 handle not in the registry
// -2 upload failed, reason not classified (fallback)
// -3 std::exception escaped
// -4 unknown exception escaped
// -20 permanent: bad argument, licence, bucket, or HTTP 4xx
// -21 not connected yet / reconnect in progress
// -22 transient: network, timeout or 5xx after internal retries
//
// Retry rule for the caller: retry on -21 and -22 (and -2/-3/-4 if
// it wants to be generous); do NOT retry on -20 or 0 — those need a
// configuration or code fix. Success stays the only positive value,
// so both `rc == 1` and `rc != 1` remain correct tests.
static constexpr int kCallSuccess = 1;
static constexpr int kCallPermanentError = -20;
static constexpr int kCallNotConnected = -21;
static constexpr int kCallTransientError = -22;
// Status of the most recent upload call made ON THIS THREAD.
// Thread-local, so concurrent workers sharing one handle never see
// each other's result. Read it immediately after the call.
static int LastCallStatus();
ANSAWSS3();
~ANSAWSS3();
[[nodiscard]] bool Initialize(const std::string& licenseKey);
@@ -398,6 +520,12 @@ extern "C" ANSULT_API int ScanANSDevicesCpp(int timeoutMs, std::string& resultJ
extern "C" ANSULT_API int GetANSLoggerErrorEvents(int clearAfterRead, LStrHandle result);
extern "C" ANSULT_API int GetANSLoggerErrorEventsCpp(int clearAfterRead, std::string& result);
// Watermarked variant: only returns events not seen by previous calls.
// Watermark is stored in HKCU\Software\ANSCENTER\ANSUtilities\EventLogWatermark\ANSLogger
// (REG_QWORD). No admin needed for the skip; only for clearAfterRead.
extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCall(int clearAfterRead, LStrHandle result);
extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCallCpp(int clearAfterRead, std::string& result);
// Unicode conversion utilities for LabVIEW
extern "C" ANSULT_API int ANSConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM = 1);
extern "C" ANSULT_API int ANSDecodeJsonUnicodeToUTF16LE(const char* escapedStr, LStrHandle result);
+138 -84
View File
@@ -209,6 +209,20 @@ public:
AWSHandleGuard& operator=(const AWSHandleGuard&) = delete;
};
// Failure code for an upload whose C++ call returned false. ANSAWSS3 records
// WHY on the calling thread, which is what lets the caller decide whether a
// retry is worthwhile:
// -20 permanent — bad argument / licence / bucket / HTTP 4xx: do not retry
// -21 not connected — link not up yet, background retry running: retry later
// -22 transient — network/timeout/5xx after internal retries: retry
// Falls back to the historical -2 if no status was recorded. Never returns a
// success code, so `rc == 1` remains an exact success test.
static int AwsUploadFailureCode() {
const int status = ANSCENTER::ANSAWSS3::LastCallStatus();
if (status == ANSCENTER::ANSAWSS3::kCallSuccess) return -2;
return status;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
@@ -1910,7 +1924,7 @@ extern "C" ANSULT_API int UploadTextDataANSAWSHandle(ANSCENTER::ANSAWSS3** Hand
return 0;
}
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadTextData(bucketName, textDataPath, outPath)) {
@@ -1922,13 +1936,13 @@ extern "C" ANSULT_API int UploadTextDataANSAWSHandle(ANSCENTER::ANSAWSS3** Hand
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) {
return 0;
return -3;
}
catch (...) {
return 0;
return -4;
}
}
extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* binaryDataPath, LStrHandle uploadedFilePath) {
@@ -1936,7 +1950,7 @@ extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Ha
return 0;
}
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadBinaryData(bucketName, binaryDataPath, outPath)) {
@@ -1948,13 +1962,13 @@ extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Ha
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) {
return 0;
return -3;
}
catch (...) {
return 0;
return -4;
}
}
extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, LStrHandle uploadedFilePath) {
@@ -1962,7 +1976,7 @@ extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle(ANSCENTER::ANSAWSS3*
return 0;
}
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadFileStream(bucketName, fileDataPath, outPath)) {
@@ -1974,13 +1988,13 @@ extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle(ANSCENTER::ANSAWSS3*
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) {
return 0;
return -3;
}
catch (...) {
return 0;
return -4;
}
}
extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, int fileSize, LStrHandle uploadedFilePath) {
@@ -1988,7 +2002,7 @@ extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle(ANSCENTER::ANSAWSS3**
return 0;
}
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadMultipartData(bucketName, fileDataPath, outPath, fileSize)) {
@@ -2000,13 +2014,13 @@ extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle(ANSCENTER::ANSAWSS3**
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) {
return 0;
return -3;
}
catch (...) {
return 0;
return -4;
}
}
extern "C" ANSULT_API int DownloadFileStreamANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* objectName, const char* savedFilePath) {
@@ -2043,7 +2057,7 @@ extern "C" ANSULT_API int UploadJpegImageANSAWSHandle(ANSCENTER::ANSAWSS3** Han
}
return 1;
}
else return -2;// failed upload
else return AwsUploadFailureCode();
}
catch (std::exception& e) {
return -3;//excception
@@ -2088,9 +2102,10 @@ extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle(ANSCENTER::ANSAWSS3
return 1;
}
else {
ANS_DBG("ANSAWS", "Upload FAILED (-2): file=%s, UploadPrefixJpegImage returned false",
fileName ? fileName : "(null)");
return -2;
const int rc = AwsUploadFailureCode();
ANS_DBG("ANSAWS", "Upload FAILED (%d): file=%s, UploadPrefixJpegImage returned false",
rc, fileName ? fileName : "(null)");
return rc;
}
}
catch (std::exception& e) {
@@ -2159,7 +2174,7 @@ extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle(ANSCENTER::ANSA
return 0;
}
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, outPath, fileSize)) {
@@ -2171,13 +2186,13 @@ extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle(ANSCENTER::ANSA
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) {
return 0;
return -3;
}
catch (...) {
return 0;
return -4;
}
}
extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, LStrHandle uploadedFilePath) {
@@ -2185,7 +2200,7 @@ extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS
return 0;
}
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, outPath)) {
@@ -2197,79 +2212,79 @@ extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) {
return 0;
return -3;
}
catch (...) {
return 0;
return -4;
}
}
// ── C++ test APIs (std::string& instead of LStrHandle) ──
extern "C" ANSULT_API int UploadTextDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* textDataPath, std::string& uploadedFilePath) {
if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || textDataPath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
return guard.get()->UploadTextData(bucketName, textDataPath, uploadedFilePath) ? 1 : 0;
} catch (...) { return 0; }
return guard.get()->UploadTextData(bucketName, textDataPath, uploadedFilePath) ? 1 : AwsUploadFailureCode();
} catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* binaryDataPath, std::string& uploadedFilePath) {
if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || binaryDataPath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
return guard.get()->UploadBinaryData(bucketName, binaryDataPath, uploadedFilePath) ? 1 : 0;
} catch (...) { return 0; }
return guard.get()->UploadBinaryData(bucketName, binaryDataPath, uploadedFilePath) ? 1 : AwsUploadFailureCode();
} catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, std::string& uploadedFilePath) {
if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || fileDataPath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
return guard.get()->UploadFileStream(bucketName, fileDataPath, uploadedFilePath) ? 1 : 0;
} catch (...) { return 0; }
return guard.get()->UploadFileStream(bucketName, fileDataPath, uploadedFilePath) ? 1 : AwsUploadFailureCode();
} catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, int fileSize, std::string& uploadedFilePath) {
if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || fileDataPath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
return guard.get()->UploadMultipartData(bucketName, fileDataPath, uploadedFilePath, fileSize) ? 1 : 0;
} catch (...) { return 0; }
return guard.get()->UploadMultipartData(bucketName, fileDataPath, uploadedFilePath, fileSize) ? 1 : AwsUploadFailureCode();
} catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadJpegImageANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, std::string& uploadedFilePath) {
if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
return guard.get()->UploadJpegImage(bucketName, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : 0;
} catch (...) { return 0; }
return guard.get()->UploadJpegImage(bucketName, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : AwsUploadFailureCode();
} catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, std::string& uploadedFilePath) {
if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || binaryDataPath == nullptr || objectName == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
return guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, uploadedFilePath) ? 1 : 0;
} catch (...) { return 0; }
return guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, uploadedFilePath) ? 1 : AwsUploadFailureCode();
} catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* fileDataPath, const char* objectName, int fileSize, std::string& uploadedFilePath) {
if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || fileDataPath == nullptr || fileSize <= 0) return 0;
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
return guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, uploadedFilePath, fileSize) ? 1 : 0;
} catch (...) { return 0; }
return guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, uploadedFilePath, fileSize) ? 1 : AwsUploadFailureCode();
} catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, std::string& uploadedFilePath) {
if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(*Handle));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
return guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : 0;
} catch (...) { return 0; }
return guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : AwsUploadFailureCode();
} catch (...) { return -4; }
}
extern "C" ANSULT_API int GeneratePresignedURLANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* objectName, int validDays, std::string& presignedURL) {
if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || objectName == nullptr) return 0;
@@ -2767,7 +2782,7 @@ extern "C" ANSULT_API int UploadTextDataANSAWSHandle_V2(uint64_t handleVal, cons
auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
if (bucketName == nullptr || textDataPath == nullptr || uploadedFilePath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(_v2h));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadTextData(bucketName, textDataPath, outPath)) {
@@ -2779,17 +2794,17 @@ extern "C" ANSULT_API int UploadTextDataANSAWSHandle_V2(uint64_t handleVal, cons
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) { return 0; }
catch (...) { return 0; }
catch (std::exception& e) { return -3; }
catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* binaryDataPath, LStrHandle uploadedFilePath) {
auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
if (bucketName == nullptr || binaryDataPath == nullptr || uploadedFilePath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(_v2h));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadBinaryData(bucketName, binaryDataPath, outPath)) {
@@ -2801,17 +2816,17 @@ extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle_V2(uint64_t handleVal, co
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) { return 0; }
catch (...) { return 0; }
catch (std::exception& e) { return -3; }
catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* fileDataPath, LStrHandle uploadedFilePath) {
auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
if (bucketName == nullptr || fileDataPath == nullptr || uploadedFilePath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(_v2h));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadFileStream(bucketName, fileDataPath, outPath)) {
@@ -2823,17 +2838,17 @@ extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle_V2(uint64_t handleVal
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) { return 0; }
catch (...) { return 0; }
catch (std::exception& e) { return -3; }
catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* fileDataPath, int fileSize, LStrHandle uploadedFilePath) {
auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
if (bucketName == nullptr || fileDataPath == nullptr || uploadedFilePath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(_v2h));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadMultipartData(bucketName, fileDataPath, outPath, fileSize)) {
@@ -2845,17 +2860,17 @@ extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle_V2(uint64_t handleVal,
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) { return 0; }
catch (...) { return 0; }
catch (std::exception& e) { return -3; }
catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadJpegImageANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, LStrHandle uploadedFilePath) {
auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
if (bucketName == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr || uploadedFilePath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(_v2h));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadJpegImage(bucketName, jpeg_string, bufferLength, fileName, outPath)) {
@@ -2867,17 +2882,17 @@ extern "C" ANSULT_API int UploadJpegImageANSAWSHandle_V2(uint64_t handleVal, con
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) { return 0; }
catch (...) { return 0; }
catch (std::exception& e) { return -3; }
catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, LStrHandle uploadedFilePath) {
auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
if (bucketName == nullptr || prefix == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr || uploadedFilePath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(_v2h));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, outPath)) {
@@ -2889,10 +2904,10 @@ extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_V2(uint64_t handleVa
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) { return 0; }
catch (...) { return 0; }
catch (std::exception& e) { return -3; }
catch (...) { return -4; }
}
extern "C" ANSULT_API int GeneratePresignedURLANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, const char* objectName, int validDays, LStrHandle presignedURL) {
@@ -2921,7 +2936,7 @@ extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_V2(uint64_t hand
auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
if (bucketName == nullptr || prefix == nullptr || fileDataPath == nullptr || fileSize <= 0 || uploadedFilePath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(_v2h));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, outPath, fileSize)) {
@@ -2933,17 +2948,17 @@ extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_V2(uint64_t hand
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) { return 0; }
catch (...) { return 0; }
catch (std::exception& e) { return -3; }
catch (...) { return -4; }
}
extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, LStrHandle uploadedFilePath) {
auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
if (bucketName == nullptr || prefix == nullptr || binaryDataPath == nullptr || objectName == nullptr || uploadedFilePath == nullptr) return 0;
AWSHandleGuard guard(AcquireAWSHandle(_v2h));
if (!guard) return 0;
if (!guard) return -1; // handle not in registry
try {
std::string outPath;
if (guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, outPath)) {
@@ -2955,10 +2970,10 @@ extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle_V2(uint64_t handleV
}
return 1;
}
else return 0;
else return AwsUploadFailureCode();
}
catch (std::exception& e) { return 0; }
catch (...) { return 0; }
catch (std::exception& e) { return -3; }
catch (...) { return -4; }
}
extern "C" ANSULT_API int DownloadFileStreamANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* objectName, const char* savedFilePath) {
@@ -3093,3 +3108,42 @@ extern "C" ANSULT_API int GetANSLoggerErrorEvents(int clearAfterRead, LStrHandle
}
catch (...) { return -1; }
}
// ---------------------------------------------------------------------------
// GetANSLoggerErrorEventsSinceLastCall — watermark-based incremental read.
// Stores highest EventRecordID seen in HKCU; subsequent calls return only
// newer events. No admin needed for the skip (only for clearAfterRead).
// ---------------------------------------------------------------------------
extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCallCpp(int clearAfterRead, std::string& result) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCallCpp: clearAfterRead=%d", clearAfterRead);
try {
result = ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(clearAfterRead != 0);
return 1;
}
catch (const std::exception& e) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCallCpp threw: %s", e.what());
result.clear();
return 0;
}
catch (...) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCallCpp threw unknown");
result.clear();
return 0;
}
}
extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCall(int clearAfterRead, LStrHandle result) {
ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCall: clearAfterRead=%d", clearAfterRead);
if (!result) return -1;
try {
std::string s;
GetANSLoggerErrorEventsSinceLastCallCpp(clearAfterRead, s);
int size = static_cast<int>(s.size());
MgErr error = DSSetHandleSize(result, sizeof(int32) + size * sizeof(uChar));
if (error != noErr) return -2;
(*result)->cnt = size;
if (size > 0) memcpy((*result)->str, s.data(), size);
return 1;
}
catch (...) { return -1; }
}
+39 -5
View File
@@ -833,7 +833,7 @@ int RSTPTestCVClient() {
// std::string rtspUrl = "rtsp://admin:admin123@192.168.1.23:554";//"rtsp://alex:admin123@192.168.1.11:554";//
//std::string rtspUrl = "rtsp://anscamuser:SonTay2020@anssontay.tplinkdns.com:554/Streaming/Channels/401";// "rtsp://admin:JA!uV_mx@42.1.117.99:8093/1/1";
std::string rtspUrl = "rtsp://admin:Grb%401212@14.224.237.228:15541/Streaming/channels/101";//"rtsp://admin:AnsHome16%&@192.168.1.104:554";//"rtsp://alex:admin123@192.168.1.11:554";//112
std::string rtspUrl ="rtsp://admin:AnsHome16%&@192.168.1.103:554";//"rtsp://alex:admin123@192.168.1.11:554";//112
CreateANSRTSPHandle(&rtspClient, "", "", "", rtspUrl.c_str());
//void SetRTSPImageQuality(ANSCENTER::ANSRTSPClient** Handle, int mode)
@@ -877,7 +877,32 @@ int RSTPTestCVClient() {
//if ((index == 2000) || (index == 2300) || (index == 2700)) { StartRTSP(&rtspClient); }
//if (index > 20000) break;
auto loopStart = std::chrono::steady_clock::now();
auto getStart = loopStart;
// ── Camera-dead watchdog (mirrors ANSVIS production behaviour) ──────
// ANSRTSPClient does NOT auto-reconnect by design: the application
// layer polls frame age and calls ReconnectRTSP. Without this, a dead
// session (camera reboot, network drop, laptop standby killing the TCP
// connection) leaves the client returning the cached last frame
// forever. Threshold is 20 s: the decoder's corruption keyframe gate
// can legitimately hold delivery for up to ~12 s (300-frame safety
// valve) during heavy packet loss, and a reconnect mid-gate would
// throw away a recovery that was about to complete. Retry at most
// every 15 s so a camera that is genuinely off doesn't get hammered.
{
static auto lastReconnectAttempt = std::chrono::steady_clock::now();
const double frameAgeMs = GetRTSPLastFrameAgeMs(&rtspClient);
if (frameAgeMs >= 20000.0 &&
loopStart - lastReconnectAttempt > std::chrono::seconds(15)) {
lastReconnectAttempt = loopStart;
const std::time_t t = std::time(nullptr);
char ts[32]; std::strftime(ts, sizeof ts, "%Y-%m-%d %H:%M:%S", std::localtime(&t));
printf("[%s] [WATCHDOG] no decoded frame for %.0f s -- calling ReconnectRTSP\n",
ts, frameAgeMs / 1000.0);
ReconnectRTSP(&rtspClient);
}
}
auto getStart = std::chrono::steady_clock::now();
cv::Mat* image = nullptr; // ✅ Use a pointer to hold the allocated image
GetRTSPCVImage(&rtspClient, width, height, pts,&image);
auto getEnd = std::chrono::steady_clock::now();
@@ -887,7 +912,10 @@ int RSTPTestCVClient() {
// ✅ Check if the image is valid BEFORE accessing it
if (!image || image->empty()) {
ANSCV_ReleaseImage_S(&image);
std::this_thread::sleep_for(std::chrono::seconds(1));
// Wait via waitKey (not sleep) so the HighGUI window keeps pumping
// Windows messages — a raw sleep here makes the "Image" window go
// "Not Responding" whenever the stream stalls for a few seconds.
if (cv::waitKey(1000) == 27) break;
continue; // Skip processing if the image is empty
}
@@ -1060,7 +1088,10 @@ int FPTestCVClient() {
// ✅ Check if the image is valid BEFORE accessing it
if (!image || image->empty()) {
ANSCV_ReleaseImage_S(&image);
std::this_thread::sleep_for(std::chrono::seconds(1));
// Wait via waitKey (not sleep) so the HighGUI window keeps pumping
// Windows messages — a raw sleep here makes the "Image" window go
// "Not Responding" whenever the stream stalls for a few seconds.
if (cv::waitKey(1000) == 27) break;
continue; // Skip processing if the image is empty
}
@@ -1549,7 +1580,10 @@ int VideoPlayerClientTest() {
// ✅ Check if the image is valid BEFORE accessing it
if (!image || image->empty()) {
ANSCV_ReleaseImage_S(&image);
std::this_thread::sleep_for(std::chrono::seconds(1));
// Wait via waitKey (not sleep) so the HighGUI window keeps pumping
// Windows messages — a raw sleep here makes the "Image" window go
// "Not Responding" whenever the stream stalls for a few seconds.
if (cv::waitKey(1000) == 27) break;
continue; // Skip processing if the image is empty
}
+56 -33
View File
@@ -4,40 +4,63 @@ add_executable(ANSCV-UnitTest
OpenCVTest.cpp
)
target_include_directories(ANSCV-UnitTest PRIVATE
${CMAKE_SOURCE_DIR}/modules/ANSCV
${CMAKE_SOURCE_DIR}/MediaClient
${CMAKE_SOURCE_DIR}/MediaClient/media
${CMAKE_SOURCE_DIR}/MediaClient/rtsp
${CMAKE_SOURCE_DIR}/MediaClient/rtp
${CMAKE_SOURCE_DIR}/MediaClient/bm
${CMAKE_SOURCE_DIR}/MediaClient/http
${CMAKE_SOURCE_DIR}/MediaClient/directx
${CMAKE_SOURCE_DIR}/MediaClient/rtmp
${CMAKE_SOURCE_DIR}/MediaClient/librtmp
${CMAKE_SOURCE_DIR}/MediaClient/srt
${CMAKE_SOURCE_DIR}/MediaClient/ffmpeg/include
${CMAKE_SOURCE_DIR}/MediaClient/openssl/include
${CMAKE_SOURCE_DIR}/MediaClient/libsrt/include
${SHARED_INCLUDE_DIR}
${ANSLIBS_DIR}/TurboJpeg
${ANSLIBS_DIR}/QRCode
# ANSCV Console Test — headless smoke test (no GUI/camera/disk), prints
# PASS/FAIL per check and exits with the failure count. Built as its own
# executable so it does not collide with ANSCV-UnitTest's GUI main().
add_executable(ANSCV-ConsoleTest
ConsoleTest.cpp
)
target_link_libraries(ANSCV-UnitTest
PRIVATE ANSCV
PRIVATE ANSLicensingSystem
PRIVATE anslicensing
PRIVATE labview
PRIVATE opencv
PRIVATE turbojpeg
PRIVATE zxing
PRIVATE CUDA::cudart
PRIVATE CUDA::nvjpeg
)
if(WIN32)
target_link_libraries(ANSCV-UnitTest PRIVATE ${WIN_COMMON_LIBS})
# ans_imshow.hpp lives outside the repo (shipped with ANSMIND). Default to the
# standard install location but allow overriding on other machines/CI via
# -DANSMIND_INCLUDE_DIR=<path>. Warn (don't fail configure) if it's missing, so
# unrelated targets still build; ConsoleTest will then fail to compile clearly.
set(ANSMIND_INCLUDE_DIR "C:/ProgramData/ANSCENTER/ANSMIND"
CACHE PATH "Directory containing ans_imshow.hpp (shipped with ANSMIND)")
if(NOT EXISTS "${ANSMIND_INCLUDE_DIR}/ans_imshow.hpp")
message(WARNING
"ans_imshow.hpp not found in ANSMIND_INCLUDE_DIR='${ANSMIND_INCLUDE_DIR}'. "
"ANSCV-ConsoleTest will fail to compile. "
"Set -DANSMIND_INCLUDE_DIR=<path> to the directory containing ans_imshow.hpp.")
endif()
target_compile_definitions(ANSCV-UnitTest PRIVATE UNICODE _UNICODE)
foreach(_anscv_test ANSCV-UnitTest ANSCV-ConsoleTest)
target_include_directories(${_anscv_test} PRIVATE
${CMAKE_SOURCE_DIR}/modules/ANSCV
${CMAKE_SOURCE_DIR}/MediaClient
${CMAKE_SOURCE_DIR}/MediaClient/media
${CMAKE_SOURCE_DIR}/MediaClient/rtsp
${CMAKE_SOURCE_DIR}/MediaClient/rtp
${CMAKE_SOURCE_DIR}/MediaClient/bm
${CMAKE_SOURCE_DIR}/MediaClient/http
${CMAKE_SOURCE_DIR}/MediaClient/directx
${CMAKE_SOURCE_DIR}/MediaClient/rtmp
${CMAKE_SOURCE_DIR}/MediaClient/librtmp
${CMAKE_SOURCE_DIR}/MediaClient/srt
${FFMPEG_INCLUDE_DIR} # FFmpeg 8.1 (n8.1); was vendored MediaClient/ffmpeg/include 4.0.2
${CMAKE_SOURCE_DIR}/MediaClient/openssl/include
${CMAKE_SOURCE_DIR}/MediaClient/libsrt/include
${SHARED_INCLUDE_DIR}
${ANSLIBS_DIR}/TurboJpeg
${ANSLIBS_DIR}/QRCode
${ANSMIND_INCLUDE_DIR} # ans_imshow.hpp (ConsoleTest.cpp)
)
target_link_libraries(${_anscv_test}
PRIVATE ANSCV
PRIVATE ANSLicensingSystem
PRIVATE anslicensing
PRIVATE labview
PRIVATE opencv
PRIVATE turbojpeg
PRIVATE zxing
PRIVATE CUDA::cudart
PRIVATE CUDA::nvjpeg
)
if(WIN32)
target_link_libraries(${_anscv_test} PRIVATE ${WIN_COMMON_LIBS})
endif()
target_compile_definitions(${_anscv_test} PRIVATE UNICODE _UNICODE)
endforeach()
+379
View File
@@ -0,0 +1,379 @@
// ANSCV Console Test
// ------------------------------------------------------------------
// A headless (no GUI) smoke test for ANSCENTER::ANSOPENCV image ops plus an
// optional RTSP capture test (ANSCENTER::ANSRTSPClient). It prints a PASS/FAIL
// line per check so results are visible in a plain console (and in CI).
// Exit code == number of failed checks (0 == all OK).
//
// Usage:
// ANSCV-ConsoleTest.exe # image ops only (no camera needed)
// ANSCV-ConsoleTest.exe <rtsp-url> # image ops + headless RTSP capture
// ANSCV-ConsoleTest.exe <rtsp-url> <maxFrames>
// ANSCV-ConsoleTest.exe <rtsp-url> <maxFrames> <saveDir>
// When <saveDir> is given:
// <saveDir>/frames/NNNN.jpg full-res captured frames (MP4 input)
// <saveDir>/preview_NNNN.jpg downscaled previews for quick viewing
// <saveDir>/rtsp_clip.mp4 short clip (NVENC, via ANSCV ImagesToMP4HW)
//
// This is intentionally separate from ANSCV-UnitTest.cpp, whose main()
// opens OpenCV windows (cv::imshow) for the same RTSP stream.
#include "ANSOpenCV.h"
#include "ANSRTSP.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <chrono>
#include <thread>
#include <filesystem>
#ifdef _WIN32
# define NOMINMAX
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
using ANSCENTER::ANSOPENCV;
using ANSCENTER::ANSRTSPClient;
using ANSCENTER::DetectionObject;
namespace {
int g_pass = 0;
int g_fail = 0;
// Report a single check. `detail` is appended for context (sizes, counts, ...).
void check(const std::string& name, bool ok, const std::string& detail = "") {
if (ok) {
++g_pass;
std::cout << " [PASS] " << name;
} else {
++g_fail;
std::cout << " [FAIL] " << name;
}
if (!detail.empty()) std::cout << " (" << detail << ")";
std::cout << std::endl;
}
std::string dims(const cv::Mat& m) {
return std::to_string(m.cols) + "x" + std::to_string(m.rows) +
" ch=" + std::to_string(m.channels());
}
// A 640x480 BGR test image: smooth gradient plus a few filled shapes so the
// transforms operate on something with real structure (not a flat color).
cv::Mat MakeTestImage(int w = 640, int h = 480) {
cv::Mat img(h, w, CV_8UC3);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
img.at<cv::Vec3b>(y, x) = cv::Vec3b(
static_cast<uchar>(x * 255 / w),
static_cast<uchar>(y * 255 / h),
static_cast<uchar>((x + y) * 255 / (w + h)));
}
}
cv::rectangle(img, cv::Rect(50, 50, 200, 150), cv::Scalar(0, 0, 255), -1);
cv::circle(img, cv::Point(450, 300), 80, cv::Scalar(0, 255, 0), -1);
return img;
}
// Headless RTSP capture test. Connects to `url`, pulls up to `maxFrames`
// frames (or until `timeoutMs` elapses), and reports resolution, frame count,
// JPEG sizes, per-grab latency and approximate FPS. No GUI — every frame is
// reported as text. Bounded so it always terminates even if the camera is
// unreachable. Adds PASS/FAIL checks to the global tally.
void RtspConsoleTest(const std::string& url, int maxFrames,
const std::string& saveDir = "", int timeoutMs = 15000) {
std::cout << "-- RTSP capture --" << std::endl;
std::cout << "URL: " << url << std::endl;
// Give slow/high-res streams enough wall-clock to reach maxFrames.
timeoutMs = (std::max)(timeoutMs, maxFrames * 1000);
std::cout << "Grabbing up to " << maxFrames << " frames (timeout "
<< timeoutMs << " ms)..." << std::endl;
// When saving, full-res frames go into <saveDir>/frames (clean, zero-padded
// names) so the MP4 encoder globs only those; previews go into <saveDir>.
std::string framesDir;
if (!saveDir.empty()) {
framesDir = saveDir + "/frames";
std::error_code ec;
std::filesystem::create_directories(framesDir, ec);
std::cout << "Saving frames to: " << saveDir << std::endl;
}
ANSOPENCV::InitCameraNetwork();
ANSRTSPClient* client = nullptr;
// CreateANSRTSPHandle(handle, licenseKey, username, password, url).
// Credentials embedded in the URL → pass empty user/pass, matching the
// existing RSTPTestClient() usage.
int created = CreateANSRTSPHandle(&client, "", "", "", url.c_str());
check("CreateANSRTSPHandle", client != nullptr,
"rc=" + std::to_string(created));
if (client == nullptr) {
ANSOPENCV::DeinitCameraNetwork();
return;
}
StartRTSP(&client);
int width = 0, height = 0;
int64_t pts = 0;
int frames = 0;
long long totalJpegBytes = 0;
long long totalGrabMs = 0;
int firstW = 0, firstH = 0;
int64_t firstPts = 0, lastPts = 0;
const auto t0 = std::chrono::steady_clock::now();
auto elapsedMs = [&] {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - t0).count();
};
int polls = 0;
while (frames < maxFrames && elapsedMs() < timeoutMs) {
++polls;
std::string jpeg;
const auto g0 = std::chrono::steady_clock::now();
GetRTSPStrImage(&client, width, height, pts, jpeg);
const auto grabMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - g0).count();
if (jpeg.empty()) {
// No frame ready yet (connecting / between frames). Back off briefly.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
continue;
}
if (frames == 0) { firstW = width; firstH = height; firstPts = pts; }
lastPts = pts;
++frames;
totalJpegBytes += static_cast<long long>(jpeg.size());
totalGrabMs += grabMs;
// Optionally persist the frame so it can be viewed outside the console.
// The grabbed bytes are already a JPEG, so the full-res file is a direct
// dump (no re-encode). The preview is decoded + downscaled for quick view.
if (!saveDir.empty()) {
// Zero-pad to 4 digits (lexicographic sort == capture order) without
// snprintf: some included headers #define snprintf -> _snprintf.
std::string idx = std::to_string(frames);
if (idx.size() < 4) idx.insert(0, 4 - idx.size(), '0');
// Full-res frame -> frames/ (MP4 input). Already JPEG: direct dump.
std::ofstream f(framesDir + "/" + idx + ".jpg", std::ios::binary);
f.write(jpeg.data(), static_cast<std::streamsize>(jpeg.size()));
f.close();
// Downscaled preview -> saveDir (chat-friendly viewing).
std::vector<uchar> buf(jpeg.begin(), jpeg.end());
cv::Mat full = cv::imdecode(buf, cv::IMREAD_COLOR);
if (!full.empty()) {
const int previewW = 960;
cv::Mat preview;
cv::resize(full, preview,
cv::Size(previewW, full.rows * previewW / full.cols));
cv::imwrite(saveDir + "/preview_" + idx + ".jpg", preview);
}
}
// Report every frame for the first few, then every 10th to stay readable.
if (frames <= 5 || frames % 10 == 0) {
std::cout << " frame " << frames << ": " << width << "x" << height
<< " pts=" << pts << " jpeg=" << jpeg.size() << "B"
<< " grab=" << grabMs << "ms" << std::endl;
}
}
const long long elapsed = elapsedMs();
StopRTSP(&client);
ReleaseANSRTSPHandle(&client);
ANSOPENCV::DeinitCameraNetwork();
std::cout << " polls=" << polls << " frames=" << frames
<< " elapsed=" << elapsed << "ms" << std::endl;
if (frames > 0) {
const double fps = elapsed > 0 ? frames * 1000.0 / elapsed : 0.0;
std::cout << " resolution=" << firstW << "x" << firstH
<< " avgJpeg=" << (totalJpegBytes / frames) << "B"
<< " avgGrab=" << (totalGrabMs / frames) << "ms"
<< " ~fps=" << fps
<< " ptsDelta=" << (lastPts - firstPts) << std::endl;
}
check("RTSP received >=1 frame", frames > 0,
std::to_string(frames) + " frames in " + std::to_string(elapsed) + "ms");
if (frames > 0) {
check("RTSP frame has valid resolution", firstW > 0 && firstH > 0,
std::to_string(firstW) + "x" + std::to_string(firstH));
}
// Encode the captured frames into a short MP4 clip using ANSCV's own
// hardware (NVENC) encoder, so the stream can actually be watched as video.
// maxWidth=1280 downscales the 4K stills to a manageable file; playback fps
// is fixed at 5 so a small burst yields a watchable clip.
if (!saveDir.empty() && frames >= 2) {
const std::string mp4Path = saveDir + "/rtsp_clip.mp4";
const int playbackFps = 5;
std::cout << " encoding MP4 (" << frames << " frames -> " << mp4Path
<< ", maxW=1280, " << playbackFps << " fps)..." << std::endl;
const int rc = ANSCV_ImagesToMP4HW_S(framesDir.c_str(), mp4Path.c_str(),
1280, playbackFps);
bool ok = rc != 0 && std::filesystem::exists(mp4Path);
std::string detail = "rc=" + std::to_string(rc);
if (ok) {
const auto sz = std::filesystem::file_size(mp4Path);
detail += ", " + std::to_string(sz) + " bytes";
}
check("RTSP frames encoded to MP4", ok, detail);
}
}
} // namespace
int main(int argc, char** argv) {
std::cout << "==== ANSCV Console Test ====" << std::endl;
// Optional RTSP target. argv[1] = URL, argv[2] = max frames (default 60),
// argv[3] = directory to save captured frames into (optional).
const std::string rtspUrl = (argc > 1) ? argv[1] : "";
const int rtspMaxFrames = (argc > 2) ? std::atoi(argv[2]) : 60;
const std::string rtspSaveDir = (argc > 3) ? argv[3] : "";
ANSOPENCV cv_;
const bool licensed = cv_.Init(""); // empty license key, matching existing tests
std::cout << "Init(\"\") license valid: " << (licensed ? "yes" : "no") << std::endl;
const cv::Mat src = MakeTestImage();
std::cout << "Source image: " << dims(src) << std::endl;
std::cout << "-- Geometry / resize --" << std::endl;
{
cv::Mat out;
cv_.ImageResize(src, 320, 240, out);
check("ImageResize 320x240", out.cols == 320 && out.rows == 240, dims(out));
}
{
cv::Mat out;
cv_.ImageResizeWithRatio(src, 320, out);
check("ImageResizeWithRatio width=320", out.cols == 320 && !out.empty(), dims(out));
}
{
cv::Mat out = cv_.ImageResizeV2(src, 200);
check("ImageResizeV2 width=200", !out.empty() && out.cols == 200, dims(out));
}
{
cv::Mat out = cv_.ImageCrop(src, cv::Rect(10, 20, 100, 80));
check("ImageCrop 100x80", out.cols == 100 && out.rows == 80, dims(out));
}
{
cv::Mat out = cv_.RotateImage(src, 90.0);
check("RotateImage 90deg", !out.empty(), dims(out));
}
{
cv::Mat out = cv_.FlipImage(src, 1); // horizontal
check("FlipImage horizontal", out.size() == src.size() && !out.empty(), dims(out));
}
{
// ShiftImage expands the canvas by |shiftX| x |shiftY| to keep all
// pixels after the translation (640+30, 480+40).
cv::Mat out = cv_.ShiftImage(src, 30, 40);
check("ShiftImage (30,40) expands canvas",
out.cols == src.cols + 30 && out.rows == src.rows + 40, dims(out));
}
std::cout << "-- Color / enhancement --" << std::endl;
{
cv::Mat out = cv_.ToGray(src);
check("ToGray -> 1 channel", out.channels() == 1 && !out.empty(), dims(out));
}
{
cv::Mat out = cv_.ImageContrastEnhancement(src);
check("ImageContrastEnhancement", !out.empty(), dims(out));
}
{
cv::Mat out = cv_.ImageWhiteBalance(src);
check("ImageWhiteBalance", !out.empty(), dims(out));
}
{
cv::Mat out = cv_.ImageDarkEnhancement(src, 1.5);
check("ImageDarkEnhancement x1.5", !out.empty(), dims(out));
}
std::cout << "-- Noise augmentation --" << std::endl;
{
cv::Mat out = cv_.AddGaussianNoise(src, 0.0, 20.0);
check("AddGaussianNoise", out.size() == src.size() && !out.empty(), dims(out));
}
{
cv::Mat out = cv_.AddSaltAndPepperNoise(src, 0.02);
check("AddSaltAndPepperNoise", out.size() == src.size() && !out.empty(), dims(out));
}
{
cv::Mat out = cv_.AddSpeckleNoise(src, 0.1);
check("AddSpeckleNoise", out.size() == src.size() && !out.empty(), dims(out));
}
std::cout << "-- Encode --" << std::endl;
{
std::string bin = cv_.MatToBinaryData(src);
check("MatToBinaryData non-empty", !bin.empty(),
std::to_string(bin.size()) + " bytes");
}
{
std::string b64 = cv_.MatToBase64(src);
check("MatToBase64 non-empty", !b64.empty(),
std::to_string(b64.size()) + " chars");
}
std::cout << "-- Parse / serialize --" << std::endl;
{
// GetBoundingBoxes parses a JSON object with a "results" array, each
// entry carrying x/y/width/height.
const std::string boxesJson =
R"({"results":[)"
R"({"x":10,"y":20,"width":30,"height":40},)"
R"({"x":100,"y":110,"width":50,"height":60}]})";
std::vector<cv::Rect> boxes = cv_.GetBoundingBoxes(boxesJson);
bool ok = boxes.size() == 2 &&
boxes[0] == cv::Rect(10, 20, 30, 40) &&
boxes[1] == cv::Rect(100, 110, 50, 60);
check("GetBoundingBoxes parses 2 boxes", ok,
std::to_string(boxes.size()) + " boxes");
}
{
std::vector<DetectionObject> dets(2);
dets[0].classId = 1; dets[0].className = "person";
dets[0].confidence = 0.9f; dets[0].box = cv::Rect(10, 10, 40, 80);
dets[1].classId = 2; dets[1].className = "car";
dets[1].confidence = 0.8f; dets[1].box = cv::Rect(60, 60, 100, 50);
std::string json = cv_.VectorDetectionToJsonString(dets);
check("VectorDetectionToJsonString non-empty", !json.empty(),
std::to_string(json.size()) + " chars");
}
if (!rtspUrl.empty()) {
RtspConsoleTest(rtspUrl, rtspMaxFrames, rtspSaveDir);
} else {
std::cout << "-- RTSP capture --" << std::endl;
std::cout << " (skipped: no RTSP URL given; pass one as the first "
"argument to run it)" << std::endl;
}
std::cout << "============================" << std::endl;
std::cout << "Result: " << g_pass << " passed, " << g_fail << " failed."
<< std::endl;
// NOTE: at process exit one of the loaded DLLs throws a C++ exception
// during DllMain(PROCESS_DETACH) (observed exit code 0xE06D7363),
// regardless of test outcome. All test work is complete by this point, so
// force a deterministic exit code for CI. TerminateProcess is used because
// both return and quick_exit go through ExitProcess, which still triggers
// the faulty DLL-detach path; TerminateProcess skips it.
std::cout.flush();
std::cerr.flush();
#ifdef _WIN32
::TerminateProcess(::GetCurrentProcess(), static_cast<UINT>(g_fail));
#endif
return g_fail;
}
+663 -13
View File
@@ -19,6 +19,8 @@
#include <chrono>
#include <thread>
#include <iomanip>
#include <filesystem>
#include <set>
#include "ANSFR.h"
#include "fastdeploy/vision.h"
#include "ANSFilePlayer.h"
@@ -29,6 +31,12 @@ using namespace cv;
using namespace dnn;
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>
T GetOptionalValue(const boost::property_tree::ptree& pt, std::string attribute, T defaultValue) {
if (pt.count(attribute)) {
@@ -723,7 +731,7 @@ int ANSVISTest() {
// Get the current working directory
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 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";
@@ -731,7 +739,7 @@ int ANSVISTest() {
//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\\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";
const char* configFilePath = "";
@@ -1141,7 +1149,7 @@ int ANSVISTestFilePlayer() {
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 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 videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\school1.mp4";
@@ -1294,7 +1302,7 @@ int FRStressTest() {
std::filesystem::path currentPath = "C:\\Programs\\DemoAssets";
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 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 imagePath = "C:\\Programs\\DemoAssets\\Images\\TestFaces\\Tien1.jpg";
@@ -1619,25 +1627,668 @@ int InsertFace(std::string databaseFilePath, std::string recognizerFilePath, std
}
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 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 imageFile = "E:\\Programs\\DemoAssets\\Images\\Face\\Original.jpg";
std::string imageFile = "E:\\Programs\\DemoAssets\\Images\\Face\\61.png";
//InsertUser(databaseFilePath, recognizerFilePath, facedetectorFilePath,"0001","Tien");
std::string imageFile = "C:\\Programs\\DemoAssets\\OIP.jpg";
InsertUser(databaseFilePath, recognizerFilePath, facedetectorFilePath,"0001","Tien");
InsertFace(databaseFilePath, recognizerFilePath, facedetectorFilePath, imageFile);
std::cout << "End of program.\n";
return 1;
}
int main()
// ---- SCRFD-TRT face-detection reproduction on a video (headless) ----------
int SCRFDVideoRepro()
{
std::string licenseKey = "";
const char* configFilePath = "";
std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db";
std::string recognizerFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ServerOptimised\\ANS_FaceRecognizer_v1.1_NVIDIAGeForceRTX3070LaptopGPU.zip";
std::string detectorFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ServerOptimised\\ANS_GenericFD(GPU)_v1.0_NVIDIAGeForceRTX3070LaptopGPU.zip";
std::string videoPath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
std::cout << "[REPRO] detector = " << detectorFile << std::endl;
std::cout << "[REPRO] recognizer = " << recognizerFile << std::endl;
ANSCENTER::ANSFacialRecognition* h = nullptr;
int cr = CreateANSRFHandle(&h, licenseKey.c_str(), configFilePath,
databaseFilePath.c_str(), recognizerFile.c_str(),
detectorFile.c_str(), 0.3, 1, 1, 1);
std::cout << "[REPRO] CreateANSRFHandle = " << cr << std::endl;
int le = LoadANSRFEngine(&h);
std::cout << "[REPRO] LoadANSRFEngine = " << le << std::endl;
cv::VideoCapture cap(videoPath);
if (!cap.isOpened()) { std::cout << "[REPRO] ERROR: cannot open video " << videoPath << std::endl; return 1; }
const std::string win = "ANSFR - SCRFD/TensorRT Face Detection (ESC to quit)";
cv::namedWindow(win, cv::WINDOW_NORMAL);
cv::resizeWindow(win, 1280, 720);
int frameIdx = 0, totalDet = 0, saved = 0;
cv::Mat frame;
while (cap.read(frame)) {
if (frame.empty()) break;
++frameIdx;
if (frameIdx > 300) break; // proof run: first 300 frames
unsigned int bufLen = 0;
unsigned char* jpg = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen);
std::string res = RunANSRFInferenceBinary(&h, jpg, frame.cols, frame.rows);
delete jpg;
int n = 0;
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 int uid = GetData<int>(r, "user_id");
const std::string uname = GetData<std::string>(r, "user_name");
const float x = GetData<float>(r, "x");
const float y = GetData<float>(r, "y");
const float w = GetData<float>(r, "width");
const float ht = GetData<float>(r, "height");
cv::rectangle(frame, cv::Rect((int)x, (int)y, (int)w, (int)ht), cv::Scalar(0, 255, 0), 2);
cv::putText(frame, cv::format("%s:%d", uname.c_str(), uid), cv::Point((int)x, (int)y - 8),
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
++n;
}
}
} catch (...) {}
totalDet += n;
cv::putText(frame, cv::format("Frame %d faces detected = %d", frameIdx, n),
cv::Point(25, 45), cv::FONT_HERSHEY_SIMPLEX, 1.1, cv::Scalar(0, 255, 255), 2, cv::LINE_AA);
if (frameIdx % 30 == 0) {
std::string fp = cv::format("C:\\Programs\\DemoAssets\\ANSFR_proof_frame_%04d_faces%d.jpg", frameIdx, n);
cv::imwrite(fp, frame);
++saved;
std::cout << "[REPRO] saved annotated frame: " << fp << std::endl;
}
cv::imshow(win, frame);
if (cv::waitKey(1) == 27) break; // ESC to quit
}
std::cout << "[REPRO] played " << frameIdx << " frames, total faces drawn = " << totalDet
<< ", saved " << saved << " annotated images to C:\\Programs\\DemoAssets\\" << std::endl;
cap.release();
cv::destroyAllWindows();
// NOTE: intentionally skipping ReleaseANSRFHandle/cudaDeviceReset here — there is
// a known C++ exception at teardown (0xE06D7363); process exit reclaims everything.
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)
{
// 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) ─────────────
// 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 → all GPUs (default behaviour)
// Sets CUDA_VISIBLE_DEVICES before the first CUDA call in this process, so
// the CUDA runtime only enumerates the chosen physical GPU (it becomes
// device 0 inside the process).
//for (int i = 1; i + 1 < argc; ++i) {
// if (std::string(argv[i]) == "--gpu") {
// _putenv_s("CUDA_VISIBLE_DEVICES", argv[i + 1]);
// std::cout << "==============================================\n"
// << " GPU BISECT: CUDA_VISIBLE_DEVICES=" << argv[i + 1] << "\n"
// << " (only this physical GPU is visible to CUDA)\n"
// << "==============================================" << std::endl;
// }
//}
//FaceDetectorTest();
//TestFaceRecognition();
//TestCompleteFR1();
//TestModelSwitchV14();
ClassroomAttributesVideoTest();
//ANSVISTest();
//ANSVISImageTest();
//ANSVISTest();
//ANSVISTestFilePlayer();
@@ -1653,8 +2304,7 @@ int main()
// TestCompleteFR();
//TestFaceRecognition();
//FaceRecognitionBenchmark();
ANSVISTestCPU_Lightweight();
//ANSVISTestCPU();
std::cin.get();
//SCRFDVideoRepro();
return 0;
}
@@ -0,0 +1,481 @@
// 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.nonceSeed = 9;
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.
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.nonceSeed = 21;
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);
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 "ANSMotionDetector.h"
#include "ANSONNXSAM3.h"
#include <cstdlib>
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
@@ -1647,6 +1649,9 @@ int HelmetDetectionTest() {
}
// ============================================================================
// ElasticGPUTest — Verify elastic pool, round-robin dispatch, multi-GPU,
// concurrent inference, and idle-slot cleanup.
@@ -2222,14 +2227,47 @@ int RTMPOSETest() {
VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx
return 0;
}
int main()
int FireNSmokeTest() {
int modelType = 16; // Custom model type for helmet detection, adjust as needed based on how the model is set up in the engine
std::string onnxclmodelFilePath = "C:\\Projects\\ANSVIS\\Models\\ANS_FireSmoke_v2.0\\ANS_FireSmoke_v2.0.zip";
std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\FireNSmoke\\BCA1.mp4";
VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx
return 0;
}
// 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();
//ALPROD_Test();
//FireNSmokeTest();
FireNSmokeTest();
//ElasticGPUTest();
RTMPOSETest_CPP();
//RTMPOSETest_CPP();
//RTMPOSETest();
//YOLO26SEGYolo11Test();
//YOLO26POSEYolo11Test();

Some files were not shown because too many files have changed in this diff Show More