PHP code example of syncfly / transformers-torch-php
1. Go to this page and download the library: Download syncfly/transformers-torch-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
syncfly / transformers-torch-php example snippets
py\transformers;
$generator = transformers::pipeline('text-generation', model: 'Qwen/Qwen2.5-1.5B-Instruct');
$messages = [['role' => 'user', 'content' => 'Why is PHP great? Answer in one short sentence.']];
$output = $generator($messages, max_new_tokens: 100);
echo end($output[0]['generated_text'])['content'];
// Outputs: "PHP is a versatile and widely-used programming language known for its ease of use, large community, and extensive library support."
use py\torch;
use py\transformers\AutoTokenizer;
use py\transformers\AutoModelForCausalLM;
$model_name = 'Qwen/Qwen3-1.7B';
$tokenizer = AutoTokenizer::from_pretrained($model_name);
$model = AutoModelForCausalLM::from_pretrained(
$model_name,
dtype: torch::$bfloat16,
device_map: 'auto'
);
$messages = [['role' => 'user', 'content' => 'Which is the most popular PHP framework? Give a short answer.']];
// BatchEncoding is a dict subclass, so it arrives as a PHP array of tensors
$inputs = $tokenizer->apply_chat_template(
$messages,
add_generation_prompt: true,
return_tensors: 'pt',
enable_thinking: false
);
$outputs = $model->generate(
input_ids: $inputs['input_ids']->to($model->device),
attention_mask: $inputs['attention_mask']->to($model->device),
max_new_tokens: 1024
);
$input_len = $inputs['input_ids']->shape[1];
$generated = array_slice($outputs[0]->tolist(), $input_len);
echo $tokenizer->decode($generated, skip_special_tokens: true);
use py\transformers;
$classifier = transformers::pipeline(
'text-classification',
model: 'distilbert/distilbert-base-uncased-finetuned-sst-2-english'
);
$result = $classifier('This library is amazing!');
echo $result[0]['label']; // POSITIVE
echo $result[0]['score']; // 0.9998
// Batching works out of the box
$results = $classifier(['I love PHP.', 'I hate bugs.']);
use py\transformers;
$unmasker = transformers::pipeline('fill-mask', model: 'google-bert/bert-base-uncased');
foreach ($unmasker('Paris is the [MASK] of France.', top_k: 3) as $candidate) {
echo "{$candidate['token_str']}: {$candidate['score']}\n";
}
use py\transformers;
$ner = transformers::pipeline('token-classification', model: 'dslim/bert-base-NER');
foreach ($ner('My name is Sarah and I live in London.') as $entity) {
echo "{$entity['word']} → {$entity['entity']}\n";
}
use py\torch;
use py\transformers\AutoTokenizer;
use py\transformers\AutoModelForQuestionAnswering;
$model_name = 'deepset/roberta-base-squad2';
$tokenizer = AutoTokenizer::from_pretrained($model_name);
$model = AutoModelForQuestionAnswering::from_pretrained($model_name);
$inputs = $tokenizer('What is my name?', 'My name is Clara and I live in Berkeley.', return_tensors: 'pt');
// ModelOutput is a dict subclass, so it arrives as a PHP array
$outputs = $model(input_ids: $inputs['input_ids'], attention_mask: $inputs['attention_mask']);
$start = torch::argmax($outputs['start_logits'])->item();
$end = torch::argmax($outputs['end_logits'])->item();
$ids = $inputs['input_ids'][0]->tolist();
$answer = $tokenizer->decode(array_slice($ids, $start, $end - $start + 1));
use py\transformers\AutoTokenizer;
use py\transformers\AutoModelForSeq2SeqLM;
$model_name = 'google/flan-t5-base';
$tokenizer = AutoTokenizer::from_pretrained($model_name);
$model = AutoModelForSeq2SeqLM::from_pretrained($model_name, device_map: 'auto');
$inputs = $tokenizer('Translate English to German: How old are you?', return_tensors: 'pt');
$outputs = $model->generate($inputs['input_ids']->to($model->device), max_new_tokens: 20);
echo $tokenizer->decode($outputs[0], skip_special_tokens: true); // "Wie old sind Sie?"
use py\transformers;
use py\PIL\Image;
$classifier = transformers::pipeline('image-classification', model: 'google/vit-base-patch16-224');
// Open a file — or build an image on the fly with PIL
$image = Image::open('cat.jpg');
foreach ($classifier($image) as $prediction) {
echo "{$prediction['label']}: {$prediction['score']}\n";
}
use py\torch;
$t = torch::tensor([[1, 2], [3, 4]]);
$t->tolist(); // [[1, 2], [3, 4]] — back to a PHP array
$t->shape[0]; // 2
$t->sum()->item(); // 10
$t[1][0]->item(); // 3 — Python indexing via PHP array syntax
count($t); // 2 — len() via count()
torch::arange(6)->reshape(2, 3)->t(); // transpose
torch::matmul($a, $b); // matrix multiplication
torch::tensor([1, 2], dtype: torch::$float32);
use py\torch;
$x = torch::tensor([3.5],
$x->grad->tolist(); // [7.0] — dy/dx = 2x
// Disable tracking for inference — the equivalent of `with torch.no_grad():`
$result = Py::with(torch::no_grad(), fn () => $model($input));
use py\torch;
use py\torch\nn\Linear;
use py\torch\nn\MSELoss;
use py\torch\optim\SGD;
// Learn y = 2x + 1
$x = torch::rand(32, 1);
$y = $x->mul(2)->add(1);
$model = new Linear(1, 1);
$criterion = new MSELoss();
$optimizer = new SGD($model->parameters(), lr: 0.1);
for ($epoch = 0; $epoch < 200; $epoch++) {
$optimizer->zero_grad();
$loss = $criterion($model($x), $y);
$loss->backward();
$optimizer->step();
}
echo $model->weight->item(); // ≈ 2.0
echo $model->bias->item(); // ≈ 1.0
use py\safetensors\torch as safetensors;
safetensors::save_file(['weight' => $model->weight, 'bias' => $model->bias], 'model.safetensors');
$tensors = safetensors::load_file('model.safetensors');