GithubHelp home page GithubHelp logo

rubixml / tensor Goto Github PK

View Code? Open in Web Editor NEW
216.0 11.0 27.0 2.01 MB

A library and extension that provides objects for scientific computing in PHP.

Home Page: https://rubixml.com

License: MIT License

PHP 43.76% M4 0.32% C 36.70% Shell 0.10% Zephir 19.12% Batchfile 0.01%
linear-algebra matrix vector matrix-decompositions math tensor php scientific-computing php-extension matrix-multiplication

tensor's Introduction

Tensor: Scientific Computing for PHP

PHP from Packagist Latest Stable Version Code Checks Extension Build Downloads from Packagist GitHub

A library and extension that provides objects for scientific computing in PHP.

Installation

Follow the instructions below to install either Tensor PHP or the Tensor extension.

Tensor PHP

Install Tensor PHP into your project with Composer:

$ composer require rubix/tensor

Tensor Extension

Install the Tensor extension via PECL:

$ pecl install tensor

Note: If both the library and extension are installed, the extension will take precedence.

Requirements

  • PHP 7.4 or above

Optional To Compile Extension

Manually Compiling the Extension

Clone the repository locally using Git:

$ git clone https://github.com/RubixML/Tensor

Make sure you have all the necessary build tools installed such as a C compiler and make tools. For example, on an Ubuntu linux system you can enter the following on the command line to install the necessary dependencies.

$ sudo apt-get install make gcc gfortran php-dev libopenblas-dev liblapacke-dev re2c build-essential

Then, change into the ext directory from the project root and run the following commands from the terminal. See this guide for more information on compiling PHP extensions with PHPize.

$ cd ./ext
$ phpize
$ ./configure
$ make
$ sudo make install

Finally, add the following line to your php.ini configuration to install the extension.

extension=tensor.so

To confirm that the extension is loaded in PHP, you can run the following command.

php -m | grep tensor

Performance Comparison

Tensor Performance MNIST

Tensor Performance Benchmarks

Contributing

See CONTRIBUTING.md for guidelines.

License

The code is licensed MIT and the documentation is licensed CC BY-NC 4.0.

tensor's People

Contributors

27pchrisl avatar andrewdalpino avatar aweptimum avatar cmb69 avatar mlocati avatar villfa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tensor's Issues

Rubix/ML TruncatedSVD() made PHP crash without any message

This is a copy issue from Rubix/ML RubixML/ML#303
I'm putting this here because I don't know whether this problem is related to Rubix/ML or Rubix/tensor

hello, I try to create a chatbot using LSA Algorithm.
I am new to Machine learning, so I don't know if this is correct.

since I use the LSA algorithm, so I need to call TruncatedSVD()
but this script made PHP dev server crash without any message.
when I remove it from Pipeline everything is working

my machine is
Debian 12
AMD A9
RAM 4GB
PHP 8.2.7
Laravel 9

I have installed tensor extension manually using this fork/PR #36 (since PHP 8.2 is not fully supported yet)

here is my code

<?php
namespace App;
use Illuminate\Support\Facades\Storage;
use Rubix\ML\Classifiers\KDNeighbors;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Graph\Trees\BallTree;
use Rubix\ML\Kernels\Distance\Cosine;
use Rubix\ML\Loggers\Screen;
use Rubix\ML\PersistentModel;
use Rubix\ML\Pipeline;
use Rubix\ML\Transformers\TextNormalizer;
use Rubix\ML\Transformers\TfIdfTransformer;
use Rubix\ML\Transformers\TruncatedSVD;
use Rubix\ML\Transformers\WordCountVectorizer;
use Rubix\ML\Persisters\Filesystem;

class Chatbot
{
    private $estimator;
    private $persister;
    private $logger;

