Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support pow with scalar input, square, cast, var, size operators for deepxde #46024

Merged
merged 6 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion paddle/fluid/operators/prim_ops/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ set(PRIM_OP_SRCS
pow_p_op.cc
max_p_op.cc
erf_p_op.cc
abs_p_op.cc)
abs_p_op.cc
cast_p_op.cc)

cc_test(
prim_op_test
Expand Down
78 changes: 78 additions & 0 deletions paddle/fluid/operators/prim_ops/cast_p_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed 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.

#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"

namespace paddle {
namespace framework {
class InferShapeContext;
class VarDesc;
} // namespace framework
} // namespace paddle

namespace paddle {
namespace operators {
class CastPrimOp : public framework::OperatorBase {
public:
CastPrimOp(const std::string &type,
const framework::VariableNameMap &inputs,
const framework::VariableNameMap &outputs,
const framework::AttributeMap &attrs)
: framework::OperatorBase(type, inputs, outputs, attrs) {}
void RunImpl(const framework::Scope &scope,
const platform::Place &dev_place) const override {
PADDLE_THROW(platform::errors::Unimplemented(
"Prim operator cast_p should not be excuted directly"));
}
};

class CastPrimOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X", "(Tensor), The input tensor of cast_p op.");
AddOutput("Y", "(Tensor), The output tensor of cast_p op.");
AddAttr<int>("dtype", "output data type");
AddComment(R"DOC(Autograd primitive cast_p operator.)DOC");
}
};

class CastPrimOpShapeInference : public framework::InferShapeBase {
public:
void operator()(framework::InferShapeContext *ctx) const override {
framework::InferShapeVarPtr x_var_ptr = ctx->GetInputVarPtrs("X")[0];
framework::InferShapeVarPtr y_var_ptr = ctx->GetOutputVarPtrs("Y")[0];
framework::VarDesc *x_var = PADDLE_GET(framework::VarDesc *, x_var_ptr);
PADDLE_GET(framework::VarDesc *, y_var_ptr)->SetShape(x_var->GetShape());
}
};

class CastPrimOpVarTypeInference
: public framework::StaticGraphVarTypeInference {
public:
void operator()(framework::InferVarTypeContext *ctx) const override {
auto out_type = static_cast<framework::proto::VarType::Type>(
PADDLE_GET_CONST(int, ctx->GetAttr("dtype")));
ctx->SetOutputDataType("Y", out_type);
}
};

} // namespace operators
} // namespace paddle

REGISTER_OPERATOR(cast_p,
paddle::operators::CastPrimOp,
paddle::operators::CastPrimOpMaker,
paddle::operators::CastPrimOpShapeInference,
paddle::operators::CastPrimOpVarTypeInference);
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,42 @@ def init_data(self):
]


class TestCastPJVPAndTranspose(TestAddPJVPAndTranspose):

def init_data(self):
# Set prim op
self.op_type = 'cast_p'
X = paddle.static.data(name='X', shape=[5, 6], dtype='int64')
self.prim_input = {
'X': X,
}
self.prim_output = {
'Y':
self.layer_help.create_variable_for_type_inference(dtype=X.dtype)
}
self.prim_attrs = {'dtype': paddle.float64}

# Set JVP
X_DOT = paddle.static.data(name='X_DOT', shape=[5, 6], dtype='int64')
self.jvp_args = (X_DOT, )
self.jvp_out_shape_map = {0: self.prim_output['Y']}

# Set transpose
check_dot = lambda v: True
Y_BAR = paddle.static.data(name='Y_BAR', shape=[5, 6], dtype='float')
self.transpose_args = (check_dot, Y_BAR)
self.transpose_out_shape_map = {0: X}

self.all_ops = [
# prim op:
'cast_p',
# jvp op:
'cast_p',
# transpose op:
'cast_p'
]


class TestLogPJVPAndTranspose(TestAddPJVPAndTranspose):

