未验证 提交 2fc93f39 编写于 作者: W WangZhen 提交者: GitHub

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

* Pybind JitLayer VarBase Function and add python UT

* Add multi program load UT

* Fix UT place error

* Update jit.save param name

* Remove some comments

* Polish cmakelists

* Polish JitLayer in Python

* Fix comments
上级 21ae549e
......@@ -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
......@@ -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
......
......@@ -22,16 +22,28 @@ namespace jit {
std::shared_ptr<BaseFunction> CompilationUnit::Function(
const std::string &name) const {
PADDLE_ENFORCE_EQ(
function_dict_.count(name),
function_map_.count(name),
1,
platform::errors::InvalidArgument(
"Funciton name %s is not exist in function_dict_.", name));
return function_dict_.at(name);
"Funciton name %s is not exist in function_map_.", name));
return function_map_.at(name);
}
void CompilationUnit::SetFunction(
const std::string &name, const std::shared_ptr<BaseFunction> &function) {
function_dict_[name] = function;
function_map_[name] = function;
}
std::vector<std::string> CompilationUnit::FunctionNames() const {
std::vector<std::string> names;
for (auto it = function_map_.begin(); it != function_map_.end(); it++) {
names.emplace_back(it->first);
}
return names;
}
const Name2FunctionMap &CompilationUnit::FunctionMap() const {
return function_map_;
}
} // namespace jit
......
......@@ -21,6 +21,8 @@
namespace paddle {
namespace jit {
using Name2FunctionMap =
std::unordered_map<std::string, std::shared_ptr<BaseFunction>>;
class CompilationUnit {
public:
......@@ -32,8 +34,12 @@ class CompilationUnit {
void SetFunction(const std::string &name,
const std::shared_ptr<BaseFunction> &function);
std::vector<std::string> FunctionNames() const;
const Name2FunctionMap &FunctionMap() const;
private:
std::unordered_map<std::string, std::shared_ptr<BaseFunction>> function_dict_;
Name2FunctionMap function_map_;
};
} // namespace jit
......
......@@ -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_;
......
......@@ -42,5 +42,13 @@ void Layer::SetFunction(const std::string& name,
unit_.SetFunction(name, function);
}
std::vector<std::string> Layer::FunctionNames() const {
return unit_.FunctionNames();
}
const Name2FunctionMap& Layer::FunctionMap() const {
return unit_.FunctionMap();
}
} // namespace jit
} // namespace paddle
......@@ -50,6 +50,10 @@ class Layer {
void SetFunction(const std::string& name,
const std::shared_ptr<BaseFunction>& function);
std::vector<std::string> FunctionNames() const;
const Name2FunctionMap& FunctionMap() const;
private:
// internal::Object obj_;
Name2VariableMap params_dict_;
......
......@@ -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)
......@@ -121,7 +122,8 @@ set(PYBIND_SRCS
io.cc
generator_py.cc
communication.cc
cuda_streams_py.cc)
cuda_streams_py.cc
jit.cc)
if(WITH_CUSTOM_DEVICE)
set(PYBIND_DEPS ${PYBIND_DEPS} phi_capi)
......
/* 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/pybind/jit.h"
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/imperative/layer.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/jit/executor_function.h"
#include "paddle/fluid/jit/function_schema.h"
#include "paddle/fluid/jit/layer.h"
#include "paddle/fluid/jit/serializer.h"
namespace py = pybind11;
namespace paddle {
namespace pybind {
using Variable = paddle::framework::Variable;
void BindJit(pybind11::module *m) {
py::class_<jit::Layer>(*m, "Layer", R"DOC(Layer Class.)DOC")
.def("function_dict", &jit::Layer::FunctionMap);
py::class_<jit::ExecutorFunction, std::shared_ptr<jit::ExecutorFunction>>(
*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;
})
.def("info", &jit::ExecutorFunction::Info);
py::class_<jit::FunctionInfo, std::shared_ptr<jit::FunctionInfo>>(
*m, "FunctionInfo", R"DOC(FunctionInfo Class.)DOC")
.def("name", &jit::FunctionInfo::FunctionName)
.def("input_names", &jit::FunctionInfo::InputArgNames)
.def("output_names", &jit::FunctionInfo::OutputArgNames);
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) {
return paddle::jit::Load(path, cuda_place);
});
}
} // namespace pybind
} // namespace paddle
/* 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. */
#pragma once
#include <Python.h>
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
namespace paddle {
namespace pybind {
void BindJit(pybind11::module* m);
} // namespace pybind
} // namespace paddle
......@@ -90,6 +90,7 @@ limitations under the License. */
#include "paddle/fluid/pybind/eager.h"
#include "paddle/fluid/pybind/imperative.h"
#include "paddle/fluid/pybind/io.h"
#include "paddle/fluid/pybind/jit.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/lod_utils.h"
#include "paddle/utils/none.h"
......@@ -563,6 +564,7 @@ PYBIND11_MODULE(core_noavx, m) {
BindEager(&m);
BindEagerStringTensor(&m);
BindCudaStream(&m);
BindJit(&m);
// Not used, just make sure cpu_info.cc is linked.
paddle::platform::CpuTotalPhysicalMemory();
......
# 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.jit.layer import Layer
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 = Layer()
jit_layer.load(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()
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2021 NVIDIA Corporation. 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.
from paddle.fluid.core import Load
class Layer(object):
def __init__(self):
self.cpp_layer = None
# {name: Function}
self.functions = {}
def load(self, load_path, place):
self.cpp_layer = Load(load_path, place)
function_dict = self.cpp_layer.function_dict()
for name, function in function_dict.items():
self.functions[name] = Function(function)
setattr(self, name, self.functions[name])
class Function():
def __init__(self, function):
self.function = function
self.info = FunctionInfo(function.info())
def __call__(self, *args):
return self.function(args)
class FunctionInfo():
def __init__(self, info):
self.info = info
def name(self):
return self.info.name()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册