Skip to content

Commit 0564dd1

Browse files
fix: Tensor topK error when -1 is passed
additional: New tensor method `random`
1 parent 3008013 commit 0564dd1

File tree

7 files changed

+43
-14
lines changed

7 files changed

+43
-14
lines changed

src/Pipelines/AudioClassificationPipeline.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Codewithkyrian\Transformers\Utils\Audio;
99

10+
use function Codewithkyrian\Transformers\Utils\array_pop_key;
11+
1012
/**
1113
* Audio classification pipeline using any `AutoModelForAudioClassification`.
1214
* This pipeline predicts the class of a raw waveform or an audio file.
@@ -39,7 +41,7 @@ class AudioClassificationPipeline extends Pipeline
3941
{
4042
public function __invoke(array|string $inputs, ...$args): array
4143
{
42-
$topK = $args["topK"] ?? 1;
44+
$topK = array_pop_key($args, 'topK', 1);
4345

4446
$isBatched = is_array($inputs);
4547

src/Pipelines/FillMaskPipeline.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Codewithkyrian\Transformers\Pipelines\Pipeline;
1010
use Codewithkyrian\Transformers\Utils\Math;
1111

12+
use function Codewithkyrian\Transformers\Utils\array_pop_key;
13+
1214
/**
1315
* Masked language modeling prediction pipeline.
1416
*
@@ -34,7 +36,7 @@ class FillMaskPipeline extends Pipeline
3436
{
3537
public function __invoke(array|string $inputs, ...$args): array
3638
{
37-
$topK = $args["topK"] ?? 5;
39+
$topK = array_pop_key($args, 'topK', 5);
3840

3941
$modelInputs = $this->tokenizer->__invoke($inputs, padding: true, truncation: true);
4042

src/Pipelines/ImageClassificationPipeline.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Codewithkyrian\Transformers\Models\Output\SequenceClassifierOutput;
99
use Codewithkyrian\Transformers\Utils\Math;
10+
11+
use function Codewithkyrian\Transformers\Utils\array_pop_key;
1012
use function Codewithkyrian\Transformers\Utils\prepareImages;
1113
use function Codewithkyrian\Transformers\Utils\timeUsage;
1214

@@ -54,7 +56,7 @@ class ImageClassificationPipeline extends Pipeline
5456
{
5557
public function __invoke(array|string $inputs, ...$args): array
5658
{
57-
$topK = $args["topK"] ?? 1;
59+
$topK = array_pop_key($args, 'topK', 1);
5860

5961
$isBatched = is_array($inputs);
6062

src/Pipelines/QuestionAnsweringPipeline.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Codewithkyrian\Transformers\Models\Output\QuestionAnsweringModelOutput;
99
use Codewithkyrian\Transformers\Utils\Math;
1010

11+
use function Codewithkyrian\Transformers\Utils\array_pop_key;
12+
1113
/**
1214
* Question answering pipeline
1315
*
@@ -27,7 +29,7 @@ public function __invoke(array|string $inputs, ...$args): array
2729
{
2830
$question = $inputs;
2931
$context = $args[0] ?? $args["context"];
30-
$topK = $args["topK"] ?? 1;
32+
$topK = array_pop_key($args, 'topK', 1);
3133

3234
$inputs = $this->tokenizer->__invoke($question, $context, padding: true, truncation: true);
3335

src/Pipelines/TextClassificationPipeline.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Codewithkyrian\Transformers\Models\Output\SequenceClassifierOutput;
99
use Codewithkyrian\Transformers\Tensor\Tensor;
1010

11+
use function Codewithkyrian\Transformers\Utils\array_pop_key;
12+
1113
/**
1214
* Text classification pipeline
1315
*
@@ -59,7 +61,7 @@ class TextClassificationPipeline extends Pipeline
5961
{
6062
public function __invoke(array|string $inputs, ...$args): array
6163
{
62-
$topK = $args["topK"] ?? 1;
64+
$topK = array_pop_key($args, 'topK', 1);
6365

6466
$modelInputs = $this->tokenizer->tokenize($inputs, padding: true, truncation: true);
6567

src/Pipelines/ZeroShotObjectDetectionPipeline.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Codewithkyrian\Transformers\Pipelines;
77

88
use Codewithkyrian\Transformers\Models\Output\ObjectDetectionOutput;
9+
10+
use function Codewithkyrian\Transformers\Utils\array_pop_key;
911
use function Codewithkyrian\Transformers\Utils\getBoundingBox;
1012
use function Codewithkyrian\Transformers\Utils\prepareImages;
1113

@@ -51,9 +53,9 @@ class ZeroShotObjectDetectionPipeline extends Pipeline
5153
public function __invoke(array|string $inputs, ...$args): array
5254
{
5355
$candidateLabels = $args[0];
54-
$threshold = $args['threshold'] ?? 0.1;
55-
$topK = $args['topK'] ?? null;
56-
$percentage = $args['percentage'] ?? false;
56+
$threshold = array_pop_key($args, 'threshold', 0.1);
57+
$topK = array_pop_key($args, 'topK', -1);
58+
$percentage = array_pop_key($args, 'percentage', false);
5759

5860
$isBatched = is_array($inputs);
5961

src/Tensor/Tensor.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use Rindow\Math\Matrix\Complex;
1717
use Rindow\Math\Matrix\ComplexUtils;
1818
use Rindow\Math\Matrix\Drivers\Service;
19-
20-
//use Rindow\Math\Matrix\MatrixOperator;
2119
use Rindow\Math\Matrix\Range;
2220
use RuntimeException;
2321
use Serializable;
@@ -390,6 +388,16 @@ public static function fromString(string $string, int $dtype, array $shape): sta
390388
return new static($buffer, $dtype, $shape, 0);
391389
}
392390

391+
public static function random(array $shape, ?int $dtype = null): static
392+
{
393+
$dtype ??= NDArray::float32;
394+
$size = array_product($shape);
395+
396+
$buffer = Tensor::newBuffer($size, $dtype);
397+
$buffer->load(random_bytes($size * TensorBuffer::$valueSize[$dtype]));
398+
return new static($buffer, shape: $shape, offset: 0);
399+
}
400+
393401
/**
394402
* Convert the tensor into an array.
395403
*/
@@ -680,6 +688,15 @@ public function multiply(Tensor|float|int $value): self
680688
return new static($ndArray->buffer(), $ndArray->dtype(), $ndArray->shape(), $ndArray->offset());
681689
}
682690

