forked from CodeWithKyrian/transformers-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBertProcessing.php
More file actions
63 lines (50 loc) · 1.94 KB
/
BertProcessing.php
File metadata and controls
63 lines (50 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace Codewithkyrian\Transformers\PostProcessors;
/**
* A post-processor that adds special tokens to the beginning and end of the input.
*/
class BertProcessing extends PostProcessor
{
/**
* @var string The special token to add to the beginning of the input.
*/
protected string $cls;
/**
* @var string The special token to add to the end of the input.
*/
protected string $sep;
/**
* @param array $config
*/
public function __construct(array $config)
{
parent::__construct($config);
$this->cls = $config['cls'][0];
$this->sep = $config['sep'][0];
}
/**
* Adds the special tokens to the beginning and end of the input.
*
* @param string[] $tokens The input tokens.
* @param string[]|null $tokenPair The input tokens for the second sequence in a pair.
* @param bool $addSpecialTokens Whether to add the special tokens associated with the corresponding model.
* @return PostProcessedOutput
*/
public function postProcess(array $tokens, ?array $tokenPair = null, bool $addSpecialTokens = true): PostProcessedOutput
{
if ($addSpecialTokens) {
$tokens = array_merge([$this->cls], $tokens, [$this->sep]);
}
$tokenTypeIds = array_fill(0, count($tokens), 0);
if ($tokenPair) {
// NOTE: It is intended to add 2 EOS tokens after the first set of tokens
// https://github.com/huggingface/tokenizers/issues/983
$middle = $addSpecialTokens && $this instanceof RobertaProcessing ? [$this->sep] : [];
$after = $addSpecialTokens ? [$this->sep] : [];
$tokens = array_merge($tokens, $middle, $tokenPair, $after);
$tokenTypeIds = array_merge($tokenTypeIds, array_fill(0, count($middle) + count($tokenPair) + count($after), 1));
}
return new PostProcessedOutput($tokens, $tokenTypeIds);
}
}