Skip to content

Commit

Permalink
[FEAT] Implement benchmark for cpu and gpu for initializer methods Cl…
Browse files Browse the repository at this point in the history
…oses #57 (#65)

* CPU Benchmark files for initializers

Added benchmark files for CPU initializers using phpBench

* New methods and features for 0.6.0 (#64)

* feat: squeeze manipulation routine with axis support

* feat: NDArray::full implementation

* feat: NDArray::fill release

* feat: argmin and argmax with axis support

* feat: NDArray::positive implementation

* feat: NDArray::reciprocal implementation

* feat: NDArray::reciprocal stubs

* feat: NDArray::reciprocal stubs

* feat: NDArray::swapaxes implementation
feat: NDArray::transpose permutation

* feat: NDArray::swapaxes and new transpose stubs

* feat: NDArray::argmin and argmax keepdims option

* feat: NDArray::rollaxis implementation

* feat: NDArray::moveaxis implementation

* fix: improvements to the slicing mechanism

* feat: ndarray slicing stubs

* fix: fixed slicing shape when arrays contain values

* feat: hstack, vstack and dstack implementation

* feat: column_stack implementation

* feat: column_stack PHP stubs

* feat: NDArray::concatenate implementation

* feat: NDArray::append implementation

* feat: NDArray::append stubs

* docs: update README.md

* fix: NDArray::append memory leak when using PHP arrays as arguments.

* fix: new routines GPU implementation

* Added Param Providers

+ Added parameter providers for readability and coding best practices

- removed repetitive code

* Added default phpbench file

+ Added default PHPBench configuration file

* Update composer.json

* Update initializers.c

Fixed comment for NDArray ones

* Fixed setUp for benchmarking

Refactored function names for clarity, and fixed phpBench setup method

* Added benchmarks for the linear algebra methods

Added benchmarks for Linear Algebra methods with the exception of functions Cond, Inv, and Qr

* Added benchmarks for sum and products methods

Added benchmarks for sum and products methods

* Added benchmarks for the arithmetic methods

Added benchmarks for the arithmetic methods
- removed initial testArray values

* Added default values for aggregate benchmarking

Added default Revs and iteration values for more accurate runtime time estimates.

* Added Benchmarks for QR decomp, Condi, and Inverse

- remove extra comments on Cholesky benchmark file
- removed extra param comment in linalg file
+ Benchmark for QR decomposition function
+ Benchmark for Inverse function
+ Benchmark for Cond function

* Added annotations and ignore configure~ file

+ Updated .gitignore to ignore generated configure file
+ Added MIT license under composer file

* Added Group annotations

Added Group annotations to all benchmarks.

* Update readability of ArrayInitializerBench

Updated Array Initializer Benchmark to utilize zeros Initializer for setup instead of hard coded arrays

* Temp files in .gitignore

Removed temp files from .gitignore

---------

Co-authored-by: Henrique Borba <[email protected]>
  • Loading branch information
jiyo4476 and henrique-borba authored Sep 30, 2024
1 parent 0e20c24 commit d6ed83b
Show file tree
Hide file tree
Showing 56 changed files with 3,001 additions and 171 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ test.php
*.IoT
*.loT
tensor
test_img.jpg
test_img.jpg
composer.lock
vendor
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,23 @@ NumPower aims to manage memory more efficiently than a matrix in PHP arrays
- **Optional (GPU)**: CUBLAS, CUDA Build Toolkit and cuDNN
- **Optional (Image)**: PHP-GD

## Composer Install
## Compiling

```
$ phpize
$ ./configure
$ make install
```

## Compiling with GPU (CUDA) support

```
$ phpize
$ ./configure --with-cuda
$ make install-cuda
```

## Composer install
The composer package provides a stubs file to facilitate autocomplete in the IDE. To install, simply run the command below in your environment with composer installed:

```bash
Expand All @@ -42,7 +58,7 @@ $ composer require numpower/numpower

The composer package will follow the same versioning as the extension.

## GPU Support
## GPU support

If you have an NVIDIA graphics card with CUDA support, you can use your graphics card
to perform operations. To do this, just copy your array to the GPU memory.
Expand Down
35 changes: 35 additions & 0 deletions benchmarks/initializers/ArangeInitializerBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* @Groups({"initializers"})
*/
class ArangeInitializerBench
{
/**
* @var size
*/
private $size = 1;

public function setUp(array $params): void
{
$this->size = $params['size'];
}

/**
* @BeforeMethods("setUp")
* @Revs(1000)
* @Iterations(5)
* @ParamProviders({
* "provideSizes",
* })
*/
public function benchARange($params)
{
$ndarray = \NDArray::arange($this->size, 0, 1);
}
public function provideSizes() {
yield ['size' => 100];
yield ['size' => 500];
yield ['size' => 1000];
}
}
?>
39 changes: 39 additions & 0 deletions benchmarks/initializers/ArrayInitializerBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @Groups({"initializers"})
*/
class ArrayInitializerBench
{
/**
* @var matrix
*/
private $matrix = [];

public function setUp(array $params): void
{
$this->matrix = $params['matrix'];
}

/**
* @BeforeMethods("setUp")
* @Revs(1000)
* @Iterations(5)
* @ParamProviders({
* "provideMatrix",
* })
*/
public function benchArray($params)
{
$ndarray = \NDArray::array($this->matrix);
}

public function provideMatrix() {
yield ['matrix' => \NDArray::zeros([1, 100])];
yield ['matrix' => \NDArray::zeros([1, 500])];
yield ['matrix' => \NDArray::zeros([1, 1000])];
yield ['matrix' => \NDArray::zeros([10, 100])];
yield ['matrix' => \NDArray::zeros([1000, 500])];
yield ['matrix' => \NDArray::zeros([10000, 1000])];
}
}
?>
39 changes: 39 additions & 0 deletions benchmarks/initializers/FullInitializerBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @Groups({"initializers"})
*/
class FullInitializerBench
{
/**
* @var shape
*/
private $shape = [];

public function setUp(array $params): void
{
$this->shape = $params['shape'];
}

/**
* @BeforeMethods("setUp")
* @Revs(1000)
* @Iterations(5)
* @ParamProviders({
* "provideShapes"
* })
*/
public function benchFull($params): void
{
\NDArray::full($this->shape, 4);
}

public function provideShapes() {
yield ['shape' => [100, 1]];
yield ['shape' => [500, 1]];
yield ['shape' => [1000, 1]];
yield ['shape' => [10, 100]];
yield ['shape' => [500, 1000]];
yield ['shape' => [1000, 10000]];
}
}
?>
36 changes: 36 additions & 0 deletions benchmarks/initializers/IdentityInitializerBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* @Groups({"initializers"})
*/
class IdentityInitializerBench
{
/**
* @var size
*/
private $size = 0;

public function setUp(array $params): void
{
$this->size = $params['size'];
}

/**
* @BeforeMethods("setUp")
* @Revs(1000)
* @Iterations(5)
* @ParamProviders({
* "provideSizes",
* })
*/
public function benchIdentity($params)
{
$ndarray = \NDArray::identity($this->size);
}

public function provideSizes() {
yield ['size' => 100];
yield ['size' => 500];
yield ['size' => 1000];
}
}
?>
39 changes: 39 additions & 0 deletions benchmarks/initializers/OnesInitializerBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @Groups({"initializers"})
*/
class OnesInitializerBench
{
/**
* @var shape
*/
private $shape = [];

public function setUp(array $params): void
{
$this->shape = $params['shape'];
}

/**
* @BeforeMethods("setUp")
* @Revs(1000)
* @Iterations(5)
* @ParamProviders({
* "provideShapes"
* })
*/
public function benchOnes($params): void
{
\NDArray::ones($this->shape);
}

public function provideShapes() {
yield ['shape' => [100, 1]];
yield ['shape' => [500, 1]];
yield ['shape' => [1000, 1]];
yield ['shape' => [10, 100]];
yield ['shape' => [500, 1000]];
yield ['shape' => [1000, 10000]];
}
}
?>
39 changes: 39 additions & 0 deletions benchmarks/initializers/ZerosInitializerBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @Groups({"initializers"})
*/
class ZerosInitializerBench
{
/**
* @var shape
*/
private $shape = [];

public function setUp(array $params): void
{
$this->shape = $params['shape'];
}

/**
* @BeforeMethods("setUp")
* @Revs(1000)
* @Iterations(5)
* @ParamProviders({
* "provideShapes"
* })
*/
public function benchZeros($params): void
{
\NDArray::zeros($this->shape);
}

public function provideShapes() {
yield ['shape' => [100, 1]];
yield ['shape' => [500, 1]];
yield ['shape' => [1000, 1]];
yield ['shape' => [10, 100]];
yield ['shape' => [500, 1000]];
yield ['shape' => [1000, 10000]];
}
}
?>
49 changes: 49 additions & 0 deletions benchmarks/linalg/CholeskyBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* @Groups({"linearAlgebra"})
*/
class CholeskyBench
{
/**
* @var testArray
*/
private $testArray = [];

public function setUp(array $params): void
{
$this->testArray = $params['testArray'];
}

/**
* @BeforeMethods("setUp")
* @Revs(1000)
* @Iterations(5)
* @ParamProviders({
* "provideArrays"
* })
*/
public function benchCholesky($params): void
{
\NDArray::cholesky($this->testArray);
}

private function createArray($ndim) {
$symmetric = \NDArray::ones([$ndim, $ndim]);
for ($i=0; $i<$ndim; $i++) {
$symmetric[$i][$i] += $ndim * 10;
}
$L = \NDArray::cholesky($symmetric);
$L_T = \NDArray::transpose($L);
$positive_definite_matrix = \NDArray::matmul($L, $L_T);
return $positive_definite_matrix;
}

public function provideArrays() {
$testSizes = array(10, 100, 1000, 10000);
foreach ($testSizes as &$value) {
$currTestMatrix = $this->createArray($value);
yield ['testArray' => $currTestMatrix];
}
}
}
?>
46 changes: 46 additions & 0 deletions benchmarks/linalg/CondBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @Groups({"linearAlgebra"})
*/
class CondBench
{
/**
* @var testArray
*/
private $testArray = [];

public function setUp(array $params): void
{
$this->testArray = $params['testArray'];
}

/**
* @BeforeMethods("setUp")
* @Revs(1000)
* @Iterations(5)
* @ParamProviders({
* "provideArrays"
* })
*/
public function benchCond($params): void
{
\NDArray::cond($this->testArray);
}

private function createArray($ndim) {
$symmetric = \NDArray::ones([$ndim, $ndim]);
for ($i=0; $i<$ndim; $i++) {
$symmetric[$i][$i] += $ndim * 10;
}
return $symmetric;
}

public function provideArrays() {
$testSizes = array(10, 100, 1000);
foreach ($testSizes as &$value) {
$currTestMatrix = $this->createArray($value);
yield ['testArray' => $currTestMatrix];
}
}
}
?>
Loading

0 comments on commit d6ed83b

Please sign in to comment.