parent
5368e50d84
commit
15f5f10ed5
@ -1,3 +1,4 @@
|
|||||||
cc_library(var_handle SRCS var_handle.cc DEPS place)
|
cc_library(var_handle SRCS var_handle.cc DEPS place)
|
||||||
cc_library(op_handle_base SRCS op_handle_base.cc DEPS var_handle device_context)
|
cc_library(op_handle_base SRCS op_handle_base.cc DEPS var_handle device_context)
|
||||||
cc_library(scale_loss_grad_op_handle SRCS scale_loss_grad_op_handle.cc DEPS op_handle_base scope lod_tensor ddim memory)
|
cc_library(scale_loss_grad_op_handle SRCS scale_loss_grad_op_handle.cc DEPS op_handle_base scope lod_tensor ddim memory)
|
||||||
|
cc_library(fetch_op_handle SRCS fetch_op_handle.cc DEPS op_handle_base scope lod_tensor ddim memory)
|
||||||
|
@ -0,0 +1,77 @@
|
|||||||
|
// Copyright (c) 2018 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/details/fetch_op_handle.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
FetchOpHandle::FetchOpHandle(FeedFetchList *data, size_t offset,
|
||||||
|
std::vector<Scope *> *local_scopes)
|
||||||
|
: data_(data), offset_(offset), local_scopes_(local_scopes) {}
|
||||||
|
|
||||||
|
FetchOpHandle::~FetchOpHandle() {
|
||||||
|
for (auto *input_var : inputs_) {
|
||||||
|
input_var->pending_ops_.erase(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FetchOpHandle::Wait(platform::DeviceContext *waited_dev) {
|
||||||
|
PADDLE_THROW("Nobody should wait FetchOp. Unexpceted Error");
|
||||||
|
}
|
||||||
|
|
||||||
|
void FetchOpHandle::WaitAndMergeCPUTensors() const {
|
||||||
|
// Wait fetch stream done.
|
||||||
|
for (auto &ctx : dev_ctx_) {
|
||||||
|
ctx.second->Wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<const LoDTensor *> tensors_ptr;
|
||||||
|
tensors_ptr.reserve(tensors_.size());
|
||||||
|
for (auto &t : tensors_) {
|
||||||
|
tensors_ptr.emplace_back(&t);
|
||||||
|
}
|
||||||
|
data_->at(offset_).MergeLoDTensor(tensors_ptr, platform::CPUPlace());
|
||||||
|
}
|
||||||
|
|
||||||
|
void FetchOpHandle::RunImpl() {
|
||||||
|
for (auto *input : inputs_) {
|
||||||
|
auto *var = static_cast<VarHandle *>(input);
|
||||||
|
var->generated_op_->Wait(this->dev_ctx_[var->place_]);
|
||||||
|
}
|
||||||
|
|
||||||
|
tensors_.resize(inputs_.size());
|
||||||
|
auto *var = static_cast<VarHandle *>(inputs_[0]);
|
||||||
|
auto &var_name = var->name_;
|
||||||
|
platform::CPUPlace cpu;
|
||||||
|
auto &scopes = *local_scopes_;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < scopes.size(); ++i) {
|
||||||
|
auto &scope = scopes[i];
|
||||||
|
auto &t = scope->FindVar(var_name)->Get<framework::LoDTensor>();
|
||||||
|
if (platform::is_gpu_place(var->place_)) {
|
||||||
|
#ifdef PADDLE_WITH_CUDA
|
||||||
|
TensorCopy(t, cpu, *dev_ctx_[t.place()], &tensors_[i]);
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
tensors_[i].ShareDataWith(t);
|
||||||
|
tensors_[i].set_lod(t.lod());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace framework
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,47 @@
|
|||||||
|
// Copyright (c) 2018 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 "paddle/fluid/framework/details/op_handle_base.h"
|
||||||
|
#include "paddle/fluid/framework/feed_fetch_type.h"
|
||||||
|
#include "paddle/fluid/framework/scope.h"
|
||||||
|
#include "paddle/fluid/platform/device_context.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
struct FetchOpHandle : public OpHandleBase {
|
||||||
|
FeedFetchList *data_;
|
||||||
|
size_t offset_;
|
||||||
|
std::vector<Scope *> *local_scopes_;
|
||||||
|
std::vector<LoDTensor> tensors_;
|
||||||
|
|
||||||
|
FetchOpHandle(FeedFetchList *data, size_t offset,
|
||||||
|
std::vector<Scope *> *local_scopes);
|
||||||
|
|
||||||
|
~FetchOpHandle();
|
||||||
|
|
||||||
|
void Wait(platform::DeviceContext *waited_dev) override;
|
||||||
|
|
||||||
|
void WaitAndMergeCPUTensors() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void RunImpl() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace framework
|
||||||
|
} // namespace paddle
|
Loading…
Reference in new issue