Skip to content

Commit

Permalink
add callZooFunc and change all callBigDlFunc to callZooFunc (intel-an…
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuxin2012 authored Nov 26, 2019
1 parent 2b16b5e commit dc3c581
Showing 1 changed file with 62 additions and 59 deletions.
121 changes: 62 additions & 59 deletions python/dllib/src/bigdl/dllib/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import sys

from bigdl.nn.layer import Layer, Node
from bigdl.util.common import callBigDlFunc, to_list, JTensor
from bigdl.util.common import to_list, JTensor

from zoo.common.utils import callZooFunc
import zoo.pipeline.api.keras.base as kbase
from zoo.pipeline.api.keras.objectives import LossFunction
from zoo.pipeline.api.utils import remove_batch, toMultiShape
Expand All @@ -39,7 +40,7 @@ def mean(x, axis=0, keepDims=False):
the reduced dimensions are retained with length 1.
:return: A variable with the mean of elements of `x`.
"""
return Variable.from_jvalue(callBigDlFunc("float", "mean", x, axis, keepDims))
return Variable.from_jvalue(callZooFunc("float", "mean", x, axis, keepDims))


def abs(x):
Expand All @@ -48,7 +49,7 @@ def abs(x):
:param x: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "abs", x))
return Variable.from_jvalue(callZooFunc("float", "abs", x))


def batch_dot(x, y, axes=1, normalize=False):
Expand All @@ -73,7 +74,7 @@ def batch_dot(x, y, axes=1, normalize=False):
if not normalize:
if isinstance(axes, int):
axes = [axes] * 2
return Variable.from_jvalue(callBigDlFunc("float", "batchDot", x, y, axes, normalize))
return Variable.from_jvalue(callZooFunc("float", "batchDot", x, y, axes, normalize))


def l2_normalize(x, axis):
Expand All @@ -83,7 +84,7 @@ def l2_normalize(x, axis):
:param axis: axis along which to perform normalization.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "l2Normalize", x, int(axis)))
return Variable.from_jvalue(callZooFunc("float", "l2Normalize", x, int(axis)))


def sum(x, axis=0, keepDims=False):
Expand All @@ -97,7 +98,7 @@ def sum(x, axis=0, keepDims=False):
the reduced dimensions are retained with length 1.
:return: A variable with sum of `x`.
"""
return Variable.from_jvalue(callBigDlFunc("float", "sum", x, axis, keepDims))
return Variable.from_jvalue(callZooFunc("float", "sum", x, axis, keepDims))


def stack(inputs, axis=1):
Expand All @@ -108,7 +109,7 @@ def stack(inputs, axis=1):
:param axis: axis along which to perform stacking.
:return:
"""
return Variable.from_jvalue(callBigDlFunc("float", "stack", inputs, axis))
return Variable.from_jvalue(callZooFunc("float", "stack", inputs, axis))


def expand_dims(x, axis):
Expand All @@ -118,7 +119,7 @@ def expand_dims(x, axis):
:param axis: axis Position where to add a new axis.
The axis is 0 based and if you set the axis to 0, you would change the batch dim.
"""
return Variable.from_jvalue(callBigDlFunc("float", "expandDims", x, axis))
return Variable.from_jvalue(callZooFunc("float", "expandDims", x, axis))


def clip(x, min, max):
Expand All @@ -129,15 +130,15 @@ def clip(x, min, max):
:param max: Python float or integer.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "clip", x, float(min), float(max)))
return Variable.from_jvalue(callZooFunc("float", "clip", x, float(min), float(max)))