def init_data(self):
Expand Down
93 changes: 93 additions & 0 deletions python/paddle/fluid/tests/unittests/autograd/test_orig2prim.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ def init_data(self):
self.out_map = {0: self.output['Out']}


class TestElementWiseDivOrig2Prim(TestElementWiseAddOrig2Prim):

def init_data(self):
self.op_type = 'elementwise_div'
X = paddle.static.data(name='X', shape=[8, 8], dtype='float')
Y = paddle.static.data(name='Y', shape=[8, 8], dtype='float')

self.input = {'X': X, 'Y': Y}
self.output = {
'Out':
self.layer_help.create_variable_for_type_inference(dtype=X.dtype)
}
self.attrs = {}

self.orig2prim_args = (X, Y)
self.all_ops = ['elementwise_div', 'div_p']
# { prim_op_output_index: orig_op_output_var }
self.out_map = {0: self.output['Out']}


class TestMatmulV2Orig2Prim(TestElementWiseAddOrig2Prim):

def init_data(self):
Expand Down Expand Up @@ -786,5 +806,78 @@ def init_data(self):
self.out_map = {0: self.output['Out']}


class TestSizeOrig2Prim(TestElementWiseAddOrig2Prim):

def init_data(self):
self.op_type = 'size'
X = paddle.static.data(name='X', shape=[5, 8], dtype='float')

self.input = {'Input': X}
self.output = {
'Out':
self.layer_help.create_variable_for_type_inference(
dtype=paddle.int64)
}
self.attrs = {}
self.orig2prim_args = (X, )
self.all_ops = ['size', 'fill_constant_p']
# { prim_op_output_index: orig_op_output_var }
self.out_map = {0: self.output['Out']}


class TestCastOrig2Prim(TestElementWiseAddOrig2Prim):

def init_data(self):
self.op_type = 'cast'
X = paddle.static.data(name='X', shape=[5, 8], dtype='float')

self.input = {'X': X}
self.output = {
'Out':
self.layer_help.create_variable_for_type_inference(dtype=X.dtype)
}
self.attrs = {'in_dtype': X.dtype, 'out_dtype': paddle.float64}
self.orig2prim_args = (X, )
self.all_ops = ['cast', 'cast_p']
# { prim_op_output_index: orig_op_output_var }
self.out_map = {0: self.output['Out']}


class TestPowScalarOrig2Prim(TestElementWiseAddOrig2Prim):

def init_data(self):
self.op_type = 'pow'
X = paddle.static.data(name='X', shape=[5, 8], dtype='float')

self.input = {'X': X}
self.output = {
'Out':
self.layer_help.create_variable_for_type_inference(dtype=X.dtype)
}
self.attrs = {'factor': 2.}
self.orig2prim_args = (None, X)
self.all_ops = ['pow', 'pow_p', 'fill_constant_p']
# { prim_op_output_index: orig_op_output_var }
self.out_map = {0: self.output['Out']}


class TestSquareOrig2Prim(TestElementWiseAddOrig2Prim):

def init_data(self):
self.op_type = 'square'
X = paddle.static.data(name='X', shape=[5, 8], dtype='float')

self.input = {'X': X}
self.output = {
'Out':
self.layer_help.create_variable_for_type_inference(dtype=X.dtype)
}
self.attrs = {}
self.orig2prim_args = (X, )
self.all_ops = ['square', 'pow_p', 'fill_constant_p']
# { prim_op_output_index: orig_op_output_var }
self.out_map = {0: self.output['Out']}


if __name__ == '__main__':
unittest.main()
20 changes: 20 additions & 0 deletions python/paddle/fluid/tests/unittests/autograd/test_prim2orig.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,5 +670,25 @@ def init_data(self):
self.out_map = {self.output['Z']: 0}


class TestCastPPrim2Orig(TestAddPPrim2Orig):

def init_data(self):
self.op_type = 'cast_p'
X = paddle.static.data(name='X', shape=[7, 8], dtype='float64')

