Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Machine Learning Inference in PHP by example

Machine Learning Inference in PHP by example

This presentation discusses implementing machine learning inference in PHP using ONNX and Transformers. We introduce Transformer models, which revolutionized natural language processing since their introduction in 2017 through the "Attention is All You Need" paper.

Unlike previous sequential processing models, Transformers can process all input simultaneously, featuring encoder-decoder structure, self-attention layers, and position embeddings.
The presentation demonstrates three practical use cases in PHP:

Text Classification: Using the TransformersPHP package to analyze sentiment in product reviews, showcasing code for processing and scoring text input.
Image Classification: Implementing image recognition capabilities, with an example of a "hot dog" detection system using ONNX models.
LLM Generation: Demonstrating text generation using smaller language models, including integration with the Flan-T5 model.

The technical implementation relies on PHP's Foreign Function Interface (FFI), which allows PHP code to directly interact with C libraries without additional extensions. The presentation emphasizes ONNX (Open Neural Network Exchange) as a key component, enabling interoperability between different machine learning frameworks like PyTorch and TensorFlow.
A significant current limitation is the lack of GPU support, making the speaker recommend:

Using this approach for smaller tasks like classification and image tagging
Delegating larger LLM work to GPU-based infrastructure
Considering alternatives for LLM implementation:

Using SaaS services (OpenAI, Claude API)
Utilizing HuggingFace Inference endpoints
Building custom solutions (with associated risks)

The presentation also highlights SmolLM, a compact 538MB language model suitable for smaller applications, demonstrating the potential for efficient ML implementation in PHP environments.

Guillaume Moigneu

December 10, 2024
Tweet

Other Decks in Technology

Transcript

  1. What is a Transformer? ▪ A neural network architecture that

    revolutionized NLP and beyond ▪ Introduced in 2017 paper "Attention is All You Need" ▪ Powers models like GPT, BERT, and T5
  2. Key innovation: processes all input at once instead of sequentially

    ▪ Encoder-decoder structure ▪ Self-attention layers ▪ Feed-forward neural networks ▪ Position embeddings
  3. Before Sequential processing (RNNs, LSTMs) Limited context window Slow training

    and inference After Parallel processing & lowering cost of training & inference Extended context understanding Faster training and inference Better at capturing relationships Reduced resources usage 2017
  4. Demo: Text: Symfony is a great framework for developing [MASK]

    applications. Learn more: https://cdn.cs50.net/ai/2023/x/lectures/6/lecture6.pdf
  5. PHP FFI Foreign Function Interface FFI extension allows PHP code

    to directly call functions and manipulate data from C libraries without writing additional C code or PHP extensions.
  6. Open Neural Network Exchange ONNX for short ONNX (Open Neural

    Network Exchange) is an open standard format that allows AI models to be shared between different machine learning frameworks like PyTorch, TensorFlow, and many others.
  7. Transformers and pipelines Provided by the TransformersPHP package. Courtesy of

    Kyrian Obikwelu And 7 contributors Supported pipelines
  8. Models Infinite possibilities ▪ Hundreds of models readily available on

    🤗. ▪ Convert any model to ONNX with: + Python + Notebook
  9. SmolLM Promising efficient LLM model ▪ Provided by the HF

    team ▪ 538Mb only ▪ Great alternative to fully fledged models ▪ Can even run on the browser
  10. My recommendation While this is great for small tasks (classification,

    image tagging, etc.), any LLM work should be delegated to GPU based infrastructure.
  11. Your LLM options for now Use a SaaS service: OpenAI,

    Claude API, etc. Or use HF Inference endpoints to deploy the model of your choice And query that API endpoint from your app. Or “Build your own”, at your own risk!