    public function __construct() {
        $this->logger = new Screen();
        $modelPath = Storage::path('dataset/lsa.model');
        $this->persister = new Filesystem($modelPath);
        if(is_file($modelPath)){
            $this->estimator = $this->persister->load();
            $this->logger->info('model loaded');
        } else {
            $this->train();
        }
    }
    function train() {
        $start = microtime(true);
        $this->logger->info('start load dataset');
        $samples =[
            ['Cara mengganti password', 'Bagaimana cara mengganti kata sandi wifi', 'cara ganti sandi'],
            ['cara ganti nama wifi', 'bagaimana cara mengganti nama wifi', 'gimana caranya ganti SSID'],
            ['cara bayar tagihan', 'cara membayar wifi', 'nomor rekening pembayaran'],
            ['wifi tidak stabil', 'wifi kadang terkoneksi kadang tidak', 'internet bermasalah'],
            ['indikator warna merah hidup', 'indikator los hidup', 'ada lampu warna merah menyala'],
            ['internet lemot', 'jaringan lambat', 'cara upgrade paket']
        ];

        $labels = [
            "Untuk mengganti password, atau kata sandi caranya adalah dengan masuk Google Chrome lalu Ketik 192.168.0.1 untuk Username sama password untuk login adalah user setelah masuk Klik menu Network, Klik bagian Wlan, Klik bagian Security lalu Setelah diganti klik submit",
            "Untuk mengganti nama WiFi caranya adalah dengan masuk Google Chrome lalu Ketik 192.168.0.1 untuk Username sama password untuk login adalah user setelah masuk Klik menu Network Klik bagian Wlan Klik bagian SSID Setting lalu Setelah diganti klik submit",
            "untuk pembayaran wi-fi bisa melalui transfer dengan no rekening MANDIRI : 185000416082",
            "jika internet wifi lambat atau lemot, silakan coba dikurangi jumlah penggunanya agar pemakaianya stabil dan lancar, atau upgrade pake wifi",
            "apabila indikator LOS berwarna merah menyala, kemungkinan terdapat permasalahan pada device atau terdapat kabel yang terputus sehingga diperlukan pengecekan langsung di lokasi Silakan hubungi admin melalui layanan Pengaduan Masalah.",
            "jika internet wifi lambat atau lemot, silakan coba dikurangi jumlah penggunanya agar pemakaianya stabil dan lancar, atau upgrade pake wifi",
        ];

        $dataset = new Labeled($samples, $labels);
        $nearest = 5;

        $this->estimator = new PersistentModel(
            new Pipeline([
                new TextNormalizer(),
                new WordCountVectorizer(1000,1, 0.8),
                new TfIdfTransformer(),
                new TruncatedSVD(10), //crash. but when I remove this, everything is working.
            ], new KDNeighbors($nearest,true,new BallTree($nearest, new Cosine()))),
            $this->persister
        );

        $this->estimator->train($dataset);

        $this->estimator->save();

        dump((memory_get_peak_usage()/1024/1024)." MB\n");
        $time = microtime(true) - $start;
        dump("Time: ".$time." s\n");
    }

}

Ext compilation errors and warning - PHP 8.1

Compiling the powerfull ext using phpize, configure and make throw several warnings, concerning zend ptrs.

Nevertheless the lib seems to works, but throws this warning at each php bootstraps

PHP Deprecated:  Return type of Tensor\Vector::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in Unknown on line 0

PHP Deprecated:  Return type of Tensor\Vector::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in Unknown on line 0

PHP Deprecated:  Return type of Tensor\Matrix::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in Unknown on line 0

Compiled on a Mac Mini, 10.15.7, all needed libs installed with Brew, php 8.1.10

Regards 🙂

Docker php:7.4-fpm-alpine pecl install and build from source fails

Versions according to https://pkgs.alpinelinux.org/packages

.Dockerfile for pecl install tensor

FROM php:7.4-fpm-alpine

RUN apk update && apk --no-cache add --update \
    # Optional To Compile Extension
    # A C compiler v12.1.1_git20220630-r5
    autoconf gcc musl-dev g++ \
    # A Fortran compiler v12.1.1_git20220630-r5
    bash bash-doc bash-completion musl-dev gfortran gdb make \
    # OpenBLAS development package v0.3.21-r0
    openblas-dev \
    # LAPACKE C interface to LAPACK v3.10.1-r0
    lapack \
    # re2c v3.0-r0
    re2c \
    # make v4.3-r0
    make \
    # autoconf v2.71-r0
    autoconf \
    # automake v1.16.5-r1
    automake \
    # build-essentials for Alpine 
    alpine-sdk build-base
    # execinfo.h missing error: 
    # libexecinfo-dev

