Repository navigation
Conversation
|
Thanks @jayleaton. The Pillow-exact LANCZOS and the byte-identical BiRefNet input are good to see. On the pin: let's keep the exact |
931d76c to
f064dc0
Compare
f064dc0 to
a03d468
Compare
|
I found a compile blocker on exact head zig build tf-idol-generate tf-idol-birefnet tf-kernel-contract -Dcpu=apple_m1 -j7 --summary allThe first failures are missing I also ran The trial merge into Igor Lessio - AIFLOW LABS |
a03d468 to
19a94a0
Compare
|
Rebased onto #1780's new head at
Host checks (Linux): |
19a94a0 to
ccfe66e
Compare
|
The updated zig build tf-idol-generate tf-idol-birefnet tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j4 --summary all
./zig-out/bin/tf-kernel-contract --only birefnetAgainst main Igor Lessio - AIFLOW LABS |
…oders, the denoiser and the decoders to a PLY Each stage is timed and each model freed after it. The starting noise is torch's CPU-seeded randn, reimplemented; the VAE's posterior noise and the starting noise can be given for parity with the SDK's MPS draws. The recipe's End to end section has the stage timings, memory and PLY comparison against the SDK's own IdolPipeline run.
…as AIFlowML found picture() checked only the element count, so `pixels` as float32 [1024, 1024, 3] (HWC) passed and was then read as CHW: a scrambled image reached both encoders. families/idol/picture.zig now requires `pixels` to be exactly float32 [3, 1024, 1024] and `image` exactly [1024, 1024, 3], and its host test plants the HWC `pixels`, a flat tensor, a bf16 one and a CHW `image`, each refused.
… idol.md under 600 lines; the picture's layouts
… under 600 lines with both encoders
ccfe66e to
6527531
Compare
|
Thanks @AIFlowML for retesting Ash's ccfe66e on the M5 Max: 71/71 steps, and BiRefNet's 24 kernels with 21/21 controls and no limit problems. Rebased onto the rebased #1780 (6e4d936, on main c35d9f7). The new head is 6527531, and Ash's four commits are kept on top of mine. Of the six overlaps, only Host checks on 6527531:
|
|
The rebase cleared the six overlaps I reported. On M5 Max with Zig 0.17.0: zig build tf-idol-generate tf-idol-birefnet tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j2 --summary all
./zig-out/bin/tf-kernel-contract --only birefnetThe build passed 71/71 steps. Metal passed 24 BiRefNet kernels and caught all 21 planted controls, with zero limit problems. A synthetic RGBA PNG through Igor Lessio - AIFLOW LABS |
|
Thanks @jayleaton. The raw-photo and generation paths are well structured and covered. One receipt/refusal fix is needed: a
|
tf-idol-generate's torch CPU generator moves to idol/torch_rng.zig, so its test runs without Metal. It checks the [8192, 16] then [5] draw for seed 42 against torch 2.5.1+cpu's values, produced with ATEN_CPU_CAPABILITY=default (the scalar normal_fill_16 the Mac runs): the camera's 5 values bit for bit, the latent's ends within 3 ulps and its sum, since torch's float logf, cosf and sinf are the platform's libm.
BiRefNet (Swin-L, ASPP-deformable decoder) in fp32 on Metal from the Idol snapshot's birefnet.safetensors, the SDK's preprocess on the host byte for byte (Pillow LANCZOS, erosion, crop, paste; torch's CPU bilinear input), the tf-idol-birefnet gate tool, its case tool tools/zig/idol_birefnet_case.py and the kernel contract for every idol_birefnet.metal kernel.
PICTURE may be a .png, .jpg, .jpeg or .webp photo, preprocessed natively with BiRefNet; --mode synthetic|real and --crop-margin X; a photo writes OUT_DIR/preprocessed.png and adds preprocess_s and birefnet_s to the JSON. Recipe section and CHANGELOG line.
…upported probes, as 5a4583c asks
Rebase adaptation: expose the supplied preprocessing, photo and BiRefNet modules through the public Idol namespace, and retain their import tests.
…s review asks A --noise file with `latent` and `camera` but no `noise` tensor left the VAE posterior's noise native while the run record said "given". The record now takes the choice encode made (idol/run_noise.zig): native, given, or unused when --features skips the encoders; a host test covers the noise-less file.
6527531 to
a556f29
Compare
|
Thanks for the review. Pushed a556f29 (with a lease), rebased onto #1780's new head 8c28d5f. Ash's four commits are kept; the only conflict was the I chose to report the posterior noise that actually ran, rather than require a
Checks: fmt, |
|
Thanks @jayleaton for the quick fix. What's confirmed:
One thing still needed: the LANCZOS resize in
Ours, nothing needed from you: the conflicts with main are all mechanical. Main has since landed #1780's files, and we'll handle them.
|
…as review asks The photo's LANCZOS resizes go through resample.zig's Pillow-exact Lanczos-3 instead of preprocess.zig's own resampler, which is removed. The shared View gains a channel count (3 by default) so RGBA photos resize there too, each channel alike; its other callers are unchanged. Its bytes equal the old resampler's on every case tried and Pillow 13.0.0's on the SDK's resizes (the first to a 1,024 short side, RGB and RGBA, and the crop's to 1,024), so BiRefNet's input is unchanged. A host test pins an RGBA shrink and enlarge to Pillow 13's bytes.
|
Thanks for the review. I pushed 7a2faf0 as a fast-forward on top of a556f29 and didn't rebase. The conflicts with main are yours, as agreed.
Checks: fmt, |
|
Thanks @jayleaton. The shared Lanczos call and RGBA goldens close the duplicate-resampler request. The noise record and exact JPEG pin remain correct. One crash needs fixing: Please also clarify the origin of
|
…ed, as review asks --crop-margin 1e300 passed the positive check and then overflowed the crop box's integer conversion. preprocess.checkMargin now refuses a margin that is not finite and above 0, and finish refuses a crop whose side would pass 16,384 (1 GiB of RGBA) before converting or allocating anything; a case's own crop_margin goes through the same checks. tf-idol-generate's other numeric flags parse through idol/run_flags.zig: --steps 1 to 1,000 (steps + 1 overflowed at the largest usize), --cfg finite, --gaussians in the decoders' range at parse time rather than after the encoders and denoiser ran, --seed a u64. Host tests cover 1e300, inf, nan, 0 and negative margins, a crop past the allocation bound, one past the picture's edges, and each flag's refusals.
… review asks br_erf was musl's erff. It is now our own: the Maclaurin series' terms to n = 10 below 1 (coefficients 2 / sqrt(pi) (-1)^n / (n! (2n + 1)), with x kept exact), and 1 - exp(-x^2) R(1 / x) from 1 to 4, R a degree-10 least-squares fit to erfc(x) exp(x^2) (relative error 2.3e-9 in float64); 1 past 4. idol/birefnet_erf.zig runs the same fp32 steps on the host: within 1.52 ulp of float64's erf over every positive fp32 to 6 (musl's was 0.76), well inside the GELU contract's 8 ulps.
|
Thanks for the review. I pushed 4b0acd5 on top of 7a2faf0 as two commits and didn't rebase. The conflicts with main are yours, as agreed. Crop crash (5f1d806)
Still outstanding: the Mac goldens. I couldn't run the BiRefNet goldens ( Checks
|
|
Thanks @jayleaton. The crop bounds fix the crash, the numeric flag checks are present, and the library-derived erf has been replaced with the math-based implementation. The shared-resampler and noise-record fixes remain credited. The changed GELU now needs its exact native serial comparison and the current Mac background-removal and preprocessing goldens. The host ULP bound and older Metal results do not qualify the new arithmetic; the existing tolerance cannot replace exact output checks. The current-main composition remains ours.
|
Stacked on #1780 (and through it #1773, #1774, #1775, #1766). Its own change is the last two commits (
ee8343a4a,b0642cb1d). Review after #1780.What this changes
Idol's background removal (BiRefNet) and its image preprocessing natively on Metal, so
tf-idol-generatetakes a raw photo. For #1757: with this, the wholeIdolPipelineruns natively on M5 Metal, from a photo to a 3DGS PLY. Details: the "Background removal and preprocessing" part ofdocs/recipes/idol.md.BiRefNet (Swin-L backbone with window attention and relative position bias, ASPP with deformable convolution, the decoder) in
zig/kernels/metal/idol_birefnet.metalandbirefnet*.zig, withtf-idol-birefnetand anidol_birefnet_case.pycase. These are Idol's own kernels; no shared kernel changes.Preprocessing on the host (
preprocess.zig,photo.zig), asidol/preprocess.pydoes it:MinFilter(3)erosion;Pillow's fixed-point LANCZOS is matched byte for byte. Photos are decoded by
core/picture. No EXIF orientation is applied, matching the SDK.Weights and licence
birefnet.safetensorsin thesperidlabs/idolsnapshot (Apache 2.0, ungated). It isZhengPeng7/BiRefNetate2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4(MIT, ungated) in fp16: all 742 of its tensors equal the upstream checkpoint's byte for byte, and it leaves out 12 side-output tensors (decoder.conv_ms_spvn_*) that only training uses.CLI additions to
tf-idol-generate(#1780)Additive only: every existing argument, flag, default and output is unchanged, and a safetensors PICTURE gives the same JSON keys as before.
PICTUREmay be a.png,.jpg,.jpegor.webpphoto (any case of extension), preprocessed natively; BiRefNet's weights come fromSNAPSHOT_DIR/birefnet.safetensors, which the snapshot already holds.--mode synthetic|real: the SDK's mode; defaultsynthetic(crop margin 1.2),realis 1.7.--crop-margin Xoverrides the mode's margin.OUT_DIR/preprocessed.png, the 1,024 x 1,024 RGB the encoders saw.tf-idol-generate.jsonaddspreprocess_sandbirefnet_s; for a photo,load_sincludes BiRefNet's load. stderr gains apreprocess:line.Precision
The SDK runs BiRefNet in fp16. This port computes in fp32 on the fp16 weights.
br_convadds its sums in blocks of 256 terms: one running sum over the squeeze's 51,840 terms put it at twice torch's distance. BiRefNet's input matches torch's CPU bilinearfma(t0, w0, t1 * w1)per axis, so it is byte-identical to the SDK's. Attention keeps fp32 scores and sums and rounds P itself before P V (#1772).Kernel contract
zig/tests/contract/metal_idol_birefnet*.zig: 24 kernels, 21 controls.All of Idol's together (
--only "metal idol"):Two limits:
br_window,br_gateandbr_headtake gate 4's stated-reason path. Simulated fp16 sums would miss by only 61x, 84x and 3x, so gate 4 does not itself prove that those three need fp32 sums.br_conv'syoff/row0,br_norm's andbr_resize'sldy/yoff). The end-to-end gates exercise them.Receipt
Environment: Mac Studio M5 Ultra 256 GB, macOS 27; this branch on idol: tf-idol-generate, one picture to a 3DGS PLY end to end on Metal (stacked on #1773, #1774, #1775) #1780's
192c4e902; idol-sdk4702a7f0on torch 2.13.0 (MPS).Photos: the avocado input, a WebP, and an opaque photo in synthetic and real mode (BiRefNet runs), plus an RGBA photo with real alpha (BiRefNet skipped).
Exactness:
1 - cosis about 1e-12 for both. The gate is twice torch's distance, worst row and cosine.End to end from the raw photo (seed 42, 50 steps, 262,144 Gaussians), against the SDK's
IdolPipelinefrom the same photo:Tests:
zig build test test-golden -Dcpu=apple_m1: 585 of 585 steps, 8,968 of 9,021 tests pass, 53 skipped, 0 fail; goldens 55 of 55 and 322 of 335 equal, 0 unexpected differences.zig fmt --check build.zig zig: clean.lean_check.py: 424, the same as main.Not run: CUDA (none in this PR); Metal M1 to M4. JPEG: see below.
JPEG
core/picturedecodes JPEG only through libjpeg-turbo at exactly the pinned3.1.4.1. To measure JPEG input I built that release from its official tarball (sha256ecae8008…a022, equal to GitHub's digest for the release asset) into a private prefix, and pointed only TensorFold's runs at it withTENSORFOLD_LIBTURBOJPEG. The build reportslibjpeg-turbo version 3.1.4.1, with the arm64 NEON SIMD path. The system's and Homebrew's libraries were not touched.The test photo is the opaque avocado photo saved by Pillow as a quality-92 JPEG, 1,280 x 960. The SDK decodes it with Pillow 13.0.0, whose bundled libjpeg-turbo is 3.2.0.
tf-idol-birefnet: all gates pass.1 - cos9.0e-13 (torch 1.3e-12).tf-idol-generatefrom the JPEG, against the SDK'sIdolPipelinefrom the same file:preprocessed.pngequal to the SDK's input picture, 0 pixels different;core/picture's own goldens test ("every picture 0.6.6 decoded ...") ran its JPEG pixel cases with the library and passed. Without the library it prints that they were skipped.zig build test test-golden -Dcpu=apple_m1with the variable set: 585 of 585 steps; goldens 55 of 55 and 322 of 335 equal, 0 unexpected differences.A question for review, about the pin: the check refuses any libjpeg-turbo whose built-in version string is not exactly
3.1.4.1. Homebrew's current 3.2.1 is refused by name, so a stock Mac decodes no JPEG until someone builds 3.1.4.1 by hand. On this photo, 3.1.4.1 gave the same pixels as Pillow 13's bundled 3.2.0. Should the pin stay an exact match, or accept a set of releases whose decode is verified against the goldens (for example 3.1.4.1 and 3.2.x)? I have not changed the pin.Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (n/a.)SUMMARYline is in the receipt.