Skip to content

Commit

Permalink
bugfix: Preparation of position ids not getting the tensor shape and …
Browse files Browse the repository at this point in the history
…buffer
  • Loading branch information
CodeWithKyrian committed Apr 2, 2024
1 parent a0d86e0 commit ae63823
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Models/Pretrained/PretrainedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,28 +475,27 @@ public function preparePositionIds(array $inputNames, array &$feeds, bool $useCa
}

// TODO: Verify this works properly!!!
$data = array_fill(0, count($feeds['attention_mask']), 0);
$data = array_fill(0, count($feeds['attention_mask']->buffer()), 0);

// Compute cumulative sum of the attention mask along the sequence length dimension
for ($i = 0; $i < $feeds['attention_mask']['dims'][0]; ++$i) {
$start = $i * $feeds['attention_mask']['dims'][1];
for ($i = 0; $i < $feeds['attention_mask']->shape()[0]; ++$i) {
$start = $i * $feeds['attention_mask']->shape()[1];
$sum = 0;
for ($j = 0; $j < $feeds['attention_mask']['dims'][1]; ++$j) {
for ($j = 0; $j < $feeds['attention_mask']->shape()[1]; ++$j) {
$index = $start + $j;
if ($feeds['attention_mask']['data'][$index] === 0) {
if ($feeds['attention_mask']->buffer()[$index] === 0) {
$data[$index] = 1;
} else { // === 1
$data[$index] = $sum;
$sum += $feeds['attention_mask']['data'][$index];
$sum += $feeds['attention_mask']->buffer()[$index];
}
}
}

$feeds['position_ids'] = new Tensor($data, shape: $feeds['attention_mask']->shape());

if ($useCacheBranch) {
// TODO: Fix this
// $feeds['position_ids'] = $feeds['position_ids']->slice(null, -1)->unsqueeze_(-1);
$feeds['position_ids'] = $feeds['position_ids']->slice(null, -1)->unsqueeze(-1);
}
}

Expand Down

0 comments on commit ae63823

Please sign in to comment.