forked from apache/systemds
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SYSTEMDS-3426] Python NN Builtin (Affine,Relu)
This commit adds the new interface for easy usage of our neural network in python. The design take inspiration from other neural network frameworks. This specific commit contains the building blocks of Affine and Relu. Closes apache#1848 Co-authored-by: Duc Thai Vu <[email protected]> Co-authored-by: Rahul Joshi <[email protected]> ...
- Loading branch information
1 parent
48de384
commit be15cc0
Showing
11 changed files
with
703 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# ------------------------------------------------------------- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
# ------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# ------------------------------------------------------------- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
# ------------------------------------------------------------- | ||
import os | ||
|
||
from systemds.context import SystemDSContext | ||
from systemds.operator import Matrix, Source, MultiReturn | ||
from systemds.utils.helpers import get_path_to_script_layers | ||
|
||
|
||
class Affine: | ||
_source: Source = None | ||
weight: Matrix | ||
bias: Matrix | ||
|
||
def __new__(cls, *args, **kwargs): | ||
return super().__new__(cls) | ||
|
||
def __init__(self, sds_context: SystemDSContext, d, m, seed=-1): | ||
""" | ||
sds_context: The systemdsContext to construct the layer inside of | ||
d: The number of features that are input to the affine layer | ||
m: The number of neurons that are contained in the layer, | ||
and the number of features output | ||
""" | ||
Affine._create_source(sds_context) | ||
|
||
# bypassing overload limitation in python | ||
self.forward = self._instance_forward | ||
self.backward = self._instance_backward | ||
|
||
# init weight and bias | ||
self.weight = Matrix(sds_context, '') | ||
self.bias = Matrix(sds_context, '') | ||
params_dict = {'D': d, 'M': m, 'seed': seed} | ||
out = [self.weight, self.bias] | ||
op = MultiReturn(sds_context, "affine::init", output_nodes=out, named_input_nodes=params_dict) | ||
self.weight._unnamed_input_nodes = [op] | ||
self.bias._unnamed_input_nodes = [op] | ||
op._source_node = self._source | ||
|
||
@staticmethod | ||
def forward(X: Matrix, W: Matrix, b: Matrix): | ||
""" | ||
X: An input matrix | ||
W: The hidden weights for the affine layer | ||
b: The bias added in the output. | ||
return out: An output matrix. | ||
""" | ||
Affine._create_source(X.sds_context) | ||
return Affine._source.forward(X, W, b) | ||
|
||
@staticmethod | ||
def backward(dout:Matrix, X: Matrix, W: Matrix, b: Matrix): | ||
""" | ||
dout: The gradient of the output, passed from the upstream | ||
X: The input matrix of this layer | ||
W: The hidden weights for the affine layer | ||
b: The bias added in the output | ||
return dX, dW, db: The gradients of: input X, weights and bias. | ||
""" | ||
sds = X.sds_context | ||
Affine._create_source(sds) | ||
params_dict = {'dout': dout, 'X': X, 'W': W, 'b': b} | ||
dX = Matrix(sds, '') | ||
dW = Matrix(sds, '') | ||
db = Matrix(sds, '') | ||
out = [dX, dW, db] | ||
op = MultiReturn(sds, "affine::backward", output_nodes=out, named_input_nodes=params_dict) | ||
dX._unnamed_input_nodes = [op] | ||
dW._unnamed_input_nodes = [op] | ||
db._unnamed_input_nodes = [op] | ||
op._source_node = Affine._source | ||
return op | ||
|
||
def _instance_forward(self, X: Matrix): | ||
""" | ||
X: The input matrix | ||
return out: The output matrix | ||
""" | ||
self._X = X | ||
return Affine.forward(X, self.weight, self.bias) | ||
|
||
def _instance_backward(self, dout: Matrix, X: Matrix): | ||
""" | ||
dout: The gradient of the output, passed from the upstream layer | ||
X: The input to this layer. | ||
return dX, dW,db: gradient of input, weights and bias, respectively | ||
""" | ||
return Affine.backward(dout, X, self.weight, self.bias) | ||
|
||
@staticmethod | ||
def _create_source(sds: SystemDSContext): | ||
if Affine._source is None or Affine._source.sds_context != sds: | ||
path = get_path_to_script_layers() | ||
path = os.path.join(path, "affine.dml") | ||
Affine._source = sds.source(path, "affine") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# ------------------------------------------------------------- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
# ------------------------------------------------------------- | ||
import os.path | ||
|
||
from systemds.context import SystemDSContext | ||
from systemds.operator import Matrix, Source | ||
from systemds.utils.helpers import get_path_to_script_layers | ||
|
||
|
||
class ReLU: | ||
_source: Source = None | ||
|
||
def __init__(self, sds: SystemDSContext): | ||
ReLU._create_source(sds) | ||
self.forward = self._instance_forward | ||
self.backward = self._instance_backward | ||
|
||
@staticmethod | ||
def forward(X: Matrix): | ||
""" | ||
X: input matrix | ||
return out: output matrix | ||
""" | ||
ReLU._create_source(X.sds_context) | ||
return ReLU._source.forward(X) | ||
|
||
@staticmethod | ||
def backward(dout: Matrix, X: Matrix): | ||
""" | ||
dout: gradient of output, passed from the upstream | ||
X: input matrix | ||
return dX: gradient of input | ||
""" | ||
ReLU._create_source(dout.sds_context) | ||
return ReLU._source.backward(dout, X) | ||
|
||
def _instance_forward(self, X: Matrix): | ||
self._X = X | ||
return ReLU.forward(X) | ||
|
||
def _instance_backward(self, dout: Matrix, X: Matrix): | ||
return ReLU.backward(dout, X) | ||
|
||
@staticmethod | ||
def _create_source(sds: SystemDSContext): | ||
if ReLU._source is None or ReLU._source.sds_context != sds: | ||
path = get_path_to_script_layers() | ||
path = os.path.join(path, "relu.dml") | ||
ReLU._source = sds.source(path, "relu") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# ------------------------------------------------------------- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
# ------------------------------------------------------------- |
Oops, something went wrong.