RUN pecl install tensor

Error from pecl install tensor:

#0 10.20 /tmp/pear/temp/tensor/ext/kernel/backtrace.c:15:10: fatal error: execinfo.h: No such file or directory
#0 10.20    15 | #include <execinfo.h>
#0 10.20       |          ^~~~~~~~~~~~
#0 10.20 compilation terminated.
#0 10.20 make: *** [Makefile:202: kernel/backtrace.lo] Error 1
#0 10.20 ERROR: `make' failed

Fixed by apk add libexecinfo-dev awslabs/aws-lambda-cpp#124 (comment)

But then other error pops up:

#0 85.60 /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -llapacke: No such file or directory
#0 85.61 collect2: error: ld returned 1 exit status
#0 85.61 make: *** [Makefile:289: tensor.la] Error 1
#0 85.62 ERROR: `make' failed

musl/bin/ld file is there.

bash-5.1# ls /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/bin/ -l
total 4936
-rwxr-xr-x    2 root     root         59536 Jul  7 13:57 ar
-rwxr-xr-x    2 root     root        659448 Jul  7 13:57 as
-rwxr-xr-x    4 root     root       1469904 Jul  7 13:57 ld
-rwxr-xr-x    4 root     root       1469904 Jul  7 13:57 ld.bfd
-rwxr-xr-x    2 root     root         52392 Jul  7 13:57 nm
-rwxr-xr-x    2 root     root        157808 Jul  7 13:57 objcopy
-rwxr-xr-x    2 root     root        332720 Jul  7 13:57 objdump
-rwxr-xr-x    2 root     root         59568 Jul  7 13:57 ranlib
-rwxr-xr-x    2 root     root        620928 Jul  7 13:57 readelf
-rwxr-xr-x    2 root     root        157808 Jul  7 13:57 strip

Also tried building from source - same error on make step pops up.

.Dockerfile for build tensor from source

RUN apk update && apk --no-cache add --update \
    # A C compiler v12.1.1_git20220630-r5
    autoconf gcc musl-dev g++ cmake \
    # A Fortran compiler v12.1.1_git20220630-r5
    bash bash-doc bash-completion musl-dev gfortran gdb make \
    # OpenBLAS development package v0.3.21-r0
    openblas-dev \
    # LAPACKE C interface to LAPACK v3.10.1-r0
    lapack \
    # re2c v3.0-r0
    re2c \
    # make v4.3-r0
    make \
    # autoconf v2.71-r0
    autoconf \
    # automake v1.16.5-r1
    automake \
    # build-essentials for Alpine 
    alpine-sdk build-base \
    # execinfo.h missing: https://github.com/awslabs/aws-lambda-cpp/issues/124#issuecomment-968302646
    libexecinfo-dev

# RUN pecl install tensor

RUN git clone https://github.com/RubixML/Tensor Tensor

WORKDIR /var/www/html/Tensor/ext

RUN phpize \
    && ./configure \
    && make \
    && make install

Error from build tensor from source:

#0 86.23 /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -llapacke: No such file or directory
#0 86.27 collect2: error: ld returned 1 exit status
#0 86.28 make: *** [Makefile:289: tensor.la] Error 1

What am I missing here?

Warnings when using Tensor on PHP 8.1

When using the tensor PHP extension with PHP 8.1 we have these PHP warnings:


Deprecated: Return type of Tensor\Vector::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in Unknown on line 0

Deprecated: Return type of Tensor\Vector::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in Unknown on line 0

Deprecated: Return type of Tensor\Matrix::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in Unknown on line 0

a simple fix is to simply add this line right before these methods:

#[\ReturnTypeWillChange]

See https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.core.type-compatibility-internal

Singular Value Decomposition (SVD)