self.input = {
'X': X,
}
self.output = {
'Y':
self.layer_help.create_variable_for_type_inference(dtype=X.dtype)
}
self.attrs = {'dtype': paddle.int64}

self.prim2orig_args = (X, )
self.all_ops = ['cast_p', 'cast']
self.out_map = {self.output['Y']: 0}


if __name__ == '__main__':
unittest.main()
18 changes: 17 additions & 1 deletion python/paddle/fluid/tests/unittests/autograd/test_primapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ def test_illegal_param(self):
(np.random.rand(2, 3), np.random.rand(3, 2)), None, 'float32'),
('multiply', paddle.multiply,
(np.random.rand(2, 3), np.random.rand(2, 3)), None, 'float64'),
('div', paddle.divide,
(np.random.rand(2, 3), np.random.rand(2, 3)), None, 'float64'),
('add', paddle.add,
(np.random.rand(2, 3), np.random.rand(2, 3)), None, 'float32'),
('input_not_sequence', paddle.tanh,
Expand Down Expand Up @@ -300,7 +302,21 @@ def test_illegal_param(self):
(np.random.rand(200, 345), ), None, 'float32'),
('abs', paddle.abs, (np.random.uniform(-10, 10,
(200, 345)), ), None, 'float32'),
))
('cast_float', lambda x: paddle.cast(x, paddle.float64),
(np.random.rand(10, 20), ), None, 'float32'),
('cast_int', lambda x: paddle.cast(x, paddle.int32),
(np.random.rand(10, 20), ), None, 'float32'),
('square', paddle.square, (np.random.rand(100), ), None, 'float32'),
('pow_scalar', lambda x: paddle.pow(x, 2),
(np.random.rand(20, 30), ), None, 'float32'),
('var', paddle.var, (np.random.rand(200, 324), ), None, 'float32'),
('var_with_axis', lambda x: paddle.var(x, axis=1),
(np.random.rand(10, 20, 30), ), None, 'float32'),
('var_without_unbiased',
lambda x: paddle.var(x, axis=1, unbiased=False),
(np.random.rand(10, 20, 30), ), None, 'float32'),
('var_with_keepdim', lambda x: paddle.var(x, axis=1, keepdim=True),
(np.random.rand(10, 20, 30), ), None, 'float32')))
class TestGrad(unittest.TestCase):

def setUp(self):
Expand Down
3 changes: 3 additions & 0 deletions python/paddle/fluid/tests/unittests/autograd/test_primops.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
('erf', primops.erf, randn(2, 3), {}, (2, 3), 'float64'),
('abs', primops.abs, randn(2, 3), {}, (2, 3), 'float64'),
('log', primops.log, randn(2, 3), {}, (2, 3), 'float64'),
('cast', primops.cast, randn(2, 3), {
'dtype': paddle.int64
}, (2, 3), 'int64'),
('reshape', primops.reshape, randn(2, 3), {
'shape': (3, 2)
}, (3, 2), 'float64'),
Expand Down
12 changes: 12 additions & 0 deletions python/paddle/incubate/autograd/primops.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,15 @@ def max(x, y, out=None):
@REGISTER_FN('erf_p', 'X', 'Y')
def erf(x, out=None):
return _simple_unop(LayerHelper('erf_p', **locals()))


@REGISTER_FN('cast_p', 'X', 'Y')
def cast(x, dtype, out=None):
helper = LayerHelper('cast_p', **locals())
if out is None:
out = helper.create_variable_for_type_inference(dtype)
helper.append_op(type=helper.layer_type,
inputs={'X': x},
outputs={'Y': out},
attrs={'dtype': dtype})
return out
2 changes: 1 addition & 1 deletion python/paddle/incubate/autograd/primreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def div(x, y, out=None):

"""
args = _primop_position_argnames.lookup(op.type)
assert args is not None, 'args should not be None in op_position_inputs().'
assert args is not None, f'args of {op.type} should not be None in op_position_inputs().'
*input_names, _ = args

inputs = []
Expand Down
Loading