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 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
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
20 changes: 16 additions & 4 deletions paddle/fluid/jit/compilation_unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion paddle/fluid/jit/compilation_unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace paddle {
namespace jit {
using Name2FunctionMap =
std::unordered_map<std::string, std::shared_ptr<BaseFunction>>;

class CompilationUnit {
public:
Expand All @@ -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
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
8 changes: 8 additions & 0 deletions paddle/fluid/jit/layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions paddle/fluid/jit/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
6 changes: 4 additions & 2 deletions 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 Expand Up @@ -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)
Expand Down
83 changes: 83 additions & 0 deletions paddle/fluid/pybind/jit.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* 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
27 changes: 27 additions & 0 deletions paddle/fluid/pybind/jit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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
2 changes: 2 additions & 0 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down
82 changes: 82 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,82 @@
# 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()
51 changes: 51 additions & 0 deletions python/paddle/jit/layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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()