Skip to content

make-binary-drop: count embedded AMDGPU code objects, not a target-triple string - #1448

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
1bit/1443-drop-code-objects
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
1bit/1443-drop-code-objects

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Fixes #1443

make-binary-drop.sh accepted libggml-hrx.so only if strings found amdgcn-amd-amdhsa, so it tested a spelling, not a property. HRX embeds its kernels as raw AMDGPU code objects, and how the library was assembled decides whether that string appears. The release build's library carries 54 code objects and not one copy of the string.

Change: scripts/pack-elf.sh gets pack_amdgpu_objects <file>, which counts the ELF64 headers embedded in the file whose e_machine is EM_AMDGPU (224). The outer x86-64 ELF has e_machine 62 and is not counted. The drop now fails only when that count is 0. It needs no new tool: tr plus grep -P. Newlines are mapped to 0xff first, because grep is line-oriented and would otherwise miss a header that holds a 0x0a byte. The fixture test caught that case on the first version.

Measured

The count, against a full ELF-header parse in Python, on real libraries on this box:

libggml-hrx.so.0.18.0 size pack_amdgpu_objects header parse old string count old check new check
release build (release-rc/work/build) 11 854 752 54 54 0 refused pass
c22 payload 10 825 848 50 50 0 refused pass
an installed 1bit-hrx-41ff9df 8 016 976 35 35 35 pass pass
a 2-object partial HRX build 5 513 432 2 2 2 pass pass
1bit-canonical (no HIP add-on) 5 129 432 0 0 0 refused refused

A build with no kernels is still refused; it also fails the separate ggml_hrx_hip_addon_register check, which is unchanged.

The drop itself, end to end, on the release build: make-binary-drop.sh <checkout> <out> --build release-rc/work/build --npu-lib <libnpu_rt_api.a>, under thermal-run, output to a scratch dir.

result
origin/main FAIL libggml-hrx.so carries no HIP code objects, checks failed, nothing written, rc 1
this PR checks: ok, drop … written: 21 files. Nothing published., rc 0

Test: tests/pack_amdgpu_objects_test.sh (ctest pack_amdgpu_objects) runs on fixture bytes; it needs no build and no tar. Its rows:

  • code objects without the string count;
  • the string alone counts 0;
  • the host ELF and a 32-bit e_machine-224 header count 0;
  • a header containing a newline byte counts;
  • a missing file counts 0;
  • the drop really calls the helper and no longer greps the string.

On this branch: 8/8 ok. Against origin/main's make-binary-drop.sh: the two script rows fail, as they should. pack_modes is still ok, and tools/copyright.py --check is ok.

docs/releases.md ("The public binary drop") now says how the kernels-in check counts.

Nothing was published or tagged, and no device was used.

🤖 Generated with Claude Code

…amd-amdhsa string

The drop accepted libggml-hrx.so only if strings found the target
triple. The release build's library carries 54 embedded AMDGPU code
objects and no such string, so the drop refused every real build, while
a 2-object partial build carried it and passed.

pack_amdgpu_objects (scripts/pack-elf.sh) counts ELF64 headers whose
e_machine is EM_AMDGPU (224). It matches a full header parse on five
real libraries (50, 54, 2, 0, 35), and a build with no kernels still
counts 0. New fixture test: pack_amdgpu_objects_test.sh.

Fixes #1443

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

ready for review at b613985

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY at b613985e.

The counting rule is the property, not a string, and it is the one that is right: \x7fELF, ELFCLASS64, little-endian, then e_machine at
offset 18 equal \xe0\x00 (EM_AMDGPU = 224) — which excludes the outer x86-64 ELF by e_machine 62, precisely the mistake the sibling
implementation makes. My own check on that sibling: a plain gcc executable with zero AMDGPU objects returns 1 there, because it counts the
container's own ELF magic; this rule returns 0, and on a real library it reports 54 where that one reports 56.

Two details that make this more than a regex:

  • tr '\n' '\377' before the grep, with the reason in the comment: grep is line-oriented, so a header whose skipped bytes contain 0x0a would
    be split and missed. That is the kind of byte-level bug that makes a count under-report on some libraries and not others.
  • "Measured equal to a full header parse on five shipped/built libraries" — the pattern is validated against the thing it approximates, which is
    the only way a magic-number scan should be trusted.

It also lives where the repo's other pack helpers live (pack_elf.sh, beside pack_normalise_modes/pack_writable_files), is used by
make-binary-drop.sh in the same commit, and has its own test (tests/pack_amdgpu_objects_test.sh, 66 lines).

One thing the test should pin now that the rule is right: the sibling's failure was invisible because its test only asserted non-zero. A case
asserting 0 for a plain x86-64 executable (or a build with no amdgcn objects) would make the "counts the container" bug impossible to
reintroduce — and would have caught the other PR.

Duplicate: #1447 fixes the same make-binary-drop.sh count with the weaker rule; merge-tree reports a conflict between the two. This one is
the one to land.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Closing as superseded by #1452, which merged at 11:49Z and closed #1443. I am not rebasing this, because this PR has a real false negative that #1452 does not.

While rebasing I measured both rules on six real libggml-hrx.so.0.18.0 files:

library real AMDGPU code objects (Python header parse) #1452 on main: \x7fELF images > 1 this PR: pack_amdgpu_objects
release build (release-rc/work/build) 54 56, pass 54, pass
c22 payload 50 52, pass 50, pass
installed 1bit-hrx-41ff9df 35 36, pass 35, pass
2-object partial build 2 3, pass 2, pass
1bit-canonical (no add-on) 0 1, refused 0, refused
rlk-build 49 51, pass 0, refused (wrong)

Cause: to catch a header containing a 0x0a byte, this PR maps every newline to 0xff before grep -P. That turns an 11 MB library into a single line, and grep -P then fails silently on it (its stderr is discarded), so the helper returns 0. On rlk-build the same pattern without the tr finds all 49. It happened to work on the other libraries, and the fixture test only uses small files, so it never exercised this.

This also corrects what I wrote above: the claims that "a build with no kernels counts 0" and that rlk-build "rightly" counts 0 came from this broken helper. rlk-build has 49 code objects and the add-on export, and main's rule accepts it correctly.

#1452's rule (grep -ao per match, no PCRE) is right on all six. One note for its owner, not a blocker: it counts any \x7fELF byte run, and these libraries carry one stray run that is not an ELF header (e_machine 46863). So a kernel-less library with one such run would pass. The separate ggml_hrx_hip_addon_register check still refuses that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make-binary-drop: the code-object check greps a string the shipped release library does not contain (56 code objects, 0 matches)

1 participant