Fix Tensor::normalize()/norm() and PHP 8.4/8.5 deprecations - #106
Open
vendelev wants to merge 5 commits into
Open
Fix Tensor::normalize()/norm() and PHP 8.4/8.5 deprecations#106vendelev wants to merge 5 commits into
vendelev wants to merge 5 commits into
Conversation
TensorBuffer implements ArrayAccess/Countable but not Iterator or IteratorAggregate. `foreach ($this->buffer as ...)` on such an object iterates its public properties instead of raising an error, which for TensorBuffer means zero iterations. As a result, norm()'s axis-reduction loop never accumulated anything and normalize() never divided any value, silently turning both into no-ops for real (non-scalar) tensors. Use toBufferArray() instead, matching the pattern already used by the axis === null branch in norm().
The condition was inverted: raising the accumulated sum to the power of 1/ord was gated on `$ord === 1`, where 1/1 = 1 makes it a no-op anyway. For the common case $ord = 2 (L2 norm) the square root was never taken, so norm() returned the sum of squares instead of the actual norm.
mb_str_split() can return multi-byte graphemes (e.g. Cyrillic), and ord() on PHP 8.4+ is deprecated when given a string longer than one byte. Use ord($c[0]) as suggested by the deprecation notice itself, which preserves the previous (first-byte) behaviour.
curl_close() has been a no-op since PHP 8.0 (CurlHandle is closed by the garbage collector) and is deprecated since PHP 8.5.
Adds regression tests for the two bugs fixed in the preceding commits: - norm() with an axis now actually accumulates values (buffer iteration) and takes the 1/ord root for L2. - normalize() actually divides every element by the computed norm, producing a unit-length vector. All four new tests fail against the pre-fix implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What:
Description:
Three bugs found while integrating TransformersPHP into a PHP-only semantic
search pipeline (E5 embeddings + pgvector).
Tensor::normalize()/Tensor::norm()(axis branch) silently no-op.TensorBufferimplementsArrayAccess/Countablebut notIterator/IteratorAggregate.foreach ($this->buffer as $i => $value)on such an object iterates its public properties instead of throwing —
for
TensorBufferthat means zero iterations. Sonorm()'saccumulation loop never ran and
normalize()never divided anything.Confirmed via
feature-extraction/embeddingspipeline withnormalize: true: the returned vector was not unit-length at all(
‖v‖² ≈ 20.5instead of1.0) for a real 384-dim E5 embedding.Fix: iterate over
toBufferArray()instead, same pattern already usedby the
axis === nullbranch a few lines above.norm()never takes the root for L2 (and any$ord != 1).if ($ord === 1) { $result = $mo->op($result, '**', 1 / $ord); }— for$ord = 1this is a no-op anyway (1/1 = 1), and for$ord = 2(thecommon L2 norm) the square root is never applied, so
norm()returned the sum of squares instead of the norm. Condition should be
$ord !== 1.ord()deprecation on PHP 8.4+ inPrecompiled::DoubleArray::commonPrefixSearch().mb_str_split($key)can yield multi-byte graphemes (e.g. Cyrillic),and
ord()on a string longer than one byte is deprecated sincePHP 8.4. Fixed with
ord($c[0]), as suggested by the deprecationnotice itself (same first-byte behaviour, no warning).
curl_close()deprecation on PHP 8.5 inDownloader.php.curl_close()has been a no-op since PHP 8.0 (the handle is closed byGC) and triggers a deprecation notice on PHP 8.5. Removed the three
calls.
All four are isolated, one commit per bug. Verified end-to-end against
Xenova/multilingual-e5-small: before the fix,normalize: trueproducednon-unit vectors; after,
‖v‖² = 1.000000exactly.Added regression tests in tests/tensors/TensorTest.php, verified they fail against the pre-fix code.
Related: