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

[JitLayer]Pybind JitLayer VarBase Function and add python UT #44010

Merged
merged 9 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
10 changes: 3 additions & 7 deletions paddle/fluid/jit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ cc_library(
cc_library(
jit_layer
SRCS layer.cc
DEPS jit_compilation_unit)
DEPS jit_serializer jit_function_utils jit_serializer_utils
jit_compilation_unit jit_function_schema)

if(WITH_TESTING
AND NOT WIN32
Expand All @@ -45,12 +46,7 @@ if(WITH_TESTING
feed_op
fetch_op
scale_op
jit_serializer
jit_layer
jit_function_utils
jit_function_schema
jit_compilation_unit
jit_serializer_utils)
jit_layer)
cc_test(
layer_test
SRCS layer_test.cc
Expand Down
8 changes: 8 additions & 0 deletions paddle/fluid/jit/compilation_unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,13 @@ void CompilationUnit::SetFunction(
function_dict_[name] = function;
}

std::vector<std::string> CompilationUnit::FunctionNames() const {
std::vector<std::string> names;
for (auto it = function_dict_.begin(); it != function_dict_.end(); it++) {
names.emplace_back(it->first);
}
return names;
}

} // namespace jit
} // namespace paddle
2 changes: 2 additions & 0 deletions paddle/fluid/jit/compilation_unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class CompilationUnit {
void SetFunction(const std::string &name,
const std::shared_ptr<BaseFunction> &function);

std::vector<std::string> FunctionNames() const;

private:
std::unordered_map<std::string, std::shared_ptr<BaseFunction>> function_dict_;
};
Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/jit/executor_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class ExecutorFunction : public BaseFunction {
return res;
}

const std::shared_ptr<FunctionInfo> &Info() const { return info_; }

private:
std::shared_ptr<FunctionInfo> info_;
framework::Scope scope_;
Expand Down
4 changes: 4 additions & 0 deletions paddle/fluid/jit/layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ void Layer::SetFunction(const std::string& name,
unit_.SetFunction(name, function);
}

std::vector<std::string> Layer::FunctionNames() const {
return unit_.FunctionNames();
}

} // namespace jit
} // namespace paddle
2 changes: 2 additions & 0 deletions paddle/fluid/jit/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Layer {
void SetFunction(const std::string& name,
const std::shared_ptr<BaseFunction>& function);

std::vector<std::string> FunctionNames() const;

private:
// internal::Object obj_;
Name2VariableMap params_dict_;
Expand Down
3 changes: 2 additions & 1 deletion paddle/fluid/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ set(PYBIND_DEPS
global_utils
phi_utils
tcp_store
new_profiler)
new_profiler
jit_layer)

if(WITH_PSCORE)
set(PYBIND_DEPS ${PYBIND_DEPS} ps_service)
Expand Down
45 changes: 45 additions & 0 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ limitations under the License. */
#ifdef PADDLE_WITH_ASCEND
#include "paddle/fluid/pybind/ascend_wrapper_py.h"
#endif
#include "paddle/fluid/jit/executor_function.h"
#include "paddle/fluid/jit/layer.h"
#include "paddle/fluid/jit/serializer.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里三个头文件都是不需要的吧?

#include "paddle/fluid/pybind/bind_cost_model.h"
#include "paddle/fluid/pybind/bind_fleet_executor.h"
#include "paddle/fluid/pybind/box_helper_py.h"
Expand Down Expand Up @@ -1743,6 +1746,48 @@ PYBIND11_MODULE(core_noavx, m) {
return new_rows;
});

py::class_<jit::ExecutorFunction, std::shared_ptr<jit::ExecutorFunction>>(
Aurelius84 marked this conversation as resolved.
Show resolved Hide resolved
m, "ExectorFunction", R"DOC(ExectorFunction Class.)DOC")
.def("__call__",
[](jit::ExecutorFunction &self,
const std::vector<std::shared_ptr<imperative::VarBase>>
&tensor_inputs) {
std::vector<Variable> var_inputs;
for (auto &tensor : tensor_inputs) {
var_inputs.emplace_back(tensor->Var());
}
auto var_outputs = self(var_inputs);

std::vector<std::shared_ptr<imperative::VarBase>> tensor_outputs;
auto output_names = self.Info()->OutputArgNames();
for (size_t i = 0; i < var_outputs.size(); ++i) {
auto var = var_outputs[i];
std::string name = output_names[i];
imperative::VariableWrapper var_wrapper(name, var);
auto shared_wrapper =
std::make_shared<imperative::VariableWrapper>(var_wrapper);
auto shared_varbase =
std::make_shared<imperative::VarBase>(shared_wrapper);
tensor_outputs.emplace_back(shared_varbase);
}
return tensor_outputs;
});

py::class_<jit::Layer>(m, "Layer", R"DOC(Layer Class.)DOC")
.def("function", &jit::Layer::Function)
.def("forward", &jit::Layer::forward)
.def("function_names", &jit::Layer::FunctionNames);

m.def("Load",
[](const std::string &path, const platform::CPUPlace &cpu_place) {
return paddle::jit::Load(path, cpu_place);
});

m.def("Load",
[](const std::string &path, const platform::CUDAPlace &cuda_place) {
Aurelius84 marked this conversation as resolved.
Show resolved Hide resolved
return paddle::jit::Load(path, cuda_place);
});

py::class_<Variable>(m, "Variable", R"DOC(Variable Class.

All parameter, weight, gradient are variables in Paddle.
Expand Down
17 changes: 17 additions & 0 deletions python/paddle/fluid/dygraph/jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,30 @@
from paddle.fluid.framework import _current_expected_place, _dygraph_guard, _dygraph_tracer
from paddle.fluid.framework import dygraph_only, _non_static_mode
from paddle.fluid.wrapped_decorator import wrap_decorator
from paddle.fluid.core import Load

__all__ = [
'TracedLayer', 'declarative', 'dygraph_to_static_func', 'set_code_level',
'set_verbosity', 'save', 'load', 'not_to_static'
]


class JitLayer():
Aurelius84 marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, load_path, place):
self.cpp_jit_layer = Load(load_path, place)
Aurelius84 marked this conversation as resolved.
Show resolved Hide resolved
# bind method
for func_name in self.cpp_jit_layer.function_names():
setattr(self, func_name, self.bind_funciton(func_name))

def bind_funciton(self, name):

def inner_funciton(*args):
return self.cpp_jit_layer.function(name)(args)
Aurelius84 marked this conversation as resolved.
Show resolved Hide resolved

return inner_funciton


def create_program_from_desc(program_desc):
program = Program()
program.desc = program_desc
Expand Down
81 changes: 81 additions & 0 deletions python/paddle/fluid/tests/unittests/test_jit_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright (c) 2020 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.

import os
import paddle
import unittest
import tempfile
import numpy as np
from paddle.static import InputSpec
from paddle.fluid.framework import _enable_legacy_dygraph
from paddle.fluid.dygraph.jit import JitLayer
from paddle.fluid.dygraph.dygraph_to_static.program_translator import ProgramTranslator

_enable_legacy_dygraph()
paddle.seed(1)


class Net(paddle.nn.Layer):

def __init__(self):
super(Net, self).__init__()
self.fc1 = paddle.nn.Linear(4, 4)
self.fc2 = paddle.nn.Linear(4, 4)
self._bias = 0.4

@paddle.jit.to_static(input_spec=[InputSpec([None, 4], dtype='float32')])
def forward(self, x):
out = self.fc1(x)
out = self.fc2(out)
out = paddle.nn.functional.relu(out)
out = paddle.mean(out)
return out

@paddle.jit.to_static(input_spec=[InputSpec([None, 4], dtype='float32')])
def infer(self, input):
out = self.fc2(input)
out = out + self._bias
out = paddle.mean(out)
return out


class TestMultiLoad(unittest.TestCase):

def test_multi_load(self):
self.temp_dir = tempfile.TemporaryDirectory()

x = paddle.full([2, 4], 2)
model = Net()
program_translator = ProgramTranslator()
program_translator.enable(False)
forward_out1 = model.forward(x)
infer_out1 = model.infer(x)
program_translator.enable(True)

model_path = os.path.join(self.temp_dir.name, 'multi_program')
paddle.jit.save(model, model_path, combine_params=True)
place = paddle.CPUPlace()
if paddle.is_compiled_with_cuda():
place = paddle.CUDAPlace(0)
jit_layer = JitLayer(model_path, place)
forward_out2 = jit_layer.forward(x)
infer_out2 = jit_layer.infer(x)
self.assertEqual(np.allclose(forward_out1, forward_out2[0]), True)
self.assertEqual(np.allclose(infer_out1, infer_out2[0]), True)

self.temp_dir.cleanup()


if __name__ == '__main__':
unittest.main()