Skip to content

Commit

Permalink
Type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiis committed Apr 26, 2024
1 parent 3f6d6a8 commit e9b1a25
Show file tree
Hide file tree
Showing 173 changed files with 4,331 additions and 2,169 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: tests

on: [push]
on:
push:
branches:
- master

jobs:
no_ext_tests:
Expand All @@ -20,6 +23,12 @@ jobs:
with:
php_version: ${{ matrix.php }}

- name: PHP Static Analysys
uses: php-actions/phpstan@v3
with:
php_version: ${{ matrix.php }}
path: src/

- name: PHPUnit Tests
uses: php-actions/phpunit@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Required environment
--------------------

- PHP 8.1, 8.2, 8.3
- For PHP 7.x environments, use Release 1.x.
- For PHP 7.x, 8.0 environments, use Release 1.x.

Install
-------
Expand All @@ -61,7 +61,7 @@ Please set up an external library.

Prebuilt binaries:
- Rindow-matlib: https://github.com/rindow/rindow-matlib/releases
- OpenBLAS: https://github.com/xianyi/OpenBLAS/releases
- OpenBLAS: https://github.com/OpenMathLib/OpenBLAS/releases

Please set up according to your environment. Click [here](https://github.com/rindow/rindow-math-matrix-matlibffi) for detailed instructions.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"suggest": {
"rindow/rindow-math-plot": "when visualize machine learning results",
"rindow/rindow-math-matrix-matlibffi": "~1.0.0",
"rindow/rindow-math-matrix-matlibffi": "~1.0.1",
"ext-pdo_sqlite": "for save model"
},
"autoload": {
Expand Down
21 changes: 21 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
parameters:
ignoreErrors:
-
message: "#^Cannot call method dtype\\(\\) on iterable\\<Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray\\>\\.$#"
count: 1
path: src/Data/Image/ImageFilter.php

-
message: "#^Cannot call method shape\\(\\) on iterable\\<Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray\\>\\.$#"
count: 2
path: src/Data/Image/ImageFilter.php

-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
path: src/Gradient/Core/GraphFunction.php

-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
path: src/Gradient/Func/Get.php
25 changes: 25 additions & 0 deletions phpstan-interop.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray::setPortableSerializeMode\\(\\)#"
-
message: "#of function count expects array\\|Countable, Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray given#"

# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dtype\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:addr\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:value_size\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dump\\(\\)#"
# -
# message: "#function count expects array|Countable, Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray given#"
# -
# message: "#^Argument of an invalid type Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray supplied for foreach, only iterables are supported#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:bytes\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:copy\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:read\\(\\)#"
25 changes: 25 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
includes:
- phpstan-baseline.neon
- phpstan-interop.neon

parameters:
level: 6
paths:
- src
ignoreErrors:
-
message: "#Call to an undefined method Rindow\\\\NeuralNetworks\\\\Gradient\\\\Variable::value\\(\\)#"
-
message: "#^Call to an undefined method .*Rindow\\\\NeuralNetworks\\\\Model\\\\Model::forward\\(\\)#"
-
message: "#^Method Rindow\\\\NeuralNetworks\\\\Model\\\\Sequential::call\\(\\)#"
path: src/Model/Sequential.php
-
message: "#^Call to an undefined method Rindow\\\\NeuralNetworks\\\\Model\\\\AbstractModel::call\\(\\)#"
path: src/Model/AbstractModel.php
-
message: "#Call to an undefined method Rindow\\\\NeuralNetworks\\\\Builder\\\\Builder::utils\\(\\)#"
-
message: "#^Function Rindow\\\\Math\\\\Matrix\\\\R not found#"
-
message: "#^Used function Rindow\\\\Math\\\\Matrix\\\\R not found#"
2 changes: 1 addition & 1 deletion samples/basic-image-classification-custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function call($inputs)
}

echo "device type: ".$nn->deviceType()."\n";
$model = new ImageClassification($nn->backend(),$nn);
$model = new ImageClassification($nn);
echo "creating model ...\n";
$model->compile(
loss:$nn->losses()->SparseCategoricalCrossentropy(from_logits:true),
Expand Down
4 changes: 1 addition & 3 deletions samples/neural-machine-translation-with-attention.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ public function convert($lang, NDArray $tensor) : void

class Encoder extends AbstractModel
{
protected $backend;
protected $vocabSize;
protected $wordVectSize;
protected $units;
Expand Down Expand Up @@ -216,7 +215,6 @@ public function computeMask($inputs)

class Decoder extends AbstractModel
{
protected $backend;
protected $vocabSize;
protected $wordVectSize;
protected $units;
Expand All @@ -238,6 +236,7 @@ public function __construct(
int $targetLength
)
{
parent::__construct($builder);
$this->vocabSize = $vocabSize;
$this->wordVectSize = $wordVectSize;
$this->units = $units;
Expand Down Expand Up @@ -298,7 +297,6 @@ class Seq2seq extends AbstractModel
protected $decoder;
protected $out;
protected $mo;
protected $backend;
protected $startVocId;
protected $endVocId;
protected $inputLength;
Expand Down
4 changes: 2 additions & 2 deletions src/Activation/AbstractActivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ abstract class AbstractActivation implements Activation
abstract protected function call(NDArray $inputs, bool $training=null) : NDArray;
abstract protected function differentiate(NDArray $dOutputs) : NDArray;

protected $states;
protected $backend;
protected ?object $states=null;
protected object $backend;
public function __construct(object $backend)
{
$this->backend = $backend;
Expand Down
14 changes: 4 additions & 10 deletions src/Activation/FunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

class FunctionFactory
{
static $functions = [
/** @var array<string,string|null> $functions */
static array $functions = [
'sigmoid' => Sigmoid::class,
'softmax' => Softmax::class,
'relu' => ReLU::class,
Expand All @@ -16,22 +17,15 @@ static public function factory(object $backend, string $name) : ?Activation
{
if(array_key_exists($name,self::$functions)) {
$class = self::$functions[$name];
if($class===null) {
if($class==null) {
return null;
}
return new $class($backend);
}
if(is_subclass_of($name, Activation::class)) {
return new $class($backend);
return new $name($backend);
}

if(is_string($name)||is_numeric($name)) {
;
} elseif(is_object($name)) {
$name = 'object:'.get_class($name);
} else {
$name = gettype($name);
}
throw new InvalidArgumentException('invalid function name:'.$name);
}
}
2 changes: 1 addition & 1 deletion src/Activation/Sigmoid.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Sigmoid extends AbstractActivation
{
protected $incorporatedLoss = false;
protected bool $incorporatedLoss = false;

protected function call(NDArray $inputs, bool $training=null) : NDArray
{
Expand Down
Loading

0 comments on commit e9b1a25

Please sign in to comment.