691+
public function matmul(Tensor $other, ?bool $transposeA = null, ?bool $transposeB = null): Tensor
692+
{
693+
$mo = self::mo();
694+
695+
$result = $mo->la()->matmul($this, $other, $transposeA, $transposeB);
696+
697+
return new static($result->buffer(), $result->dtype(), $result->shape(), $result->offset());
698+
}
699+
683700
public function log(): self
684701
{
685702
$mo = self::mo();
@@ -942,7 +959,7 @@ public function to(int $dtype): static
942959
/**
943960
* Returns the mean value of each row of the tensor in the given axis.
944961
*/
945-
public function mean(?int $axis = null, bool $keepShape = false): static|float|int
962+
public function mean(?int $axis = null, bool $keepShape = false): static|float|int|Tensor
946963
{
947964
$mo = self::mo();
948965

@@ -1017,7 +1034,7 @@ public function stdMean(?int $axis = null, int $correction = 1, bool $keepShape
10171034
$num = floor($num / $size);
10181035
}
10191036

1020-
$result->buffer[$resultIndex] += pow($this->buffer[$i] - $mean->buffer()[$resultIndex], 2);
1037+
$result->buffer[$resultIndex] += pow($this->buffer[$i] - $mean->buffer[$resultIndex], 2);
10211038
}
10221039

10231040
for ($i = 0; $i < count($result->buffer); ++$i) {
@@ -1226,9 +1243,9 @@ protected function softmax2D(): static
12261243
*
12271244
* @return array The top k values and indices of the tensor.
12281245
*/
1229-
public function topk(int $k = null, bool $sorted = true): array
1246+
public function topk(int $k = -1, bool $sorted = true): array
12301247
{
1231-
if ($k === null) {
1248+
if ($k === -1) {
12321249
$k = $this->shape[0];
12331250
}
12341251

0 commit comments

Comments
 (0)