def contiguous(x):
"""
Turn the output and grad to be contiguous for the input Variable
:param x: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "contiguous", x))
return Variable.from_jvalue(callZooFunc("float", "contiguous", x))


def square(x):
Expand All @@ -146,7 +147,7 @@ def square(x):
:param x: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "square", x))
return Variable.from_jvalue(callZooFunc("float", "square", x))


def sqrt(x):
Expand All @@ -155,7 +156,7 @@ def sqrt(x):
:param x: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "sqrt", x))
return Variable.from_jvalue(callZooFunc("float", "sqrt", x))


def exp(x):
Expand All @@ -164,7 +165,7 @@ def exp(x):
:param x: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "exp", x))
return Variable.from_jvalue(callZooFunc("float", "exp", x))


def maximum(x, y):
Expand All @@ -174,7 +175,7 @@ def maximum(x, y):
:param y: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "maximum", x, y))
return Variable.from_jvalue(callZooFunc("float", "maximum", x, y))


def log(x):
Expand All @@ -183,7 +184,7 @@ def log(x):
:param x: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "log", x))
return Variable.from_jvalue(callZooFunc("float", "log", x))


def pow(x, a):
Expand All @@ -193,15 +194,15 @@ def pow(x, a):
:param a: Python integer.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "pow", x, float(a)))
return Variable.from_jvalue(callZooFunc("float", "pow", x, float(a)))


def epsilon():
"""
Define the value of epsilon.
:return: A value of type Double.
"""
return Variable.from_jvalue(callBigDlFunc("float", "epsilon"))
return Variable.from_jvalue(callZooFunc("float", "epsilon"))


def neg(x):
Expand All @@ -210,7 +211,7 @@ def neg(x):
:param x: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "neg", x))
return Variable.from_jvalue(callZooFunc("float", "neg", x))


def softsign(x):
Expand All @@ -219,7 +220,7 @@ def softsign(x):
:param x: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "softsign", x))
return Variable.from_jvalue(callZooFunc("float", "softsign", x))


def softplus(x):
Expand All @@ -228,7 +229,7 @@ def softplus(x):
:param x: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "softplus", x))
return Variable.from_jvalue(callZooFunc("float", "softplus", x))


def mm(x, y, axes=None):
Expand All @@ -240,7 +241,7 @@ def mm(x, y, axes=None):
:param axes: Axes along which to perform multiplication.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "mm", x, y, axes))
return Variable.from_jvalue(callZooFunc("float", "mm", x, y, axes))


def erf(x):
Expand All @@ -249,7 +250,7 @@ def erf(x):
:param x: A variable.
:return: A variable.
"""
return Variable.from_jvalue(callBigDlFunc("float", "erf", x))
return Variable.from_jvalue(callZooFunc("float", "erf", x))


class VariableOperator(object):
Expand All @@ -264,10 +265,10 @@ def __process_shape(self, shape):
return [self.__to_batch_shape(s) for s in shape]

def get_input_shape(self):
return self.__process_shape(callBigDlFunc("float", "varGetInputShape", self))
return self.__process_shape(callZooFunc("float", "varGetInputShape", self))

def get_output_shape(self):
return self.__process_shape(callBigDlFunc("float", "varGetOutputShape", self))
return self.__process_shape(callZooFunc("float", "varGetOutputShape", self))

@property
def shape(self):
Expand All @@ -278,35 +279,35 @@ def from_jvalue(jvalue):
return Variable(input_shape=None, node=None, jvalue=jvalue)

def add(self, var):
return Variable.from_jvalue(callBigDlFunc("float", "add", self, var))
return Variable.from_jvalue(callZooFunc("float", "add", self, var))
# self.value.getClass().getSimpleName()

def sub(self, var):
return Variable.from_jvalue(callBigDlFunc("float", "sub", self, var))
return Variable.from_jvalue(callZooFunc("float", "sub", self, var))

def __sub__(self, other):
return self.sub(other)

def __rsub__(self, other):
return Variable.from_jvalue(callBigDlFunc("float", "sub", other, self))
return Variable.from_jvalue(callZooFunc("float", "sub", other, self))

def __add__(self, other):
return self.add(other)

__radd__ = __add__

def __mul__(self, other):
return Variable.from_jvalue(callBigDlFunc("float", "mul", self, other))
return Variable.from_jvalue(callZooFunc("float", "mul", self, other))

__rmul__ = __mul__

def __div__(self, other):
return Variable.from_jvalue(callBigDlFunc("float", "div", self, other))
return Variable.from_jvalue(callZooFunc("float", "div", self, other))

__truediv__ = __div__

def __rdiv__(self, other):
return Variable.from_jvalue(callBigDlFunc("float", "div", other, self))
return Variable.from_jvalue(callZooFunc("float", "div", other, self))

__rtruediv__ = __rdiv__