Singular Value Decomposition (SVD) is used in a few machine learning methods such as Latent Semantic Analysis (LSA), collaborative filtering recommender systems, and more. It generalizes the eigendecomposition of a square normal matrix to any m x n matrix via an extension of the polar decomposition. This ticket is to research the best SVD algorithm (or set of algorithms) to implement that covers the two aforementioned use cases. Note that we'll need both PHP and Zephir/C implementations of SVD, however, for the prototype it would be perfectly fine to focus on one (PHP) or the other (Zephir/C).

https://en.wikipedia.org/wiki/Singular_value_decomposition

https://en.wikipedia.org/wiki/Matrix_factorization_(recommender_systems)

https://en.wikipedia.org/wiki/Latent_semantic_analysis

https://numpy.org/devdocs/reference/generated/numpy.linalg.svd.html

Research paths to 20X and beyond

We have decided to direct some efforts toward researching a new Tensor 3.0 extension with the API code written in Zephir and optimized code written in C with the goal of drastically accelerating tensor operations. The Tensor 2.0 extension gave us a 2.5X speedup over the PHP implementation, this research is to develop a set of minimum features necessary to achieve at least a 10X speedup. Possible ways to achieve this are BLAS library integration, cache-efficient blocking algorithms, a fixed or strided array implementation, and vectorized operations. Optional but desirable enhancements to the extension include n-d array operations and CPU and GPU parallelization. In addition, incremental backwards compatible optimizations can be applied to the 2.0.0 branch as part of this research.

With a 10X+ speedup over the PHPland code we should be able to implement more advanced deep learning models including LSTMs and potentially Convnets (if we figure out a sensible n-d array implementation).

Other things to consider ...

  • Will it be easier to reverse-engineer the Zephir-generated code or write it from scratch?
  • Will it be easier/better to interface to TensorFlow under the hood?

Resources

Instabilities in Tensor\Matrix->multiply

I installed version 3.0.4 of both the composer package (for autocompletion) and the PHP extension (using php-extension-installer) inside of a docker environment.

I verified it was enabled correctly using php -i, and then I did a basic multiplication test:

$A = Tensor\Matrix::quick([
  [1,2,3],
  [1,2,3],
  [1,2,3],
]);

$b = Tensor\Vector::quick([1,2,4]);
print_r($A->multiply($b))

What I got back was not the right answer:

[
  [1, 12, 0],
  [1, 12, 0],
  [1, 12, 0],
]

It should be:

[
  [1, 4, 12],
  [1, 4, 12],
  [1, 4, 12],
]

