diff --git a/.gitignore b/.gitignore index 36d0eea5f7834..349576c3e1811 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,10 @@ cmake-build-* paddle/fluid/operators/distributed/send_recv.proto model_test -scripts \ No newline at end of file +python/paddle/distributed/fleet/proto/ +python/paddle/fluid/core*.so + +scripts +sdk/ +demos/ +*.onnx \ No newline at end of file diff --git a/paddle/fluid/framework/ipu/CMakeLists.txt b/paddle/fluid/framework/ipu/CMakeLists.txt index ee0c560be4a74..05a8ea313b329 100644 --- a/paddle/fluid/framework/ipu/CMakeLists.txt +++ b/paddle/fluid/framework/ipu/CMakeLists.txt @@ -1,2 +1,2 @@ -cc_library(ipu_backend SRCS ipu_backend.cc DEPS popart graph framework_proto enforce ) -# cc_test(ipu_backend_test SRCS ipu_backend_test.cc DEPS ipu_backend) \ No newline at end of file +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) diff --git a/paddle/fluid/framework/ipu/ipu_backend.cc b/paddle/fluid/framework/ipu/ipu_backend.cc index f96e862c58b35..450148c19cc40 100644 --- a/paddle/fluid/framework/ipu/ipu_backend.cc +++ b/paddle/fluid/framework/ipu/ipu_backend.cc @@ -29,6 +29,7 @@ limitations under the License. */ #include "paddle/fluid/framework/feed_fetch_type.h" #include "paddle/fluid/framework/framework.pb.h" +#include "paddle/fluid/framework/ipu/ipu_utils.h" #include "paddle/fluid/framework/ir/graph.h" #include "paddle/fluid/framework/ir/node.h" #include "paddle/fluid/framework/lod_tensor.h" @@ -39,37 +40,6 @@ namespace framework { std::shared_ptr IpuBackend::instance_ = nullptr; -popart::DataType toPopartType(proto::VarType::Type type) { - switch (type) { - case proto::VarType::UINT8: - return popart::DataType::UINT8; - case proto::VarType::INT8: - return popart::DataType::INT8; - case proto::VarType::INT16: - return popart::DataType::INT16; - case proto::VarType::INT32: - return popart::DataType::INT32; - case proto::VarType::INT64: - return popart::DataType::INT64; - case proto::VarType::BOOL: - return popart::DataType::BOOL; - case proto::VarType::FP32: - return popart::DataType::FLOAT; - case proto::VarType::FP16: - return popart::DataType::FLOAT16; - case proto::VarType::BF16: - return popart::DataType::BFLOAT16; - case proto::VarType::COMPLEX64: - return popart::DataType::COMPLEX64; - case proto::VarType::COMPLEX128: - return popart::DataType::COMPLEX128; - - default: - PADDLE_THROW( - platform::errors::Unavailable("Unsupported Paddle var type.")); - } -} - IpuBackend::IpuBackend() { builder_ = popart::Builder::create(); } void IpuBackend::Compile(ir::Graph* graph, @@ -85,7 +55,7 @@ void IpuBackend::Compile(ir::Graph* graph, if (feed_name == var_desc->Name()) { // Get tensor_info from var_desc VLOG(1) << "feed_name= " << var_desc->Name(); - popart::DataType data_type = toPopartType(var_desc->GetDataType()); + auto data_type = VarType2PopartType(var_desc->GetDataType()); popart::TensorInfo input_info{data_type, var_desc->GetShape()}; // Create popart tensor VLOG(1) << "popart input_info = " << input_info; @@ -126,9 +96,9 @@ void IpuBackend::Compile(ir::Graph* graph, for (const auto& fetch_name : fetch_list) { auto tensor = tensors_.find(fetch_name); - PADDLE_ENFORCE_NE( - tensor, tensors_.end(), - platform::errors::NotFound("output tensor %s does not exist.", fetch_name)); + PADDLE_ENFORCE_NE(tensor, tensors_.end(), + platform::errors::NotFound( + "output tensor %s does not exist.", fetch_name)); VLOG(1) << "fetch_name= " << fetch_name; VLOG(1) << "popart output tensor id = " << tensor->second; @@ -165,8 +135,8 @@ void IpuBackend::Compile(ir::Graph* graph, VLOG(1) << "Preparing session device...done"; } -void IpuBackend::Run(const std::vector &inputs, - std::vector &outputs) { +void IpuBackend::Run(const std::vector& inputs, + std::vector& outputs) { // Prepare input tensor std::map popart_inputs; std::map> input_wrappers; @@ -175,7 +145,8 @@ void IpuBackend::Run(const std::vector &inputs, auto tensor_id = inputs_[i]; const Tensor* tensor = inputs[i]; std::vector tensor_shape = builder_->getTensorShape(tensor_id); - popart::NDArrayWrapper data(const_cast(tensor->data()), tensor_shape); + popart::NDArrayWrapper data( + const_cast(tensor->data()), tensor_shape); VLOG(1) << "Preparing Input data for tensor " << tensor_id; input_wrappers.emplace(tensor_id, std::move(data)); popart_inputs.emplace(tensor_id, input_wrappers.at(tensor_id)); @@ -184,11 +155,12 @@ void IpuBackend::Run(const std::vector &inputs, // Prepare output tensor std::map popart_anchors; std::map> anchor_wrappers; - for(size_t i = 0; i < outputs.size(); i++) { + for (size_t i = 0; i < outputs.size(); i++) { auto tensor_id = outputs_[i]; Tensor* tensor = outputs[i]; std::vector tensor_shape = builder_->getTensorShape(tensor_id); - popart::NDArrayWrapper data(const_cast(tensor->data()), tensor_shape); + popart::NDArrayWrapper data( + const_cast(tensor->data()), tensor_shape); VLOG(1) << "Preparing Output data for tensor " << tensor_id; anchor_wrappers.emplace(tensor_id, std::move(data)); popart_anchors.emplace(tensor_id, anchor_wrappers.at(tensor_id)); diff --git a/paddle/fluid/framework/ipu/ipu_backend.h b/paddle/fluid/framework/ipu/ipu_backend.h index 0d6f3322ff8b3..f409e4003950d 100644 --- a/paddle/fluid/framework/ipu/ipu_backend.h +++ b/paddle/fluid/framework/ipu/ipu_backend.h @@ -66,6 +66,10 @@ class IpuBackend { optimizer_.attrs[attr] = value; } + std::vector GetTensorShape(const std::string& var_name) { + return builder_->getTensorShape(tensors_[var_name]); + } + static std::shared_ptr GetInstance() { if (NULL == instance_) { instance_.reset(new IpuBackend()); diff --git a/paddle/fluid/framework/ipu/ipu_utils.cc b/paddle/fluid/framework/ipu/ipu_utils.cc new file mode 100644 index 0000000000000..760e860184776 --- /dev/null +++ b/paddle/fluid/framework/ipu/ipu_utils.cc @@ -0,0 +1,51 @@ +/* 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_utils.h" + +namespace paddle { +namespace framework { + +popart::DataType VarType2PopartType(proto::VarType::Type type) { + switch (type) { + case proto::VarType::UINT8: + return popart::DataType::UINT8; + case proto::VarType::INT8: + return popart::DataType::INT8; + case proto::VarType::INT16: + return popart::DataType::INT16; + case proto::VarType::INT32: + return popart::DataType::INT32; + case proto::VarType::INT64: + return popart::DataType::INT64; + case proto::VarType::BOOL: + return popart::DataType::BOOL; + case proto::VarType::FP32: + return popart::DataType::FLOAT; + case proto::VarType::FP16: + return popart::DataType::FLOAT16; + case proto::VarType::BF16: + return popart::DataType::BFLOAT16; + case proto::VarType::COMPLEX64: + return popart::DataType::COMPLEX64; + case proto::VarType::COMPLEX128: + return popart::DataType::COMPLEX128; + default: + PADDLE_THROW(paddle::platform::errors::Unavailable( + "Unsupported Paddle var type.")); + } +} + +} // namespace framework +} // namespace paddle \ No newline at end of file diff --git a/paddle/fluid/framework/ipu/ipu_utils.h b/paddle/fluid/framework/ipu/ipu_utils.h new file mode 100644 index 0000000000000..12da7bb5487b7 --- /dev/null +++ b/paddle/fluid/framework/ipu/ipu_utils.h @@ -0,0 +1,55 @@ +/* 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 +#include +#include +#include + +#include "paddle/fluid/framework/framework.pb.h" +#include "paddle/fluid/framework/lod_tensor.h" + +namespace paddle { +namespace framework { + +popart::DataType VarType2PopartType(proto::VarType::Type type); + +template +std::unique_ptr> Tensor2IArray(Tensor &tensor) { + auto dtype = VarType2PopartType(tensor.type()); + auto shape = std::vector(); + for (size_t i = 0; i < tensor.dims().size(); ++i) { + shape.push_back(tensor.dims().at(i)); + } + popart::TensorInfo tensor_info(dtype, shape); + + return std::make_unique>( + reinterpret_cast(tensor.data()), tensor_info); +} + +template +std::unique_ptr> LoDTensor2IArray( + LoDTensor &lod_tensor) { + if (lod_tensor.lod().size() == 0) { + return Tensor2IArray(lod_tensor); + } else { + PADDLE_THROW( + platform::errors::Unimplemented("LoDTensor2IArray is Unimplemented")); + } +} + +} // namespace framework +} // namespace paddle \ No newline at end of file diff --git a/paddle/fluid/operators/ipu_runtime_op.h b/paddle/fluid/operators/ipu_runtime_op.h index c951af05295d1..08718ae315601 100644 --- a/paddle/fluid/operators/ipu_runtime_op.h +++ b/paddle/fluid/operators/ipu_runtime_op.h @@ -15,6 +15,7 @@ #pragma once #include #include + #include "paddle/fluid/framework/op_registry.h" #ifdef PADDLE_WITH_IPU #include "paddle/fluid/framework/ipu/ipu_backend.h" @@ -27,14 +28,17 @@ namespace operators { template class IpuRuntimeKernel : public framework::OpKernel { public: - void Compute(const framework::ExecutionContext &ctx) const override { + void Compute(const framework::ExecutionContext& ctx) const override { #ifdef PADDLE_WITH_IPU auto ipu_backend = paddle::framework::IpuBackend::GetInstance(); VLOG(4) << "IpuRuntime Kernel, begin to run graph"; auto inputs = ctx.MultiInput("FeedList"); auto outputs = ctx.MultiOutput("FetchList"); - for (auto* out : outputs){ - out->Resize(framework::make_ddim({1})); + auto output_names = ctx.OutputNames("FetchList"); + for (size_t i = 0; i < outputs.size(); ++i) { + auto* out = outputs[i]; + auto oshape = ipu_backend->GetTensorShape(output_names[i]); + out->Resize(framework::make_ddim(oshape)); out->mutable_data(ctx.GetPlace()); } ipu_backend->Run(inputs, outputs);