Expand Down Expand Up @@ -334,7 +335,7 @@ def slice(self, dim, start_index, length):
:param length The length to be sliced. Default is 1.
"""
return Variable.from_jvalue(
callBigDlFunc("float", "slice", self, dim, start_index, length))
callZooFunc("float", "slice", self, dim, start_index, length))

def index_select(self, dim, index):
"""
Expand All @@ -352,7 +353,7 @@ def index_select(self, dim, index):
-1 means the last dimension of the input.
:return:
"""
return Variable.from_jvalue(callBigDlFunc("float", "indexSelect", self, dim, index))
return Variable.from_jvalue(callZooFunc("float", "indexSelect", self, dim, index))

def squeeze(self, dim=None):
"""
Expand All @@ -362,7 +363,7 @@ def squeeze(self, dim=None):
Squeeze(dim = 1) will give output size (2, 3, 4, 1)
Squeeze(dims = null) will give output size (2, 3, 4)
"""
return Variable.from_jvalue(callBigDlFunc("float", "squeeze", self, dim))
return Variable.from_jvalue(callZooFunc("float", "squeeze", self, dim))


class Variable(kbase.ZooKerasCreator, VariableOperator):
Expand Down Expand Up @@ -424,10 +425,10 @@ def __call__(self, x=None):
if isinstance(self, Lambda):
input_shapes = [var.get_output_shape() for var in x]
layer = self.create(remove_batch(input_shapes))
return Variable.from_jvalue(callBigDlFunc(self.bigdl_type,
"connectInputs",
layer,
to_list(x)))
return Variable.from_jvalue(callZooFunc(self.bigdl_type,
"connectInputs",
layer,
to_list(x)))

# input_shapes should not contain batch dim
def create(self, input_shapes):
Expand Down Expand Up @@ -457,6 +458,7 @@ class Parameter(kbase.ZooKerasLayer, VariableOperator):
:param init_weight: A ndarray as the init value.
:param trainable It's true by default, meaning the value would be updated by gradient.
"""

def __init__(self, shape, init_method=None,
init_weight=None, trainable=True, **kwargs):
if not init_method:
Expand All @@ -467,7 +469,7 @@ def __init__(self, shape, init_method=None,
init_method,
kbase.JTensor.from_ndarray(init_weight),
trainable,
** kwargs)
**kwargs)

@property
def shape(self):
Expand All @@ -477,20 +479,20 @@ def get_weight(self):
"""
:return: the ndarray for the current weight
"""
jtensor = callBigDlFunc(self.bigdl_type,
"getParameterWeight",
self)
jtensor = callZooFunc(self.bigdl_type,
"getParameterWeight",
self)
return jtensor.to_ndarray()

def set_weight(self, value):
"""
:param value: value is a ndarray
:return:
"""
callBigDlFunc(self.bigdl_type,
"setParameterWeight",
self,
kbase.JTensor.from_ndarray(value))
callZooFunc(self.bigdl_type,
"setParameterWeight",
self,
kbase.JTensor.from_ndarray(value))


class Constant(kbase.ZooKerasCreator, VariableOperator):
Expand All @@ -499,6 +501,7 @@ class Constant(kbase.ZooKerasCreator, VariableOperator):
:param data: value of the Variable.
:param name: Optional. Name of the Variable
"""

def __init__(self, data, name=None, bigdl_type="float"):
self.data = data
super(Constant, self).__init__(None, bigdl_type, JTensor.from_ndarray(data), name)
Expand Down Expand Up @@ -533,13 +536,13 @@ def forward(self, y_true, y_pred):
target = y_true
jinput, input_is_table = Layer.check_input(input)
jtarget, target_is_table = Layer.check_input(target)
output = callBigDlFunc(self.bigdl_type,
"criterionForward",
self.value,
jinput,
input_is_table,
jtarget,
target_is_table)
output = callZooFunc(self.bigdl_type,
"criterionForward",
self.value,
jinput,
input_is_table,
jtarget,
target_is_table)
return output

def backward(self, y_true, y_pred):
Expand All @@ -555,11 +558,11 @@ def backward(self, y_true, y_pred):
target = y_true
jinput, input_is_table = Layer.check_input(input)
jtarget, target_is_table = Layer.check_input(target)
output = callBigDlFunc(self.bigdl_type,
"criterionBackward",
self.value,
jinput,
input_is_table,
jtarget,
target_is_table)
output = callZooFunc(self.bigdl_type,
"criterionBackward",
self.value,
jinput,
input_is_table,
jtarget,
target_is_table)
return Layer.convert_output(output)

0 comments on commit dc3c581

Please sign in to comment.