The first column is fine, however, the last column is shifted over one and the second column (of 4's) is just missing.

Failed to install PHP extension in PHP Docker images with pecl

I can't install tensor in a PHP 7.2 Alpine image with pecl.

Steps to reproduce inside the docker image (docker run --rm -it php:7.2-cli-alpine sh)

apk update
apk add $PHPIZE_DEPS lapack-dev libexecinfo-dev openblas-dev
pecl install tensor

Here's the last few lines of the output:

 cc -O3 -ffast-math -I. -I/tmp/pear/temp/tensor/ext -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/include -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/main -I/tmp/pear/temp/tensor/ext -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/temp/tensor/ext/tensor/tensor.zep.c  -fPIC -DPIC -o tensor/.libs/tensor.o
/bin/sh /tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/libtool --mode=compile cc -O3 -ffast-math -I. -I/tmp/pear/temp/tensor/ext -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/include -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/main -I/tmp/pear/temp/tensor/ext -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /tmp/pear/temp/tensor/ext/tensor/vector.zep.c -o tensor/vector.lo
 cc -O3 -ffast-math -I. -I/tmp/pear/temp/tensor/ext -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/include -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/main -I/tmp/pear/temp/tensor/ext -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/temp/tensor/ext/tensor/vector.zep.c  -fPIC -DPIC -o tensor/.libs/vector.o
/bin/sh /tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/libtool --mode=compile cc -O3 -ffast-math -I. -I/tmp/pear/temp/tensor/ext -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/include -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/main -I/tmp/pear/temp/tensor/ext -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /tmp/pear/temp/tensor/ext/tensor/columnvector.zep.c -o tensor/columnvector.lo
 cc -O3 -ffast-math -I. -I/tmp/pear/temp/tensor/ext -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/include -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/main -I/tmp/pear/temp/tensor/ext -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/temp/tensor/ext/tensor/columnvector.zep.c  -fPIC -DPIC -o tensor/.libs/columnvector.o
/bin/sh /tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/libtool --mode=compile cc -O3 -ffast-math -I. -I/tmp/pear/temp/tensor/ext -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/include -I/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/main -I/tmp/pear/temp/tensor/ext -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /tmp/pear/temp/tensor/ext/tensor/decompositions/cholesky.zep.c -o tensor/decompositions/cholesky.lo
/tmp/pear/temp/pear-build-defaultuserDNKfcM/tensor-2.1.4/libtool: line 1283: can't create tensor/decompositions/cholesky.loT: nonexistent directory
mkdir tensor/decompositions/.libs
mkdir: can't create directory 'tensor/decompositions/.libs': No such file or directory
make: *** [Makefile:252: tensor/decompositions/cholesky.lo] Error 1
ERROR: `make' failed

Tensor 3.0.5 ( multiply strange behavior)

My concerns starts with tensor 3.0.5, I talked about it again and my example is T-SNE.
PHP 8.2.17 (x64)... tensor 3.0.5...
When the tensor 3.0.5 is enabled, then the results for the tsne.php and specific at line 332 of page code

$p = Matrix::quick($this->affinities($distances))
->multiply($this->exaggeration);

produce all zeros for $p

The example is with dataset iris-flower

Tensor Enabled - results
Tensor\Matrix Object
(
[a:protected] => Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
......

But if i disabled Tensor 3.0.5

then the results are as this ...

Tensor Disabled - results
Tensor\Matrix Object
(
[a:protected] => Array
(
[0] => Array
(
[0] => 0
[1] => 0.21491825237444
[2] => 0.22478852033193
[3] => 0.18097129837837
[4] => 0.40076888328063
[5] => 0.19018111784135
[6] => 0.22138822172434
[7] => 0.38127063420594
[8] => 0.11774974442454
........

the: $this->affinities($distances) has values
the: $this->exaggeration has values
but when put them together with tensor enabled, the results are all zero.

Getting lapacke.h: No such file or directory doing manual build on centOS

here is the error i'm getting:
/bin/sh /home/trendalix1/Tensor/ext/libtool --mode=compile cc -O3 -ffast-math -I. -I/home/trendalix1/Tensor/ext -DPHP_ATOM_INC -I/home/trendalix1/Tensor/ext/include -I/home/trendalix1/Tensor/ext/main -I/home/trendalix1/Tensor/ext -I/opt/cpanel/ea-php74/root/usr/include/php -I/opt/cpanel/ea-php74/root/usr/include/php/main -I/opt/cpanel/ea-php74/root/usr/include/php/TSRM -I/opt/cpanel/ea-php74/root/usr/include/php/Zend -I/opt/cpanel/ea-php74/root/usr/include/php/ext -I/opt/cpanel/ea-php74/root/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /home/trendalix1/Tensor/ext/include/linear_algebra.c -o include/linear_algebra.lo
libtool: compile: cc -O3 -ffast-math -I. -I/home/trendalix1/Tensor/ext -DPHP_ATOM_INC -I/home/trendalix1/Tensor/ext/include -I/home/trendalix1/Tensor/ext/main -I/home/trendalix1/Tensor/ext -I/opt/cpanel/ea-php74/root/usr/include/php -I/opt/cpanel/ea-php74/root/usr/include/php/main -I/opt/cpanel/ea-php74/root/usr/include/php/TSRM -I/opt/cpanel/ea-php74/root/usr/include/php/Zend -I/opt/cpanel/ea-php74/root/usr/include/php/ext -I/opt/cpanel/ea-php74/root/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /home/trendalix1/Tensor/ext/include/linear_algebra.c -fPIC -DPIC -o include/.libs/linear_algebra.o
/home/trendalix1/Tensor/ext/include/linear_algebra.c:7:21: fatal error: lapacke.h: No such file or directory
#include <lapacke.h>
^
compilation terminated.
make: *** [include/linear_algebra.lo] Error 1

I can see that i have the lapacke.h files on the server with : find / -name lapacke.h
which returns:

/usr/include/openblas/lapacke.h
/usr/include/lapacke/lapacke.h

I also tried adding the include paths so it would find it with:

export PATH="$HOME/usr/include:$PATH"
export PATH="$HOME/usr/include/openblas:$PATH"
export PATH="$HOME/usr/include/lapacke:$PATH"
export PATH="$HOME/include:$PATH"

but it doesn't seem to find the file. Sorry if this is something obvious, i don't spend much time in the command line.

Thanks, Mark

make fails on Ubuntu 20, PHP 8.2

I have carefully followed the instructions for manually compiling the Tensor extension. make failed with output ending in the following errors:

/home/sneakyimp/biz/machine-learning/tensor/Tensor/ext/kernel/main.c: In function ‘zephir_function_exists’:
/home/sneakyimp/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:285:101: warning: comparison between pointer and integer
  285 |  if (zend_hash_str_exists(CG(function_table), Z_STRVAL_P(function_name), Z_STRLEN_P(function_name)) != NULL) {
      |                                                                                                     ^~
/home/sneakyimp/biz/machine-learning/tensor/Tensor/ext/kernel/main.c: In function ‘zephir_function_exists_ex’:
/home/sneakyimp/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:301:76: warning: comparison between pointer and integer
  301 |  if (zend_hash_str_exists(CG(function_table), function_name, function_len) != NULL) {
      |                                                                            ^~
/home/sneakyimp/biz/machine-learning/tensor/Tensor/ext/kernel/main.c: In function ‘zephir_get_arg’:
/home/sneakyimp/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:571:9: error: too many arguments to function ‘zend_forbid_dynamic_call’
  571 |     if (zend_forbid_dynamic_call("func_get_arg()") == FAILURE) {
      |         ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/php/20220829/main/php.h:35,
                 from /home/sneakyimp/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:16:
/usr/include/php/20220829/Zend/zend_API.h:782:39: note: declared here
  782 | static zend_always_inline zend_result zend_forbid_dynamic_call(void)
      |                                       ^~~~~~~~~~~~~~~~~~~~~~~~
make: *** [Makefile:213: kernel/main.lo] Error 1

I have also attempted to build the commit described by myfluxi in issue #33 and the make/compile succeeds but the resulting shared library has very strange behavior, segfaulting when certain comments are in place, and producing incorrect dimensions for a matrix multiplication operation.

SVD not implemented in polyfill

I tried use this library with laravel,
there is no issue when installation via composer composer require rubix/tensor.
but when I try call TruncatedSVD() (which is require this lib)
it show error "The tensor extension is not installed, check PHP configuration"

my machine is
Windows 10
PHP 8.1.7 x64 (laragon)

I haven't tried using pear/pecl because I have problem in installation that tool.

How to install Tensor extension dependencies on Linux

I got three errors when trying to install the tensor package using pecl.
Error 1:
/tmp/pear/temp/tensor/ext/tensor.c:25:10: fatal error: cblas.h: No such file or directory
Error 2:
/tmp/pear/temp/tensor/ext/include/linear_algebra.c:7:10: fatal error: lapacke.h: No such file or directory
Error 3:

/usr/bin/ld: cannot find -lgfortran
collect2: error: ld returned 1 exit status

The solution is to install libopenblas, libraries and gfortran.

apt-get install libopenblas-dev
apt-get install liblapacke-dev
apt-get install gfortran

Broken build with GCC 14 [-Wincompatible-pointer-types]

This was a warning
This is now a error using GCC 14 (on Fedora 40)

/builddir/build/BUILD/php82-php-pecl-tensor-3.0.4/tensor-3.0.4/ext/kernel/require.c: In function 'zephir_require_ret':
/builddir/build/BUILD/php82-php-pecl-tensor-3.0.4/tensor-3.0.4/ext/kernel/require.c:54:19: error: passing argument 1 of 'zval_ptr_dtor' from incompatible pointer type [-Wincompatible-pointer-types]
   54 |     zval_ptr_dtor(zend_string_path);
      |                   ^~~~~~~~~~~~~~~~
      |                   |
      |                   zend_string * {aka struct _zend_string *}
In file included from /opt/remi/php82/root/usr/include/php/Zend/zend.h:36,
                 from /opt/remi/php82/root/usr/include/php/main/php.h:31,
                 from /builddir/build/BUILD/php82-php-pecl-tensor-3.0.4/tensor-3.0.4/ext/kernel/require.c:16:
/opt/remi/php82/root/usr/include/php/Zend/zend_variables.h:79:35: note: expected 'zval *' {aka 'struct _zval_struct *'} but argument is of type 'zend_string *' {aka 'struct _zend_string *'}
   79 | ZEND_API void zval_ptr_dtor(zval *zval_ptr);
      |                             ~~~~~~^~~~~~~~
/builddir/build/BUILD/php82-php-pecl-tensor-3.0.4/tensor-3.0.4/ext/kernel/require.c: In function 'zephir_require_once_ret':
/builddir/build/BUILD/php82-php-pecl-tensor-3.0.4/tensor-3.0.4/ext/kernel/require.c:127:19: error: passing argument 1 of 'zval_ptr_dtor' from incompatible pointer type [-Wincompatible-pointer-types]
  127 |     zval_ptr_dtor(zend_string_path);
      |                   ^~~~~~~~~~~~~~~~
      |                   |
      |                   zend_string * {aka struct _zend_string *}
/opt/remi/php82/root/usr/include/php/Zend/zend_variables.h:79:35: note: expected 'zval *' {aka 'struct _zval_struct *'} but argument is of type 'zend_string *' {aka 'struct _zend_string *'}
   79 | ZEND_API void zval_ptr_dtor(zval *zval_ptr);
      |                             ~~~~~~^~~~~~~~

Question: Future of RubixML/DL

Hello,
How is the research on RubixML/DL#1 going?
Whole repository seems to be archived few years ago.. Will there ever be a GPU support on PHP with Tensor extension?
Or is there already an alternative to it?

Guys ar Tensorflow, according to https://discuss.tensorflow.org/t/tensorflow-for-php/1373 are not planning to implement it as independant wrapper. Instead they are advising to run TensorFlow Serving server for computing and interact with it through REST API

Would be glad to hear your thoughts on what is current globally best approach to utilize GPU while using PHP. Can't find much info on internet aswell, most posts are 4years old..

Multi-threading using RubixML/Tensor is a great oportunity, but CPU performance still can't compare to GPU, especially with specialised matrix multiplication cores from nvidia RTX.

Eigenvalues and vectors from JAMA implementation are wrong

After some failed tests with CRubix interface I realized the expected eigenvalues and vectors from MatrixTest are actually wrong compared to other libraries. The following matrix was used:

[
      [22, -17, 12],
      [4, 11, -2],
      [20, -6, -9],
]

Tensor with JAMA

$values = [25.108706520450326, -15.096331148319537, 13.9876246278692];
$vectors = [
            [-0.5029346679560592, -0.1309992382037118, -0.33107976181279675],
            [0.15580805853732102, -0.08643645234319261, -0.6918777439682378],
            [0.8501650243704214, 0.987607178637524, 0.641631809310763],
        ];

Tensor with CArray

$values = [-15.09633115,  25.10870652,  13.98762463];
$vectors = [[  0.25848695, -0.86227193, -0.66844722 ]
            [ -0.11314538, -0.17721180, -0.61268791 ]
            [ -0.95936574,  -0.47442924, -0.42165370 ]];

Computed using LAPACK_dgeev

NumPy

values = [-15.09633115,  25.10870652,  13.98762463]
vectors = [[ 0.25848695, -0.86227193, -0.66844722],
           [-0.11314538, -0.1772118 , -0.61268791],
           [-0.95936574, -0.47442924, -0.4216537 ]]

Computed using LAPACK_dgeev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.