Skip to content

Commit

Permalink
Develop xiaobingw (PaddlePaddle#17)
Browse files Browse the repository at this point in the history
* apply lint

* add ipu_build_strategy

* del unused code

* add popart_canonicalization_utils to ipu_backend deps

* Update compiler.py

* Update ipu_build_strategy.cc
  • Loading branch information
XBWGC committed Aug 4, 2021
1 parent cc6895e commit fedeabf
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 3 deletions.
6 changes: 4 additions & 2 deletions paddle/fluid/framework/ipu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ set(POPART_CANONICALIZATION_HANDLERS_SRC
"popart_canonicalization/other_ops.cpp"
)
# TODO(alleng) build static library
cc_library(popart_canonicalization_utils SHARED SRCS popart_canonicalization_utils.cc
cc_library(popart_canonicalization_utils SRCS popart_canonicalization_utils.cc
${POPART_CANONICALIZATION_HANDLERS_SRC} DEPS framework_proto enforce)

cc_library(ipu_utils SRCS ipu_utils.cc DEPS memory framework_proto popart)
cc_library(ipu_backend SRCS ipu_backend.cc DEPS popart graph framework_proto enforce ipu_utils)
cc_library(ipu_build_strategy SRCS ipu_build_strategy.cc DEPS popart graph framework_proto enforce)
cc_library(ipu_backend SRCS ipu_backend.cc DEPS popart graph framework_proto enforce ipu_utils
ipu_build_strategy popart_canonicalization_utils)
6 changes: 5 additions & 1 deletion paddle/fluid/framework/ipu/ipu_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ limitations under the License. */
#include <unordered_set>
#include <vector>

#include "paddle/fluid/framework/ipu/ipu_build_strategy.h"
#include "paddle/fluid/framework/feed_fetch_type.h"
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/lod_tensor.h"
Expand All @@ -38,6 +39,8 @@ limitations under the License. */
namespace paddle {
namespace framework {

using ipu::IpuBuildStrategy;

struct Optimizer {
std::string type;
// as far as we know, attr is usually float
Expand Down Expand Up @@ -84,6 +87,7 @@ class IpuBackend {

private:
Optimizer optimizer_;
IpuBuildStrategy ipu_build_strategy_;

std::vector<popart::TensorId> inputs_;
std::vector<popart::TensorId> outputs_;
Expand All @@ -97,4 +101,4 @@ class IpuBackend {
};

} // namespace framework
} // namespace paddle
} // namespace paddle
27 changes: 27 additions & 0 deletions paddle/fluid/framework/ipu/ipu_build_strategy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (c) 2021 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/ipu/ipu_build_strategy.h"
#include "paddle/fluid/framework/ir/ipu/ipu_graph_builder_pass.h"

#include <glog/logging.h>
#include "paddle/fluid/framework/ir/graph_printer.h"

namespace paddle {
namespace framework {
namespace ipu {

}
}
}
44 changes: 44 additions & 0 deletions paddle/fluid/framework/ipu/ipu_build_strategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright (c) 2021 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 <memory>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

#include <popart/sessionoptions.hpp>

#include "boost/optional.hpp"
#include "paddle/fluid/framework/ir/pass_builder.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {

namespace ipu {

struct IpuBuildStrategy {
popart::SessionOptions popart_options;
};

} // namespace ipu

} // namespace framework
} // namespace paddle
7 changes: 7 additions & 0 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ limitations under the License. */

#ifdef PADDLE_WITH_IPU
#include "paddle/fluid/platform/ipu_info.h"
#include "paddle/fluid/framework/ipu/ipu_backend.h"
#endif

#ifdef PADDLE_WITH_CRYPTO
Expand Down Expand Up @@ -3202,6 +3203,12 @@ All parameter, weight, gradient are variables in Paddle.
})
.def("device_count", &ParallelExecutor::DeviceCount);

#ifdef PADDLE_WITH_IPU
py::class_<framework::IpuBackend, std::shared_ptr<framework::IpuBackend>>(m,
"IpuBackend")
.def(py::init(&IpuBackend::GetInstance));
#endif

BindFleetWrapper(&m);

#ifdef PADDLE_WITH_PSLIB
Expand Down
48 changes: 48 additions & 0 deletions python/paddle/fluid/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,51 @@ def _get_places(self, place, place_list):
place_list = cpu_places()
assert place_list, "No places for execution."
return place_list


class IpuCompiler(object):
"""
The IpuCompiler is used to transform a program to a ipu-target program.
Args:
program(framework.Program): This argument is the Program being executed.
ipu_build_strategy: This argument is used to build the program with the
specified options, such as operators' replacement, dtype, etc.
Returns:
framework.Program
"""

def __init__(self, program, ipu_build_strategy=None):
if not isinstance(program, framework.Program):
raise TypeError(
"The type of program is wrong, expected Program, but got %s" %
type(program))

self._scope = None
self._program = program
self._graph = core.Graph(program.desc)
self._ipu_build_strategy = ipu_build_strategy
self._compiled = False
self._backend = core.IpuBackend()
self._graph_passes = ["optimizer_extract_pass",
"forward_graph_extract_pass"]

def compile(self, feed_list, fetch_list, scope=None):
for pass_name in self._graph_passes:
graph_pass = core.get_pass(pass_name)
graph_pass.apply(self._graph)

ipu_graph_builder_pass = core.get_pass("ipu_graph_builder_pass")
ipu_graph_builder_pass.set("feed_list", feed_list)
ipu_graph_builder_pass.set("fetch_list", fetch_list)
ipu_graph_builder_pass.apply(self._graph)

convert_pass = core.get_pass('graph_to_program_pass')
desc = core.ProgramDesc()
convert_pass.set_not_owned('program', desc)
convert_pass.apply(self._graph)
program = framework.Program._construct_from_desc(desc)

return program

0 comments on commit fedeabf

Please sign in to comment.