未验证 提交 aa0f254f 编写于 作者: C Chen Weihang 提交者: GitHub

Add macro BOOST_GET to enrich the error information of boost :: get (#24175)

* add new macro BOOST_GET_SAFELY & unittests, test=develop

* add different macro type, test=develop

* fix get macro type in executor, test=develop

* four macro part change backup

* using one macro for all case, test=develop

* revert attribute change, test=develop

* change to three func to solve gcc4.8 bug, test=develop

* polish some details, test=develop
上级 9c073bbd
......@@ -60,10 +60,10 @@ struct ExtractAttribute<bool> {
bool* operator()(Attribute& attr) const {
if (attr.type() == typeid(int)) { // NOLINT
int val = boost::get<int>(attr);
int val = BOOST_GET_CONST(int, attr);
attr = static_cast<bool>(val);
} else if (attr.type() == typeid(float)) { // NOLINT
float val = boost::get<float>(attr);
float val = BOOST_GET_CONST(float, attr);
attr = static_cast<bool>(val);
}
bool* attr_value = nullptr;
......@@ -86,10 +86,10 @@ struct ExtractAttribute<int64_t> {
int64_t* operator()(Attribute& attr) const {
if (attr.type() == typeid(int)) { // NOLINT
int val = boost::get<int>(attr);
int val = BOOST_GET_CONST(int, attr);
attr = static_cast<int64_t>(val);
} else if (attr.type() == typeid(float)) { // NOLINT
int val = boost::get<float>(attr);
int val = BOOST_GET_CONST(float, attr);
attr = static_cast<int64_t>(val);
}
int64_t* attr_value = nullptr;
......@@ -112,11 +112,11 @@ struct ExtractAttribute<std::vector<int64_t>> {
std::vector<int64_t>* operator()(Attribute& attr) const {
if (attr.type() == typeid(std::vector<int>)) { // NOLINT
std::vector<int> val = boost::get<std::vector<int>>(attr);
std::vector<int> val = BOOST_GET_CONST(std::vector<int>, attr);
std::vector<int64_t> vec(val.begin(), val.end());
attr = vec;
} else if (attr.type() == typeid(std::vector<float>)) { // NOLINT
std::vector<float> val = boost::get<std::vector<float>>(attr);
std::vector<float> val = BOOST_GET_CONST(std::vector<float>, attr);
std::vector<int64_t> vec(val.begin(), val.end());
attr = vec;
}
......@@ -140,10 +140,10 @@ struct ExtractAttribute<float> {
float* operator()(Attribute& attr) const {
if (attr.type() == typeid(int)) { // NOLINT
int val = boost::get<int>(attr);
int val = BOOST_GET_CONST(int, attr);
attr = static_cast<float>(val);
} else if (attr.type() == typeid(int64_t)) { // NOLINT
int64_t val = boost::get<int64_t>(attr);
int64_t val = BOOST_GET_CONST(int64_t, attr);
attr = static_cast<float>(val);
}
float* attr_value = nullptr;
......
......@@ -182,7 +182,7 @@ void AllReduceOpHandle::NCCLAllReduceFunc(
void AllReduceOpHandle::SyncNCCLAllReduce() {
if (FLAGS_sync_nccl_allreduce) {
for (auto &p : places_) {
int dev_id = boost::get<platform::CUDAPlace>(p).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, p).device;
auto *nccl_ctxs =
nccl_ctxs_->GetRunEnvNCCLCtx(run_order_, use_hierarchical_allreduce_);
auto &nccl_ctx = nccl_ctxs->at(dev_id);
......
......@@ -54,21 +54,22 @@ void ProcessGraph(std::vector<ir::Graph *> graphs, Scope *scope) {
if (node && node->IsOp()) {
if (node->Name() == "send") {
auto send_var_name = node->Op()->Input("X")[0];
auto send_varnames = boost::get<std::vector<std::string>>(
node->Op()->GetNullableAttr("send_varnames"));
auto epmap = boost::get<std::vector<std::string>>(
node->Op()->GetNullableAttr("epmap"));
auto height_section = boost::get<std::vector<int64_t>>(
node->Op()->GetNullableAttr("sections"));
auto send_varnames =
BOOST_GET_CONST(std::vector<std::string>,
node->Op()->GetNullableAttr("send_varnames"));
auto epmap = BOOST_GET_CONST(std::vector<std::string>,
node->Op()->GetNullableAttr("epmap"));
auto height_section = BOOST_GET_CONST(
std::vector<int64_t>, node->Op()->GetNullableAttr("sections"));
auto trainer_id =
boost::get<int>(node->Op()->GetNullableAttr("trainer_id"));
BOOST_GET_CONST(int, node->Op()->GetNullableAttr("trainer_id"));
auto merge_add =
boost::get<bool>(node->Op()->GetNullableAttr("merge_add"));
BOOST_GET_CONST(bool, node->Op()->GetNullableAttr("merge_add"));
if (!merge_add) {
merge_add = FLAGS_communicator_is_sgd_optimizer;
}
auto use_send_handler =
boost::get<bool>(node->Op()->GetNullableAttr("use_send_handler"));
auto use_send_handler = BOOST_GET_CONST(
bool, node->Op()->GetNullableAttr("use_send_handler"));
send_varname_to_ctx[send_var_name] = operators::distributed::RpcContext(
send_var_name, send_varnames, epmap, height_section, trainer_id,
merge_add, use_send_handler);
......@@ -198,16 +199,16 @@ FetchResultType AsyncSSAGraphExecutor::Run(
HandleException();
FetchList ret;
auto &val = boost::get<FetchList>(fetch_data);
auto &val = BOOST_GET(FetchList, fetch_data);
for (size_t fetch_idx = 0; fetch_idx < fetch_tensors.size(); ++fetch_idx) {
if (data_is_lod_tensor(val.at(fetch_idx))) {
std::vector<const LoDTensor *> lodtensor_ptrs;
lodtensor_ptrs.push_back(&(boost::get<LoDTensor>(val.at(fetch_idx))));
lodtensor_ptrs.push_back(&(BOOST_GET(LoDTensor, val.at(fetch_idx))));
LoDTensor var;
var.MergeLoDTensor(lodtensor_ptrs, platform::CPUPlace());
ret.emplace_back(var);
} else {
auto array = boost::get<LoDTensorArray>(val.at(fetch_idx));
auto array = BOOST_GET(LoDTensorArray, val.at(fetch_idx));
LoDTensorArray item_array;
item_array.reserve(array.size());
for (size_t i = 0; i < array.size(); ++i) {
......
......@@ -75,7 +75,8 @@ void BroadcastOpHandle::BroadcastOneVar(
} else {
#if defined(PADDLE_WITH_NCCL)
VarHandle *out_handle = nullptr;
int root_id = boost::get<platform::CUDAPlace>(in_tensor.place()).device;
int root_id =
BOOST_GET_CONST(platform::CUDAPlace, in_tensor.place()).device;
std::vector<std::function<void()>> broadcast_calls;
int type = platform::ToNCCLDataType(in_tensor.type());
......@@ -86,7 +87,7 @@ void BroadcastOpHandle::BroadcastOneVar(
->FindVar(out_var_handle->name());
int dst_id =
boost::get<platform::CUDAPlace>(out_var_handle->place()).device;
BOOST_GET_CONST(platform::CUDAPlace, out_var_handle->place()).device;
auto &nccl_ctx = nccl_ctxs_->at(dst_id);
......
......@@ -46,7 +46,7 @@ EagerDeletionOpHandle::EagerDeletionOpHandle(
platform::DeviceContextPool::Instance().Get(place));
if (dynamic_cast<StreamGarbageCollector *>(gc_)) {
platform::CUDADeviceGuard guard(
boost::get<platform::CUDAPlace>(place).device);
BOOST_GET_CONST(platform::CUDAPlace, place).device);
PADDLE_ENFORCE(cudaEventCreateWithFlags(&event_, cudaEventDisableTiming));
PADDLE_ENFORCE_NOT_NULL(event_);
}
......@@ -62,7 +62,7 @@ EagerDeletionOpHandle::EagerDeletionOpHandle(
EagerDeletionOpHandle::~EagerDeletionOpHandle() {
#ifdef PADDLE_WITH_CUDA
if (event_) {
auto gpu_place = boost::get<platform::CUDAPlace>(dev_ctx_->GetPlace());
auto gpu_place = BOOST_GET_CONST(platform::CUDAPlace, dev_ctx_->GetPlace());
platform::CUDADeviceGuard guard(gpu_place.device);
PADDLE_ENFORCE(cudaEventDestroy(event_));
}
......@@ -72,7 +72,7 @@ EagerDeletionOpHandle::~EagerDeletionOpHandle() {
void EagerDeletionOpHandle::InitCUDA() {
#ifdef PADDLE_WITH_CUDA
int dev_id =
boost::get<platform::CUDAPlace>(dev_ctxes_.begin()->first).device;
BOOST_GET_CONST(platform::CUDAPlace, dev_ctxes_.begin()->first).device;
events_[dev_id] = nullptr;
#endif
}
......
......@@ -68,22 +68,22 @@ static void CheckDims(const framework::DDim &tensor_dims,
void FetchOpHandle::WaitAndMergeCPUFetchVars() const {
if (return_merged_) {
if (data_is_lod_tensor(tensors_[0])) {
const auto &tensor_dims = boost::get<LoDTensor>(tensors_[0]).dims();
const auto &tensor_dims = BOOST_GET_CONST(LoDTensor, tensors_[0]).dims();
for (size_t i = 1; i < tensors_.size(); i++) {
const auto &ele_dims = boost::get<LoDTensor>(tensors_[i]).dims();
const auto &ele_dims = BOOST_GET_CONST(LoDTensor, tensors_[i]).dims();
CheckDims(tensor_dims, ele_dims, offset_);
}
std::vector<const LoDTensor *> tensors_ptr;
tensors_ptr.reserve(tensors_.size());
for (auto &t : tensors_) {
tensors_ptr.emplace_back(&boost::get<LoDTensor>(t));
tensors_ptr.emplace_back(&BOOST_GET_CONST(LoDTensor, t));
}
auto &val = boost::get<FetchList>(*data_);
auto &val = BOOST_GET(FetchList, *data_);
LoDTensor var;
var.MergeLoDTensor(tensors_ptr, platform::CPUPlace());
val.at(offset_) = std::move(var);
} else {
auto &array = boost::get<LoDTensorArray>(tensors_[0]);
auto &array = BOOST_GET_CONST(LoDTensorArray, tensors_[0]);
LoDTensorArray tmp_array;
tmp_array.reserve(array.size());
for (size_t i = 0; i < array.size(); ++i) {
......@@ -92,7 +92,7 @@ void FetchOpHandle::WaitAndMergeCPUFetchVars() const {
tensors_ptr.reserve(tensors_.size());
tensors_ptr.push_back(&array[i]);
for (size_t j = 1; j < tensors_.size(); ++j) {
auto &element = boost::get<LoDTensorArray>(tensors_[j]);
auto &element = BOOST_GET_CONST(LoDTensorArray, tensors_[j]);
const auto &ele_dims = element[i].dims();
CheckDims(tensor_dims, ele_dims, offset_);
tensors_ptr.push_back(&element[i]);
......@@ -100,11 +100,11 @@ void FetchOpHandle::WaitAndMergeCPUFetchVars() const {
tmp_array.emplace_back();
tmp_array.back().MergeLoDTensor(tensors_ptr, platform::CPUPlace());
}
auto &val = boost::get<FetchList>(*data_);
auto &val = BOOST_GET(FetchList, *data_);
val.at(offset_) = std::move(tmp_array);
}
} else {
auto &val = boost::get<FetchUnmergedList>(*data_);
auto &val = BOOST_GET(FetchUnmergedList, *data_);
val.at(offset_) = std::move(tensors_);
}
}
......@@ -142,13 +142,13 @@ void FetchOpHandle::RunImpl() {
if (var->IsType<LoDTensor>()) {
auto &t = var->Get<framework::LoDTensor>();
auto &item = boost::get<LoDTensor>(tensors_[i]);
auto &item = BOOST_GET(LoDTensor, tensors_[i]);
TransData(t, &item);
} else {
auto &t = var->Get<framework::LoDTensorArray>();
LoDTensorArray tmp(t.size());
tensors_[i] = tmp;
auto &item = boost::get<LoDTensorArray>(tensors_[i]);
auto &item = BOOST_GET(LoDTensorArray, tensors_[i]);
for (size_t j = 0; j < t.size(); ++j) {
TransData(t[j], &item[j]);
}
......
......@@ -84,7 +84,7 @@ inline bool IsOpRole(const OpDesc &op, OpRole role) {
const auto &attrs = op.GetAttrMap();
auto iter = attrs.find(OpProtoAndCheckerMaker::OpRoleAttrName());
if (iter == attrs.end()) return false;
return static_cast<bool>(boost::get<int>(iter->second) &
return static_cast<bool>(BOOST_GET_CONST(int, iter->second) &
static_cast<int>(role));
}
......@@ -92,13 +92,13 @@ inline std::vector<std::string> GetOpRoleVarsOrEmpty(const OpDesc &op) {
const auto &attrs = op.GetAttrMap();
auto iter = attrs.find(OpProtoAndCheckerMaker::OpRoleVarAttrName());
if (iter == attrs.end()) return {};
auto &ret = boost::get<std::vector<std::string>>(iter->second);
auto &ret = BOOST_GET_CONST(std::vector<std::string>, iter->second);
PADDLE_ENFORCE_EQ(
ret.size() % 2, 0,
platform::errors::InvalidArgument(
"The size of attribute %s must be an even number, but got %d",
OpProtoAndCheckerMaker::OpRoleVarAttrName(), ret.size()));
return boost::get<std::vector<std::string>>(iter->second);
return BOOST_GET_CONST(std::vector<std::string>, iter->second);
}
bool IsDataParallelInferenceGraph(const ir::Graph &graph);
......
......@@ -122,7 +122,7 @@ void TensorCheckerVisitor<platform::CUDADeviceContext>::apply(
auto* dev_ctx = reinterpret_cast<platform::CUDADeviceContext*>(
platform::DeviceContextPool::Instance().Get(tensor_.place()));
int dev_id = boost::get<platform::CUDAPlace>(tensor_.place()).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, tensor_.place()).device;
PADDLE_ENFORCE_EQ(
(dev_id >= 0 && dev_id < multi_op_var2gpu_str_mutex().size()), true,
platform::errors::OutOfRange("GPU dev_id must >=0 and < dev_count=%d",
......
......@@ -83,7 +83,7 @@ class NCCLOpHandleBase : public OpHandleBase {
}
for (auto& p : dev_ctxes_) {
int dev_id = boost::get<platform::CUDAPlace>(p.first).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, p.first).device;
if (inter_events_.find(dev_id) != inter_events_.end()) {
continue;
}
......@@ -104,7 +104,7 @@ class NCCLOpHandleBase : public OpHandleBase {
ncclRedOp_t op) {
PADDLE_ENFORCE(run_order_ >= 0, "run_order must > 0");
auto flat_nccl_ctxs = nccl_ctxs_->GetFlatCtx(run_order_);
int dev_id = boost::get<platform::CUDAPlace>(place).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, place).device;
auto& nccl_ctx = flat_nccl_ctxs->at(dev_id);
auto stream = nccl_ctx.stream();
auto comm = nccl_ctx.comm_;
......@@ -146,7 +146,7 @@ class NCCLOpHandleBase : public OpHandleBase {
void InterReduce(platform::Place place, const void* sendbuff, void* recvbuff,
size_t count, ncclDataType_t datatype, ncclRedOp_t op) {
auto nccl_ctxs = nccl_ctxs_->GetHierarchicalInterCtx(run_order_);
int dev_id = boost::get<platform::CUDAPlace>(place).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, place).device;
auto& nccl_ctx = nccl_ctxs->at(dev_id);
auto stream = nccl_ctx.stream();
auto comm = nccl_ctx.comm_;
......@@ -173,7 +173,7 @@ class NCCLOpHandleBase : public OpHandleBase {
ncclRedOp_t op) {
auto nccl_ctxs = nccl_ctxs_->GetHierarchicalExterCtx(run_order_);
PADDLE_ENFORCE(nccl_ctxs_, "can't get exter %d nccl_ctxs", run_order_);
int dev_id = boost::get<platform::CUDAPlace>(place).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, place).device;
auto& nccl_ctx = nccl_ctxs->at(dev_id);
auto stream = nccl_ctx.stream();
auto comm = nccl_ctx.comm_;
......@@ -199,7 +199,7 @@ class NCCLOpHandleBase : public OpHandleBase {
void InterBroadCast(platform::Place place, void* sendbuff, size_t count,
ncclDataType_t datatype, ncclRedOp_t op) {
auto nccl_ctxs = nccl_ctxs_->GetHierarchicalInterCtx(run_order_);
int dev_id = boost::get<platform::CUDAPlace>(place).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, place).device;
auto& nccl_ctx = nccl_ctxs->at(dev_id);
auto stream = nccl_ctx.stream();
auto comm = nccl_ctx.comm_;
......
......@@ -45,7 +45,7 @@ OpHandleBase::~OpHandleBase() PADDLE_MAY_THROW {
void OpHandleBase::InitCUDA() {
#ifdef PADDLE_WITH_CUDA
for (auto &p : dev_ctxes_) {
int dev_id = boost::get<platform::CUDAPlace>(p.first).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, p.first).device;
PADDLE_ENFORCE(cudaSetDevice(dev_id));
PADDLE_ENFORCE(
cudaEventCreateWithFlags(&events_[dev_id], cudaEventDisableTiming));
......@@ -55,7 +55,8 @@ void OpHandleBase::InitCUDA() {
auto *out_var_handle = dynamic_cast<VarHandle *>(out_var);
if (out_var_handle) {
int dev_id =
boost::get<platform::CUDAPlace>(out_var_handle->place()).device;
BOOST_GET_CONST(platform::CUDAPlace, out_var_handle->place())
.device;
out_var_handle->SetGenerateEvent(events_.at(dev_id));
}
}
......@@ -63,7 +64,7 @@ void OpHandleBase::InitCUDA() {
PADDLE_ENFORCE_EQ(dev_ctxes_.size(), 1UL,
"%s should have only one dev_ctx.", Name());
auto &place = dev_ctxes_.begin()->first;
int dev_id = boost::get<platform::CUDAPlace>(place).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, place).device;
for (auto &out_var : outputs_) {
auto *out_var_handle = dynamic_cast<VarHandle *>(out_var);
if (out_var_handle) {
......@@ -192,7 +193,7 @@ void OpHandleBase::RunAndRecordEvent(const std::function<void()> &callback) {
#ifdef PADDLE_WITH_CUDA
if (!events_.empty()) { // Use event
for (auto &p : dev_ctxes_) {
auto dev_id = boost::get<platform::CUDAPlace>(p.first).device;
auto dev_id = BOOST_GET_CONST(platform::CUDAPlace, p.first).device;
auto *cuda_dev_ctx = static_cast<platform::CUDADeviceContext *>(p.second);
VLOG(10) << "cudadevicecontext:" << cuda_dev_ctx << ", dev_id:" << dev_id;
PADDLE_ENFORCE_CUDA_SUCCESS(
......@@ -210,8 +211,8 @@ void OpHandleBase::RunAndRecordEvent(platform::Place p,
} else {
auto *ctx = dev_ctxes_.at(p);
auto *cuda_ctx = static_cast<platform::CUDADeviceContext *>(ctx);
cuda_ctx->RecordEvent(events_.at(boost::get<platform::CUDAPlace>(p).device),
callback);
cuda_ctx->RecordEvent(
events_.at(BOOST_GET_CONST(platform::CUDAPlace, p).device), callback);
}
#else
callback();
......
......@@ -43,7 +43,7 @@ static std::vector<std::unique_ptr<ir::Graph>> SeparateMultiDevicesGraph(
for (auto &op : op_handles) {
auto &dev_ctx = op->DeviceContext();
auto &p = dev_ctx.begin()->first;
int dev_id = boost::get<platform::CUDAPlace>(p).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, p).device;
auto &dev_dummys = graphs[dev_id]->Get<GraphDepVars>(kGraphDepVars);
graphs[dev_id]->AddNode(graph->RemoveNode(op->Node()).release());
......@@ -256,13 +256,14 @@ FetchResultType ParallelSSAGraphExecutor::Run(
if (!is_valid[scope_idx]) {
continue;
}
const auto &fetch_list = boost::get<FetchList>(fetch_data[scope_idx]);
const auto &fetch_list =
BOOST_GET_CONST(FetchList, fetch_data[scope_idx]);
if (data_is_lod_tensor(fetch_list[fetch_idx])) {
lodtensor_ptrs.push_back(
&(boost::get<LoDTensor>(fetch_list[fetch_idx])));
&(BOOST_GET_CONST(LoDTensor, fetch_list[fetch_idx])));
} else {
lodtensorarray_ptrs.push_back(
&(boost::get<LoDTensorArray>(fetch_list[fetch_idx])));
&(BOOST_GET_CONST(LoDTensorArray, fetch_list[fetch_idx])));
}
}
if (lodtensor_ptrs.size() != 0) {
......@@ -295,7 +296,7 @@ FetchResultType ParallelSSAGraphExecutor::Run(
continue;
}
const auto &fetch_list =
boost::get<FetchUnmergedList>(fetch_data[scope_idx]);
BOOST_GET_CONST(FetchUnmergedList, fetch_data[scope_idx]);
PADDLE_ENFORCE_EQ(
fetch_list[fetch_idx].size(), 1,
platform::errors::Fatal("Each place must have only one fetched "
......
......@@ -271,13 +271,13 @@ void ReduceOpHandle::RunImpl() {
out_var_handle->place(), pre_in.type());
auto out_p = out_var_handle->place();
int root_id = boost::get<platform::CUDAPlace>(out_p).device;
int root_id = BOOST_GET_CONST(platform::CUDAPlace, out_p).device;
std::vector<std::function<void()>> all_reduce_calls;
for (size_t i = 0; i < var_scopes.size(); ++i) {
auto &p = in_places[i];
auto &lod_tensor = *lod_tensors[i];
int dev_id = boost::get<platform::CUDAPlace>(p).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, p).device;
auto &nccl_ctx = nccl_ctxs_->at(dev_id);
void *buffer = const_cast<void *>(lod_tensor.data<void>());
......
......@@ -54,7 +54,7 @@ struct ScaleLossGradFunctor {
#ifdef PADDLE_WITH_CUDA
OutT cast_coeff = static_cast<OutT>(coeff_);
auto stream = static_cast<platform::CUDADeviceContext *>(ctx_)->stream();
memory::Copy(boost::get<platform::CUDAPlace>(place_), out_data,
memory::Copy(BOOST_GET_CONST(platform::CUDAPlace, place_), out_data,
platform::CPUPlace(), &cast_coeff, SizeOfType(out_dtype_),
stream);
VLOG(10) << place_ << "RUN Scale loss grad op";
......
......@@ -66,7 +66,7 @@ void ShareTensorBufferOpHandle::AddReuseVarPair(
void ShareTensorBufferOpHandle::InitCUDA() {
#ifdef PADDLE_WITH_CUDA
int dev_id =
boost::get<platform::CUDAPlace>(dev_ctxes_.begin()->first).device;
BOOST_GET_CONST(platform::CUDAPlace, dev_ctxes_.begin()->first).device;
events_[dev_id] = nullptr;
#endif
}
......
......@@ -127,7 +127,7 @@ void SparseAllReduceOpHandle::RunImplEncoded() {
PADDLE_ENFORCE(in_numel / 2 == static_cast<size_t>(k));
out_numel = (out_numel == 0) ? static_cast<size_t>(out.numel()) : out_numel;
int dev_id = boost::get<platform::CUDAPlace>(place).device;
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, place).device;
auto *nccl_ctxs = nccl_ctxs_->GetRunEnvNCCLCtx(run_order_, false);
auto &nccl_ctx = nccl_ctxs->at(dev_id);
auto stream = nccl_ctx.stream();
......
......@@ -49,7 +49,7 @@ void TestMain(const platform::Place &place, uint16_t lanes) {
CHECK_EQ(0, dl_tensor.ctx.device_id);
} else if (platform::is_gpu_place(place)) {
CHECK_EQ(kDLGPU, dl_tensor.ctx.device_type);
CHECK_EQ(boost::get<platform::CUDAPlace>(place).device,
CHECK_EQ(BOOST_GET_CONST(platform::CUDAPlace, place).device,
dl_tensor.ctx.device_id);
} else if (platform::is_cuda_pinned_place(place)) {
CHECK_EQ(kDLCPUPinned, dl_tensor.ctx.device_type);
......
......@@ -452,15 +452,15 @@ void Executor::RunPartialPreparedContext(ExecutorPrepareContext* ctx,
if (platform::is_gpu_place(place_)) {
if (IsFastEagerDeletionModeEnabled()) {
gc.reset(new UnsafeFastGPUGarbageCollector(
boost::get<platform::CUDAPlace>(place_), max_memory_size));
BOOST_GET_CONST(platform::CUDAPlace, place_), max_memory_size));
} else {
gc.reset(new DefaultStreamGarbageCollector(
boost::get<platform::CUDAPlace>(place_), max_memory_size));
BOOST_GET_CONST(platform::CUDAPlace, place_), max_memory_size));
}
} else if (platform::is_cpu_place(place_)) {
#endif
gc.reset(new CPUGarbageCollector(boost::get<platform::CPUPlace>(place_),
max_memory_size));
gc.reset(new CPUGarbageCollector(
BOOST_GET_CONST(platform::CPUPlace, place_), max_memory_size));
#ifdef PADDLE_WITH_CUDA
}
#endif
......@@ -522,7 +522,7 @@ void Executor::RunPreparedContext(
for (auto* op : global_block.AllOps()) {
if (op->Type() == kFeedOpType) {
std::string feed_target_name = op->Output("Out")[0];
int idx = boost::get<int>(op->GetAttr("col"));
int idx = BOOST_GET_CONST(int, op->GetAttr("col"));
SetFeedVariable(scope, *(*feed_targets)[feed_target_name],
feed_holder_name, idx);
}
......@@ -534,7 +534,7 @@ void Executor::RunPreparedContext(
for (auto* op : global_block.AllOps()) {
if (op->Type() == kFetchOpType) {
std::string fetch_target_name = op->Input("X")[0];
int idx = boost::get<int>(op->GetAttr("col"));
int idx = BOOST_GET_CONST(int, op->GetAttr("col"));
*(*fetch_targets)[fetch_target_name] =
GetFetchVariable(*scope, fetch_holder_name, idx);
}
......
......@@ -151,7 +151,7 @@ void BoxWrapper::PullSparse(const paddle::platform::Place& place,
} else if (platform::is_gpu_place(place)) {
#if defined(PADDLE_WITH_CUDA) && !defined(_WIN32)
VLOG(3) << "Begin copy keys, key_num[" << total_length << "]";
int device_id = boost::get<platform::CUDAPlace>(place).GetDeviceId();
int device_id = BOOST_GET_CONST(platform::CUDAPlace, place).GetDeviceId();
LoDTensor& total_keys_tensor = keys_tensor[device_id];
uint64_t* total_keys = reinterpret_cast<uint64_t*>(
total_keys_tensor.mutable_data<int64_t>({total_length, 1}, place));
......@@ -224,7 +224,7 @@ void BoxWrapper::PushSparseGrad(const paddle::platform::Place& place,
"Warning:: CPUPlace is not supported in PaddleBox now."));
} else if (platform::is_gpu_place(place)) {
#if defined(PADDLE_WITH_CUDA) && !defined(_WIN32)
int device_id = boost::get<platform::CUDAPlace>(place).GetDeviceId();
int device_id = BOOST_GET_CONST(platform::CUDAPlace, place).GetDeviceId();
LoDTensor& cached_total_keys_tensor = keys_tensor[device_id];
uint64_t* total_keys =
reinterpret_cast<uint64_t*>(cached_total_keys_tensor.data<int64_t>());
......@@ -236,7 +236,7 @@ void BoxWrapper::PushSparseGrad(const paddle::platform::Place& place,
push_boxps_timer.Start();
int ret = boxps_ptr_->PushSparseGPU(
total_keys, total_grad_values_gpu, static_cast<int>(total_length),
boost::get<platform::CUDAPlace>(place).GetDeviceId());
BOOST_GET_CONST(platform::CUDAPlace, place).GetDeviceId());
PADDLE_ENFORCE_EQ(ret, 0, platform::errors::PreconditionNotMet(
"PushSparseGPU failed in BoxPS."));
push_boxps_timer.Pause();
......
......@@ -116,7 +116,7 @@ void BoxWrapper::CopyForPull(const paddle::platform::Place& place,
const int64_t total_length) {
auto stream = dynamic_cast<platform::CUDADeviceContext*>(
platform::DeviceContextPool::Instance().Get(
boost::get<platform::CUDAPlace>(place)))
BOOST_GET_CONST(platform::CUDAPlace, place)))
->stream();
auto buf_value = memory::AllocShared(place, values.size() * sizeof(float*));
float** gpu_values = reinterpret_cast<float**>(buf_value->ptr());
......@@ -134,7 +134,7 @@ void BoxWrapper::CopyKeys(const paddle::platform::Place& place,
const int64_t* gpu_len, int slot_num, int total_len) {
auto stream = dynamic_cast<platform::CUDADeviceContext*>(
platform::DeviceContextPool::Instance().Get(
boost::get<platform::CUDAPlace>(place)))
BOOST_GET_CONST(platform::CUDAPlace, place)))
->stream();
CopyKeysKernel<<<(total_len + 512 - 1) / 512, 512, 0, stream>>>(
origin_keys, total_keys, gpu_len, slot_num, total_len);
......@@ -149,7 +149,7 @@ void BoxWrapper::CopyForPush(const paddle::platform::Place& place,
const int batch_size) {
auto stream = dynamic_cast<platform::CUDADeviceContext*>(
platform::DeviceContextPool::Instance().Get(
boost::get<platform::CUDAPlace>(place)))
BOOST_GET_CONST(platform::CUDAPlace, place)))
->stream();
auto slot_lengths_lod = slot_lengths;
for (int i = 1; i < slot_lengths_lod.size(); i++) {
......
......@@ -84,7 +84,7 @@ StreamGarbageCollector::StreamGarbageCollector(const platform::CUDAPlace &place,
}
StreamGarbageCollector::~StreamGarbageCollector() {
auto place = boost::get<platform::CUDAPlace>(this->dev_ctx_->GetPlace());
auto place = BOOST_GET_CONST(platform::CUDAPlace, this->dev_ctx_->GetPlace());
platform::CUDADeviceGuard guard(place.device);
PADDLE_ENFORCE(cudaStreamSynchronize(stream_));
PADDLE_ENFORCE(cudaStreamDestroy(stream_));
......
......@@ -161,7 +161,7 @@ class GradOpDescMakerBase {
template <typename T>
inline const T& Attr(const std::string& name) const {
return boost::get<T>(GetAttr(name));
return BOOST_GET_CONST(T, GetAttr(name));
}
std::string ForwardOpType() const { return this->fwd_op_.Type(); }
......
......@@ -170,7 +170,8 @@ void ConvBNFusePass::ApplyImpl(ir::Graph* graph) const {
eltwise_y_in_tensor->numel(), 0.0f);
// update weights and biases
float epsilon = boost::get<float>(batch_norm->Op()->GetAttr("epsilon"));
float epsilon =
BOOST_GET_CONST(float, batch_norm->Op()->GetAttr("epsilon"));
recompute_bias_and_weights(scope, conv_weight, *bn_scale, *bn_bias_tensor,
*bn_mean, *bn_variance, eltwise_y_in_tensor,
epsilon, conv_type());
......@@ -275,7 +276,8 @@ void ConvEltwiseAddBNFusePass::ApplyImpl(ir::Graph* graph) const {
scope->FindVar(bn_bias->Name())->GetMutable<LoDTensor>();
// update weights and biases
float epsilon = boost::get<float>(batch_norm->Op()->GetAttr("epsilon"));
float epsilon =
BOOST_GET_CONST(float, batch_norm->Op()->GetAttr("epsilon"));
recompute_bias_and_weights(scope, conv_weight, *bn_scale, *bn_bias_tensor,
*bn_mean, *bn_variance, eltwise_y_in_tensor,
epsilon, conv_type());
......
......@@ -90,7 +90,7 @@ class PlacementPassTest {
if (node->IsOp() && node->Op()) {
auto* op = node->Op();
if (op->HasAttr("use_cudnn") &&
boost::get<bool>(op->GetAttr("use_cudnn"))) {
BOOST_GET_CONST(bool, op->GetAttr("use_cudnn"))) {
++use_cudnn_true_count;
}
}
......
......@@ -192,9 +192,10 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
GET_IR_NODE_FROM_SUBGRAPH(mul, mul, fc_pattern);
// TODO(jczaja): Add support for is_sparse / is_distributed
auto is_sparse = boost::get<bool>(lookup_table->Op()->GetAttr("is_sparse"));
auto is_sparse =
BOOST_GET_CONST(bool, lookup_table->Op()->GetAttr("is_sparse"));
auto is_distributed =
boost::get<bool>(lookup_table->Op()->GetAttr("is_distributed"));
BOOST_GET_CONST(bool, lookup_table->Op()->GetAttr("is_distributed"));
if (is_sparse == true || is_distributed == true) {
return;
......
......@@ -173,7 +173,7 @@ void FCElementwiseLayerNormFusePass::ApplyImpl(ir::Graph *graph) const {
}
int begin_norm_axis =
boost::get<int>(layer_norm->Op()->GetAttr("begin_norm_axis"));
BOOST_GET_CONST(int, layer_norm->Op()->GetAttr("begin_norm_axis"));
auto layer_norm_x_dims = fc_out->Var()->GetShape();
auto layer_norm_x_mat_dims = framework::flatten_to_2d(
framework::make_ddim(layer_norm_x_dims), begin_norm_axis);
......
......@@ -249,8 +249,8 @@ void FuseElewiseAddActPass::RemoveIntermediateOut(Graph *graph) const {
for (auto &cur_node : graph->Nodes()) {
if (cur_node->IsVar()) continue;
if (cur_node->Name() == "fused_elemwise_activation") {
bool save_intermediate_out =
boost::get<bool>(cur_node->Op()->GetAttr("save_intermediate_out"));
bool save_intermediate_out = BOOST_GET_CONST(
bool, cur_node->Op()->GetAttr("save_intermediate_out"));
auto intermediate_out_args = cur_node->Op()->Output("IntermediateOut");
PADDLE_ENFORCE(
save_intermediate_out && !intermediate_out_args.empty(),
......
......@@ -89,29 +89,35 @@ class FuseAdamOpPass : public FuseOptimizerOpPass {
// Check attributions
// NOTE: If new attribution is added, the following code maybe need change.
int op_role = boost::get<int>(
int op_role = BOOST_GET_CONST(
int,
adam_ops[0]->Op()->GetAttr(OpProtoAndCheckerMaker::OpRoleAttrName()));
float beta1 = boost::get<float>(adam_ops[0]->Op()->GetAttr("beta1"));
float beta2 = boost::get<float>(adam_ops[0]->Op()->GetAttr("beta2"));
float epsilon = boost::get<float>(adam_ops[0]->Op()->GetAttr("epsilon"));
bool lazy_mode = boost::get<bool>(adam_ops[0]->Op()->GetAttr("lazy_mode"));
int64_t min_row_size_to_use_multithread = boost::get<int64_t>(
adam_ops[0]->Op()->GetAttr("min_row_size_to_use_multithread"));
float beta1 = BOOST_GET_CONST(float, adam_ops[0]->Op()->GetAttr("beta1"));
float beta2 = BOOST_GET_CONST(float, adam_ops[0]->Op()->GetAttr("beta2"));
float epsilon =
BOOST_GET_CONST(float, adam_ops[0]->Op()->GetAttr("epsilon"));
bool lazy_mode =
BOOST_GET_CONST(bool, adam_ops[0]->Op()->GetAttr("lazy_mode"));
int64_t min_row_size_to_use_multithread = BOOST_GET_CONST(
int64_t, adam_ops[0]->Op()->GetAttr("min_row_size_to_use_multithread"));
for (auto &adam_op : adam_ops) {
PADDLE_ENFORCE_EQ(beta1,
boost::get<float>(adam_op->Op()->GetAttr("beta1")));
PADDLE_ENFORCE_EQ(beta2,
boost::get<float>(adam_op->Op()->GetAttr("beta2")));
PADDLE_ENFORCE_EQ(epsilon,
boost::get<float>(adam_op->Op()->GetAttr("epsilon")));
PADDLE_ENFORCE_EQ(lazy_mode,
boost::get<bool>(adam_op->Op()->GetAttr("lazy_mode")));
PADDLE_ENFORCE_EQ(min_row_size_to_use_multithread,
boost::get<int64_t>(adam_op->Op()->GetAttr(
"min_row_size_to_use_multithread")));
PADDLE_ENFORCE_EQ(op_role,
boost::get<int>(adam_op->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())));
PADDLE_ENFORCE_EQ(
beta1, BOOST_GET_CONST(float, adam_op->Op()->GetAttr("beta1")));
PADDLE_ENFORCE_EQ(
beta2, BOOST_GET_CONST(float, adam_op->Op()->GetAttr("beta2")));
PADDLE_ENFORCE_EQ(
epsilon, BOOST_GET_CONST(float, adam_op->Op()->GetAttr("epsilon")));
PADDLE_ENFORCE_EQ(
lazy_mode,
BOOST_GET_CONST(bool, adam_op->Op()->GetAttr("lazy_mode")));
PADDLE_ENFORCE_EQ(
min_row_size_to_use_multithread,
BOOST_GET_CONST(int64_t, adam_op->Op()->GetAttr(
"min_row_size_to_use_multithread")));
PADDLE_ENFORCE_EQ(
op_role,
BOOST_GET_CONST(int, adam_op->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())));
}
// NOTE: fused_var is only exist in scope, so the graph doesn't have
......@@ -178,23 +184,25 @@ class FuseAdamOpPass : public FuseOptimizerOpPass {
VLOG(6) << "The number of scale op is " << scale_ops.size() << ".";
// Check attributions
// NOTE: If new attribution is added, the following code maybe need change.
int op_role = boost::get<int>(
int op_role = BOOST_GET_CONST(
int,
scale_ops[0]->Op()->GetAttr(OpProtoAndCheckerMaker::OpRoleAttrName()));
float scale = boost::get<float>(scale_ops[0]->Op()->GetAttr("scale"));
float bias = boost::get<float>(scale_ops[0]->Op()->GetAttr("bias"));
float scale = BOOST_GET_CONST(float, scale_ops[0]->Op()->GetAttr("scale"));
float bias = BOOST_GET_CONST(float, scale_ops[0]->Op()->GetAttr("bias"));
bool bias_after_scale =
boost::get<bool>(scale_ops[0]->Op()->GetAttr("bias_after_scale"));
BOOST_GET_CONST(bool, scale_ops[0]->Op()->GetAttr("bias_after_scale"));
for (auto &scale_op : scale_ops) {
PADDLE_ENFORCE_EQ(scale,
boost::get<float>(scale_op->Op()->GetAttr("scale")));
PADDLE_ENFORCE_EQ(bias,
boost::get<float>(scale_op->Op()->GetAttr("bias")));
PADDLE_ENFORCE_EQ(
scale, BOOST_GET_CONST(float, scale_op->Op()->GetAttr("scale")));
PADDLE_ENFORCE_EQ(
bias, BOOST_GET_CONST(float, scale_op->Op()->GetAttr("bias")));
PADDLE_ENFORCE_EQ(
bias_after_scale,
boost::get<bool>(scale_op->Op()->GetAttr("bias_after_scale")));
PADDLE_ENFORCE_EQ(op_role,
boost::get<int>(scale_op->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())));
BOOST_GET_CONST(bool, scale_op->Op()->GetAttr("bias_after_scale")));
PADDLE_ENFORCE_EQ(
op_role,
BOOST_GET_CONST(int, scale_op->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())));
}
// NOTE: fused_var is only exist in scope, so the graph doesn't have
......
......@@ -41,21 +41,23 @@ class FuseMomentumOpPass : public FuseOptimizerOpPass {
// Check attributions
// NOTE: If new attribution is added, the following code maybe need change.
int op_role = boost::get<int>(momentum_ops[0]->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName()));
float mu = boost::get<float>(momentum_ops[0]->Op()->GetAttr("mu"));
int op_role =
BOOST_GET_CONST(int, momentum_ops[0]->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName()));
float mu = BOOST_GET_CONST(float, momentum_ops[0]->Op()->GetAttr("mu"));
bool use_nesterov =
boost::get<bool>(momentum_ops[0]->Op()->GetAttr("use_nesterov"));
BOOST_GET_CONST(bool, momentum_ops[0]->Op()->GetAttr("use_nesterov"));
for (auto &momentum_op : momentum_ops) {
PADDLE_ENFORCE_EQ(mu,
boost::get<float>(momentum_op->Op()->GetAttr("mu")));
PADDLE_ENFORCE_EQ(
mu, BOOST_GET_CONST(float, momentum_op->Op()->GetAttr("mu")));
PADDLE_ENFORCE_EQ(
use_nesterov,
boost::get<bool>(momentum_op->Op()->GetAttr("use_nesterov")));
PADDLE_ENFORCE_EQ(op_role,
boost::get<int>(momentum_op->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())));
BOOST_GET_CONST(bool, momentum_op->Op()->GetAttr("use_nesterov")));
PADDLE_ENFORCE_EQ(
op_role,
BOOST_GET_CONST(int, momentum_op->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())));
}
// NOTE: fused_var is only exist in scope, so the graph doesn't have
......
......@@ -40,7 +40,8 @@ class FuseSgdOpPass : public FuseOptimizerOpPass {
// NOTE: fused_var is only exist in scope, so the graph doesn't have
// fused_var node.
int op_role = boost::get<int>(
int op_role = BOOST_GET_CONST(
int,
sgd_ops[0]->Op()->GetAttr(OpProtoAndCheckerMaker::OpRoleAttrName()));
VLOG(6) << "Insert sgd to graph.";
// Add fused scale
......
......@@ -45,9 +45,9 @@ FuseOptions FusePassBase::FindFuseOption(const Node& node1,
const Node& node2) const {
#ifdef PADDLE_WITH_MKLDNN
bool node1_mkldnn = node1.Op()->HasAttr("use_mkldnn") &&
boost::get<bool>(node1.Op()->GetAttr("use_mkldnn"));
BOOST_GET_CONST(bool, node1.Op()->GetAttr("use_mkldnn"));
bool node2_mkldnn = node2.Op()->HasAttr("use_mkldnn") &&
boost::get<bool>(node2.Op()->GetAttr("use_mkldnn"));
BOOST_GET_CONST(bool, node2.Op()->GetAttr("use_mkldnn"));
if (node1_mkldnn && node2_mkldnn)
return FUSE_MKLDNN;
else if (!node1_mkldnn && !node2_mkldnn)
......
......@@ -83,26 +83,26 @@ static std::string RefineTemplateWithAttr(const std::string& op_type,
proto::AttrType attr_type =
static_cast<proto::AttrType>(it->second.which() - 1);
if (attr_type == proto::AttrType::BOOLEAN) {
bool result = boost::get<bool>(attr);
bool result = BOOST_GET(bool, attr);
if (result) {
ret = "true";
} else {
ret = "false";
}
} else if (attr_type == proto::AttrType::INT) {
int result = boost::get<int>(attr);
int result = BOOST_GET(int, attr);
str_cvt << result;
ret = str_cvt.str();
} else if (attr_type == proto::AttrType::LONG) {
int64_t result = boost::get<int64_t>(attr);
int64_t result = BOOST_GET(int64_t, attr);
str_cvt << result;
ret = str_cvt.str();
} else if (attr_type == proto::AttrType::FLOAT) {
float result = boost::get<float>(attr);
float result = BOOST_GET(float, attr);
str_cvt << result;
ret = str_cvt.str();
} else if (attr_type == proto::AttrType::STRING) {
std::string result = boost::get<std::string>(attr);
std::string result = BOOST_GET(std::string, attr);
ret = result;
}
} else {
......
......@@ -94,7 +94,7 @@ static int ExtractOpRole(fusion_group::SubGraph* subgraph) {
for (auto* n : subgraph->Nodes()) {
if (n && n->IsOp() && n->Op()) {
if (n->Op()->HasAttr(attr_name)) {
op_roles.insert(boost::get<int>(n->Op()->GetAttr(attr_name)));
op_roles.insert(BOOST_GET_CONST(int, n->Op()->GetAttr(attr_name)));
}
}
}
......
......@@ -2071,7 +2071,8 @@ void patterns::ShuffleChannelPattern::operator()(PDNode *reshape1_in) {
auto reshape1_op =
pattern->NewNode(reshape1_op_repr())->assert_is_op("reshape2");
reshape1_op->assert_more([&](Node *x) {
return boost::get<std::vector<int>>(x->Op()->GetAttr("shape")).size() == 5;
return BOOST_GET_CONST(std::vector<int>, x->Op()->GetAttr("shape"))
.size() == 5;
});
auto reshape1_out = pattern->NewNode(reshape1_out_repr())
......
......@@ -143,7 +143,7 @@ struct PDNode {
PDNode* assert_op_attr(const std::string& attr_name, const T& attr) {
asserts_.emplace_back([=](Node* x) {
return x && x->IsOp() && x->Op()->HasAttr(attr_name) &&
boost::get<T>(x->Op()->GetAttr(attr_name)) == attr;
BOOST_GET_CONST(T, x->Op()->GetAttr(attr_name)) == attr;
});
return this;
}
......
......@@ -31,7 +31,8 @@ std::string FormatName(const Node* node) {
!node->Op()->HasAttr(OpProtoAndCheckerMaker::OpNamescopeAttrName())) {
return node->Name();
}
const std::string full_scope = boost::get<std::string>(
const std::string full_scope = BOOST_GET_CONST(
std::string,
node->Op()->GetAttr(OpProtoAndCheckerMaker::OpNamescopeAttrName()));
return string::Sprintf("%s%s", full_scope.c_str(), node->Name().c_str());
}
......
......@@ -102,12 +102,12 @@ TEST(IsTestPass, basic) {
for (auto* node : graph->Nodes()) {
if (node->IsOp()) {
auto* op = node->Op();
auto op_name = boost::get<std::string>(op->GetAttr("name"));
auto op_name = BOOST_GET_CONST(std::string, op->GetAttr("name"));
if (op_name == "conv3") {
ASSERT_FALSE(op->HasAttr("is_test"));
} else {
ASSERT_TRUE(op->HasAttr("is_test"));
EXPECT_TRUE(boost::get<bool>(op->GetAttr("is_test")));
EXPECT_TRUE(BOOST_GET_CONST(bool, op->GetAttr("is_test")));
}
}
}
......
......@@ -170,7 +170,8 @@ ir::Node* LockFreeOptimizePass::CreateNewSGDNode(
new_desc.SetInput("Grad", std::vector<std::string>({grad_node->Name()}));
new_desc.SetOutput("ParamOut", old_desc->Output("ParamOut"));
std::vector<std::string> op_role_vars = boost::get<std::vector<std::string>>(
std::vector<std::string> op_role_vars = BOOST_GET_CONST(
std::vector<std::string>,
new_desc.GetAttr(framework::OpProtoAndCheckerMaker::OpRoleVarAttrName()));
// replace the second op role var, because the grad name was
// changed in new optimizer
......
......@@ -186,12 +186,12 @@ TEST(test_reference_count_pass, test_no_need_buffer_var_shrink) {
ReferenceCountPassTestHelper helper(program, use_cuda);
ASSERT_TRUE(helper.IsLastLivedOps(x0, {"scale"}));
ASSERT_EQ(
boost::get<float>(helper.LastLivedOps(x0)[0]->Attrs().at("scale")),
BOOST_GET_CONST(float, helper.LastLivedOps(x0)[0]->Attrs().at("scale")),
1.0f);
ASSERT_TRUE(helper.IsLastLivedOps(x1, {"scale"}));
ASSERT_EQ(
boost::get<float>(helper.LastLivedOps(x1)[0]->Attrs().at("scale")),
BOOST_GET_CONST(float, helper.LastLivedOps(x1)[0]->Attrs().at("scale")),
3.0f);
ASSERT_TRUE(helper.IsLastLivedOps(x2, {"elementwise_mul"}));
......
......@@ -58,8 +58,9 @@ void ConvActivationFusePass::ApplyImpl(ir::Graph* graph) const {
// MKLDNN ops use alpha and beta as activation parameters but paddle ops are
// not generalized
if (activation_type() == "relu6") {
desc->SetAttr("fuse_alpha",
boost::get<float>(activation->Op()->GetAttr("threshold")));
desc->SetAttr(
"fuse_alpha",
BOOST_GET_CONST(float, activation->Op()->GetAttr("threshold")));
} else if (activation_type() == "swish") {
// paddle uses beta but mkldnn uses alpha for swish
desc->SetAttr("fuse_alpha",
......
......@@ -114,8 +114,8 @@ void MainTest(std::string activation) {
if (node->IsOp() && node->Op()->Type() == "conv2d") {
auto* op = node->Op();
ASSERT_TRUE(op->HasAttr("use_mkldnn"));
EXPECT_TRUE(boost::get<bool>(op->GetAttr("use_mkldnn")));
auto op_name = boost::get<std::string>(op->GetAttr("name"));
EXPECT_TRUE(BOOST_GET_CONST(bool, op->GetAttr("use_mkldnn")));
auto op_name = BOOST_GET_CONST(std::string, op->GetAttr("name"));
if (op->GetAttrIfExists<std::string>("fuse_activation") == activation) {
++conv_activation_count;
}
......
......@@ -118,14 +118,14 @@ void MainTest(bool convWithExistingBias) {
if (node->IsOp() && node->Op()->Type() == "conv2d") {
auto* op = node->Op();
ASSERT_TRUE(op->HasAttr("use_mkldnn"));
EXPECT_TRUE(boost::get<bool>(op->GetAttr("use_mkldnn")));
EXPECT_TRUE(BOOST_GET_CONST(bool, op->GetAttr("use_mkldnn")));
// check if "conv" convolution is fused
auto op_name = boost::get<std::string>(op->GetAttr("name"));
auto op_name = BOOST_GET_CONST(std::string, op->GetAttr("name"));
if (op_name == "conv") {
auto input_names = op->InputNames();
ASSERT_TRUE(std::find(input_names.begin(), input_names.end(), "Bias") !=
input_names.end());
auto bias = boost::get<std::vector<std::string>>(op->Input("Bias"));
auto bias = op->Input("Bias");
if (bias.size()) {
++conv_bias_count;
}
......
......@@ -111,7 +111,8 @@ void MainTest(const ProgramDesc& prog, bool fuse_relu) {
if (op->Type() == "conv2d") {
ASSERT_TRUE(op->HasAttr("fuse_activation"));
bool fuse_relu_attr =
(boost::get<std::string>(op->GetAttr("fuse_activation")) == "relu");
(BOOST_GET_CONST(std::string, op->GetAttr("fuse_activation")) ==
"relu");
EXPECT_EQ(fuse_relu, fuse_relu_attr);
} else if (op->Type() == "relu") {
relu_count++;
......
......@@ -73,7 +73,7 @@ bool IsReachable(ir::Graph* graph, Node* from, Node* to) {
template <typename T>
boost::optional<T> HasAttribute(const Node& op, const std::string& attr) {
if (op.Op()->HasAttr(attr))
return boost::get<T>(op.Op()->GetAttr(attr));
return BOOST_GET_CONST(T, op.Op()->GetAttr(attr));
else
return boost::none;
}
......
......@@ -282,8 +282,10 @@ void CPUQuantizePass::QuantizeConv(Graph* graph,
// change threshold in bounded ReLu
if (conv_op->Op()->GetAttrIfExists<std::string>("fuse_activation") ==
"relu6") {
float scale_out = boost::get<float>(conv_op->Op()->GetAttr("Scale_out"));
float threshold = boost::get<float>(conv_op->Op()->GetAttr("fuse_alpha"));
float scale_out =
BOOST_GET_CONST(float, conv_op->Op()->GetAttr("Scale_out"));
float threshold =
BOOST_GET_CONST(float, conv_op->Op()->GetAttr("fuse_alpha"));
conv_op->Op()->SetAttr("fuse_alpha", scale_out * threshold);
}
......
......@@ -179,14 +179,14 @@ void MainTest(const ProgramDesc& prog, int conv_count, int pool_count,
auto* op = node->Op();
if (op->Type() == "conv2d") {
conv2d_nodes_count++;
auto op_name = boost::get<std::string>(op->GetAttr("name"));
EXPECT_EQ(boost::get<float>(op->GetAttr("Scale_in")), scale)
auto op_name = BOOST_GET_CONST(std::string, op->GetAttr("name"));
EXPECT_EQ(BOOST_GET_CONST(float, op->GetAttr("Scale_in")), scale)
<< "Scale_in for node '" + op_name + "'.";
EXPECT_EQ(boost::get<float>(op->GetAttr("Scale_out")), scale)
EXPECT_EQ(BOOST_GET_CONST(float, op->GetAttr("Scale_out")), scale)
<< "Scale_out for node '" + op_name + "'.";
EXPECT_EQ(
boost::get<std::vector<float>>(op->GetAttr("Scale_weights"))[0],
scale)
EXPECT_EQ(BOOST_GET_CONST(std::vector<float>,
op->GetAttr("Scale_weights"))[0],
scale)
<< "Scale_weights for node '" + op_name + "'.";
} else if (op->Type() == "pool2d") {
pool2d_nodes_count++;
......@@ -340,14 +340,14 @@ void MainTestTranspose(const ProgramDesc& prog, int conv_count,
transpose_nodes_count++;
} else if (op->Type() == "conv2d") {
conv_nodes_count++;
auto op_name = boost::get<std::string>(op->GetAttr("name"));
EXPECT_EQ(boost::get<float>(op->GetAttr("Scale_in")), scale)
auto op_name = BOOST_GET_CONST(std::string, op->GetAttr("name"));
EXPECT_EQ(BOOST_GET_CONST(float, op->GetAttr("Scale_in")), scale)
<< "Scale_in for node '" + op_name + "'.";
EXPECT_EQ(boost::get<float>(op->GetAttr("Scale_out")), scale)
EXPECT_EQ(BOOST_GET_CONST(float, op->GetAttr("Scale_out")), scale)
<< "Scale_out for node '" + op_name + "'.";
EXPECT_EQ(
boost::get<std::vector<float>>(op->GetAttr("Scale_weights"))[0],
scale)
EXPECT_EQ(BOOST_GET_CONST(std::vector<float>,
op->GetAttr("Scale_weights"))[0],
scale)
<< "Scale_weights for node '" + op_name + "'.";
} else if (op->Type() == "quantize") {
quantize_nodes_count++;
......@@ -436,14 +436,14 @@ void MainTestReshape(const ProgramDesc& prog, int transpose_count,
reshape_nodes_count++;
} else if (op->Type() == "quantize") {
quantize_nodes_count++;
quant_scale = boost::get<float>(op->GetAttr("Scale"));
quant_scale = BOOST_GET_CONST(float, op->GetAttr("Scale"));
EXPECT_EQ(quant_scale, scale) << "Scale for node '" + op->Type() + "'.";
} else if (op->Type() == "dequantize") {
dequantize_nodes_count++;
auto op_name = op->GetAttrIfExists<std::string>("name");
VLOG(3) << op_name << "\n";
if (op_name != "Dequantize1") {
dequant_scale = boost::get<float>(op->GetAttr("Scale"));
dequant_scale = BOOST_GET_CONST(float, op->GetAttr("Scale"));
EXPECT_EQ(dequant_scale, scale)
<< "Scale for node '" + op->Type() + "'.";
}
......@@ -530,12 +530,12 @@ void MainTestMatmul(const ProgramDesc& prog, int matmul_count, int quant_count,
auto* op = node->Op();
if (op->Type() == "matmul") {
matmul_nodes_count++;
auto op_name = boost::get<std::string>(op->GetAttr("name"));
EXPECT_EQ(boost::get<float>(op->GetAttr("Scale_x")), scale)
auto op_name = BOOST_GET_CONST(std::string, op->GetAttr("name"));
EXPECT_EQ(BOOST_GET_CONST(float, op->GetAttr("Scale_x")), scale)
<< "Scale_x for node '" + op_name + "'.";
EXPECT_EQ(boost::get<float>(op->GetAttr("Scale_y")), scale)
EXPECT_EQ(BOOST_GET_CONST(float, op->GetAttr("Scale_y")), scale)
<< "Scale_y for node '" + op_name + "'.";
EXPECT_EQ(boost::get<float>(op->GetAttr("Scale_out")), scale)
EXPECT_EQ(BOOST_GET_CONST(float, op->GetAttr("Scale_out")), scale)
<< "Scale_out for node '" + op_name + "'.";
} else if (op->Type() == "quantize") {
quantize_nodes_count++;
......
......@@ -102,7 +102,7 @@ void MainTest(std::initializer_list<std::string> quantize_enabled_op_types,
if (node->IsOp()) {
auto* op = node->Op();
if (op->HasAttr("use_quantizer") &&
boost::get<bool>(op->GetAttr("use_quantizer"))) {
BOOST_GET_CONST(bool, op->GetAttr("use_quantizer"))) {
++use_quantizer_true_count;
}
}
......@@ -122,7 +122,7 @@ void DefaultAttrTest(unsigned expected_use_quantizer_true_count) {
if (node->IsOp()) {
auto* op = node->Op();
if (op->HasAttr("use_quantizer") &&
boost::get<bool>(op->GetAttr("use_quantizer"))) {
BOOST_GET_CONST(bool, op->GetAttr("use_quantizer"))) {
++use_quantizer_true_count;
}
}
......
......@@ -69,8 +69,10 @@ void CPUQuantizeSquashPass::DequantQuantSquash(
GET_IR_NODE_FROM_SUBGRAPH(next_op, next_op, squash_pattern);
auto* next_op_desc = next_op->Op();
float dequant_scale = boost::get<float>(dequant_op->Op()->GetAttr("Scale"));
float quant_scale = boost::get<float>(quant_op->Op()->GetAttr("Scale"));
float dequant_scale =
BOOST_GET_CONST(float, dequant_op->Op()->GetAttr("Scale"));
float quant_scale =
BOOST_GET_CONST(float, quant_op->Op()->GetAttr("Scale"));
PADDLE_ENFORCE_NE(
nodes_keep_counter->find(dequant_out), nodes_keep_counter->end(),
platform::errors::NotFound("The dequant output node is not found"));
......@@ -155,7 +157,7 @@ void CPUQuantizeSquashPass::OpRequantSquash(Graph* graph) const {
"should have requantize input as output"));
float requant_scale_out =
boost::get<float>(requant_op->Op()->GetAttr("Scale_out"));
BOOST_GET_CONST(float, requant_op->Op()->GetAttr("Scale_out"));
any_op->Op()->SetAttr("Scale_out", requant_scale_out);
any_op->Op()->SetOutput(any_op_output_name,
std::vector<std::string>({requant_out->Name()}));
......
......@@ -386,8 +386,8 @@ void EqualScaleTest(const ProgramDesc& prog, const std::string& op_name,
for (auto* node : graph->Nodes()) {
if (node->IsOp() &&
boost::get<std::string>(node->Op()->GetAttr("name")) == op_name) {
float op_scale = boost::get<float>(node->Op()->GetAttr(scale_name));
BOOST_GET_CONST(std::string, node->Op()->GetAttr("name")) == op_name) {
float op_scale = BOOST_GET_CONST(float, node->Op()->GetAttr(scale_name));
EXPECT_EQ(op_scale, scale);
}
}
......@@ -403,9 +403,11 @@ void CheckRequantScalesTest(const ProgramDesc& prog, float scale_in,
for (auto* node : graph->Nodes()) {
if (node->IsOp() && node->Op()->Type() == "requantize") {
float op_scale_in = boost::get<float>(node->Op()->GetAttr("Scale_in"));
float op_scale_in =
BOOST_GET_CONST(float, node->Op()->GetAttr("Scale_in"));
EXPECT_EQ(op_scale_in, scale_in);
float op_scale_out = boost::get<float>(node->Op()->GetAttr("Scale_out"));
float op_scale_out =
BOOST_GET_CONST(float, node->Op()->GetAttr("Scale_out"));
EXPECT_EQ(op_scale_out, scale_out);
}
}
......
......@@ -95,12 +95,12 @@ TEST(DepthwiseConvMKLDNNPass, basic) {
if (node->IsOp()) {
auto* op = node->Op();
if (op->Type() == "conv2d") {
if (boost::get<bool>(op->GetAttr("use_mkldnn")))
if (BOOST_GET_CONST(bool, op->GetAttr("use_mkldnn")))
after.mkldnn_conv_nodes++;
else
after.other_conv_nodes++;
} else if (op->Type() == "depthwise_conv2d") {
if (boost::get<bool>(op->GetAttr("use_mkldnn")))
if (BOOST_GET_CONST(bool, op->GetAttr("use_mkldnn")))
after.mkldnn_depthwise_conv_nodes++;
else
after.other_depthwise_conv_nodes++;
......
......@@ -46,9 +46,9 @@ void MatmulTransposeReshapeMKLDNNPass::ApplyImpl(ir::Graph *graph) const {
GET_IR_NODE_FROM_SUBGRAPH(reshape_out, reshape_out, mtrp);
GET_IR_NODE_FROM_SUBGRAPH(reshape_out_xshape, reshape_out_xshape, mtrp);
auto reshape_shape =
boost::get<std::vector<int>>(reshape_op->Op()->GetAttr("shape"));
BOOST_GET_CONST(std::vector<int>, reshape_op->Op()->GetAttr("shape"));
auto transpose_axis =
boost::get<std::vector<int>>(transpose_op->Op()->GetAttr("axis"));
BOOST_GET_CONST(std::vector<int>, transpose_op->Op()->GetAttr("axis"));
auto reshape_out_size = reshape_shape.size();
auto transpose_out_size = transpose_axis.size();
......
......@@ -51,7 +51,8 @@ void MKLDNNInPlacePass::ApplyImpl(ir::Graph* graph) const {
GET_IR_NODE_FROM_SUBGRAPH(next_op_out, next_op_out, mkldnn_inplace);
if ((current_op->Op()->HasAttr("use_mkldnn") == false) ||
(boost::get<bool>(current_op->Op()->GetAttr("use_mkldnn")) == false)) {
(BOOST_GET_CONST(bool, current_op->Op()->GetAttr("use_mkldnn")) ==
false)) {
VLOG(3) << "do not perform mkl-dnn inplace: use_mkldnn missing or set to "
"false";
return;
......
......@@ -111,7 +111,7 @@ class PlacementPassTest {
if (node->IsOp()) {
auto* op = node->Op();
if (op->HasAttr("use_mkldnn") &&
boost::get<bool>(op->GetAttr("use_mkldnn"))) {
BOOST_GET_CONST(bool, op->GetAttr("use_mkldnn"))) {
++use_mkldnn_true_count;
}
}
......
......@@ -86,8 +86,9 @@ void BatchMergePass::ApplyImpl(ir::Graph* graph) const {
for (auto node : nodes) {
if (!node->IsOp()) continue;
PADDLE_ENFORCE(node->Op(), "must find opdesc");
int op_role = boost::get<int>(node->Op()->GetAttr(
framework::OpProtoAndCheckerMaker::OpRoleAttrName()));
int op_role = BOOST_GET_CONST(
int, node->Op()->GetAttr(
framework::OpProtoAndCheckerMaker::OpRoleAttrName()));
if ((op_role == static_cast<int>(framework::OpRole::kForward)) ||
(op_role & static_cast<int>(framework::OpRole::kBackward)) ||
(op_role & static_cast<int>(framework::OpRole::kLoss))) {
......@@ -98,7 +99,8 @@ void BatchMergePass::ApplyImpl(ir::Graph* graph) const {
optimize_ops.push_back(node);
auto op_role_var = node->Op()->GetNullableAttr(
OpProtoAndCheckerMaker::OpRoleVarAttrName());
auto op_role_vars = boost::get<std::vector<std::string>>(op_role_var);
auto op_role_vars =
BOOST_GET_CONST(std::vector<std::string>, op_role_var);
for (size_t i = 0; i < op_role_vars.size(); i += 2) {
grad_names.insert(op_role_vars[i + 1]);
gradname2paramname[op_role_vars[i + 1]] = op_role_vars[i];
......
......@@ -50,8 +50,8 @@ typedef std::vector<details::OpHandleBase *> GraphOps;
const char kGraphOps[] = "ops";
bool OpHaveRole(const ir::Node &node, const framework::OpRole &role) {
return boost::get<int>(
node.Op()->GetAttr(OpProtoAndCheckerMaker::OpRoleAttrName())) ==
return BOOST_GET_CONST(int, node.Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())) ==
static_cast<int>(role);
}
......@@ -580,8 +580,8 @@ details::VarHandle *MultiDevSSAGraphBuilderBase::CreateReduceOp(
bool MultiDevSSAGraphBuilderBase::IsScaleLossOp(ir::Node *node) const {
return !loss_var_name_.empty() && node->Op() &&
boost::get<int>(
node->Op()->GetAttr(OpProtoAndCheckerMaker::OpRoleAttrName())) ==
BOOST_GET_CONST(int, node->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())) ==
(static_cast<int>(OpRole::kBackward) |
static_cast<int>(OpRole::kLoss));
}
......@@ -635,7 +635,8 @@ int BalanceVarSSAGraphBuilder::GetOpDeviceID(ir::Node *node) const {
if (!OpHaveRole(*node, framework::OpRole::kOptimize)) {
return -1;
}
auto param_grad = boost::get<std::vector<std::string>>(
auto param_grad = BOOST_GET_CONST(
std::vector<std::string>,
node->Op()->GetAttr(OpProtoAndCheckerMaker::OpRoleVarAttrName()));
PADDLE_ENFORCE_EQ(param_grad.size(), 2U);
......@@ -729,7 +730,8 @@ int ReduceSSAGraphBuilder::GetOpDeviceID(
return -1;
}
auto param_grad = boost::get<std::vector<std::string>>(
auto param_grad = BOOST_GET_CONST(
std::vector<std::string>,
node->Op()->GetAttr(OpProtoAndCheckerMaker::OpRoleVarAttrName()));
PADDLE_ENFORCE_EQ(param_grad.size(), 2U);
......@@ -776,10 +778,10 @@ std::vector<ir::Node *> ReduceSSAGraphBuilder::SortForReduceMode(
// This op runs on all devices, and its output may have parameter's
// gradients.
sorted_ops.emplace_back(node);
bool is_bk_op =
static_cast<bool>(boost::get<int>(node->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())) &
static_cast<int>(OpRole::kBackward));
bool is_bk_op = static_cast<bool>(
BOOST_GET_CONST(int, node->Op()->GetAttr(
OpProtoAndCheckerMaker::OpRoleAttrName())) &
static_cast<int>(OpRole::kBackward));
if (!is_bk_op) continue;
// Currently, we assume that once gradient is generated, it can be
// broadcast, and each gradient is only broadcast once.
......@@ -820,8 +822,9 @@ bool DistSSAGraphBuilder::DealWithSpecialOp(ir::Graph *result,
"Can not schedule the RPC operator to the right place.");
if (node->Op()->Type() == "recv") {
auto recv_vars_attr =
boost::get<std::vector<std::string>>(node->Op()->GetNullableAttr(
OpProtoAndCheckerMaker::OpRoleVarAttrName()));
BOOST_GET_CONST(std::vector<std::string>,
node->Op()->GetNullableAttr(
OpProtoAndCheckerMaker::OpRoleVarAttrName()));
PADDLE_ENFORCE(recv_vars_attr.size() == 2UL); // [parameter, gradient]
if (recv_vars_attr[0].find(".block") == std::string::npos) {
bcast_var_name_set_[op_dev_id].emplace(recv_vars_attr[0]);
......@@ -885,7 +888,8 @@ int DistSSAGraphBuilder::CreateRPCOp(ir::Graph *result, ir::Node *node) const {
for (ir::Node *n : node->inputs) {
input_var_names.push_back(n->Name());
}
auto send_param_grad = boost::get<std::vector<std::string>>(
auto send_param_grad = BOOST_GET_CONST(
std::vector<std::string>,
node->Op()->GetAttr(OpProtoAndCheckerMaker::OpRoleVarAttrName()));
PADDLE_ENFORCE_EQ(send_param_grad.size(), 2U);
op_dev_id = GetAppropriateDeviceID({send_param_grad[1]});
......@@ -901,7 +905,8 @@ int DistSSAGraphBuilder::CreateRPCOp(ir::Graph *result, ir::Node *node) const {
for (ir::Node *n : node->outputs) {
output_var_names.push_back(n->Name());
}
auto recv_param_grad = boost::get<std::vector<std::string>>(
auto recv_param_grad = BOOST_GET_CONST(
std::vector<std::string>,
node->Op()->GetAttr(OpProtoAndCheckerMaker::OpRoleVarAttrName()));
if (recv_param_grad.size() == 2U) {
op_dev_id = GetVarDeviceID(recv_param_grad[1]);
......
......@@ -55,10 +55,10 @@ static int BuildFusion(Graph* graph, const std::string& name_scope) {
Node* mul1_out, Node* mul2_out, Node* eltadd0_b, Node* eltadd1_b,
Node* eltadd2_b, Node* eltadd_qk_b, Node* reshape2,
Node* reshape2_qkv_out, Node* scale, Node* scale_out) {
auto scale_attr = boost::get<float>(scale->Op()->GetAttr("scale"));
// auto scale_bias = boost::get<float>(scale->Op()->GetAttr("bias"));
auto scale_attr = BOOST_GET_CONST(float, scale->Op()->GetAttr("scale"));
// auto scale_bias = BOOST_GET_CONST(float, scale->Op()->GetAttr("bias"));
// bool after_scale =
// boost::get<bool>(scale->Op()->GetAttr("bias_after_scale"));
// BOOST_GET_CONST(bool, scale->Op()->GetAttr("bias_after_scale"));
// create multihead
OpDesc multihead_op_desc;
......@@ -78,7 +78,7 @@ static int BuildFusion(Graph* graph, const std::string& name_scope) {
auto reshape_desc = reshape2->Op();
int head_number =
boost::get<std::vector<int>>(reshape_desc->GetAttr("shape")).at(2);
BOOST_GET_CONST(std::vector<int>, reshape_desc->GetAttr("shape")).at(2);
ReplaceOutputVar(mul0, mul0_out, q_var_node);
ReplaceOutputVar(mul1, mul1_out, k_var_node);
......@@ -444,7 +444,7 @@ static int BuildFusionV2(Graph* graph, const std::string& name_scope,
Node* mul1_out, Node* mul2_out, Node* mul0_w, Node* mul1_w, Node* mul2_w,
Node* eltadd0_b, Node* eltadd1_b, Node* eltadd2_b, Node* eltadd_qk_b,
Node* reshape2, Node* reshape2_qkv_out, Node* scale, Node* scale_out) {
auto scale_attr = boost::get<float>(scale->Op()->GetAttr("scale"));
auto scale_attr = BOOST_GET_CONST(float, scale->Op()->GetAttr("scale"));
// mul (B * S * Hidden) x (Hidden * 3 * N * H) = (B * S * 3 * N * H)
// bias (B * S * 3 * N * H) + bias (3 * N * H)
......@@ -524,7 +524,7 @@ static int BuildFusionV2(Graph* graph, const std::string& name_scope,
auto reshape_desc = reshape2->Op();
int head_number =
boost::get<std::vector<int>>(reshape_desc->GetAttr("shape")).at(2);
BOOST_GET_CONST(std::vector<int>, reshape_desc->GetAttr("shape")).at(2);
OpDesc multihead_op_desc;
multihead_op_desc.SetType("multihead_matmul");
......
......@@ -93,7 +93,8 @@ void RunQuantDequant(ir::Graph* graph, Scope* scope, int times,
}
}
int bit_length = boost::get<int>(quant_op->Op()->GetAttr("bit_length"));
int bit_length =
BOOST_GET_CONST(int, quant_op->Op()->GetAttr("bit_length"));
int range = ((1 << (bit_length - 1)) - 1);
// Prepare input scale
std::string input_scale_var_name = quant_op->Op()->Input("InScale").front();
......@@ -125,9 +126,9 @@ void RunQuantDequant(ir::Graph* graph, Scope* scope, int times,
delete_nodes.insert(
nodes[i * kNumFields + kDequantOpWeightScaleOffset]);
} else {
float max_range = boost::get<float>(
nodes[i * kNumFields + kDequantOpOffset]->Op()->GetAttr(
"max_range"));
float max_range = BOOST_GET_CONST(
float, nodes[i * kNumFields + kDequantOpOffset]->Op()->GetAttr(
"max_range"));
weight_scale.push_back((range * range) / max_range);
}
......
......@@ -42,7 +42,7 @@ static bool IsOutputOfFC(Node* n) {
static bool IsFCWithAct(Node* n, const std::string& act_type = "relu") {
if (n && n->IsOp() && n->Op() && n->Op()->Type() == "fc" &&
n->inputs.size() == 3U && n->outputs.size() == 1U) {
return boost::get<std::string>(n->Op()->GetAttr("activation_type")) ==
return BOOST_GET_CONST(std::string, n->Op()->GetAttr("activation_type")) ==
act_type;
}
return false;
......
......@@ -43,7 +43,7 @@ PDNode* BuildSeqPoolConcatPattern(PDPattern* pattern,
bool this_is_seqpool_op =
x && x->IsOp() && x->Op()->Type() == "sequence_pool" &&
x->Op()->HasAttr("pooltype") &&
boost::get<std::string>(x->Op()->GetAttr("pooltype")) == type &&
BOOST_GET_CONST(std::string, x->Op()->GetAttr("pooltype")) == type &&
x->outputs.size() == 2; // seqpool should only have 2 outputs
bool satisfied_all = this_is_seqpool_op;
if (this_is_seqpool_op) {
......
......@@ -55,9 +55,9 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const {
std::string output_name = reshape2_out->Name();
auto reshape1_shape =
boost::get<std::vector<int>>(reshape1_desc->GetAttr("shape"));
BOOST_GET_CONST(std::vector<int>, reshape1_desc->GetAttr("shape"));
auto reshape2_shape =
boost::get<std::vector<int>>(reshape2_desc->GetAttr("shape"));
BOOST_GET_CONST(std::vector<int>, reshape2_desc->GetAttr("shape"));
int i_c = reshape1_shape[2];
int o_c = reshape2_shape[1];
......
......@@ -53,10 +53,10 @@ bool SimplifyWithBasicOpsPass::SimplifyDropout(
// dropout_op is INT.
if (dropout_op_desc->HasAttr("is_test")) {
if (dropout_op_desc->GetAttrType("is_test") == proto::AttrType::BOOLEAN) {
is_test = boost::get<bool>(dropout_op_desc->GetAttr("is_test"));
is_test = BOOST_GET_CONST(bool, dropout_op_desc->GetAttr("is_test"));
} else if (dropout_op_desc->GetAttrType("is_test") ==
proto::AttrType::INT) {
is_test = boost::get<int>(dropout_op_desc->GetAttr("is_test")) == 0
is_test = BOOST_GET_CONST(int, dropout_op_desc->GetAttr("is_test")) == 0
? false
: true;
}
......@@ -74,12 +74,14 @@ bool SimplifyWithBasicOpsPass::SimplifyDropout(
if (dropout_op_desc->HasAttr("dropout_implementation")) {
if (dropout_op_desc->GetAttrType("dropout_implementation") ==
proto::AttrType::BOOLEAN) {
upscale_in_train =
boost::get<bool>(dropout_op_desc->GetAttr("dropout_implementation"));
upscale_in_train = BOOST_GET_CONST(
bool, dropout_op_desc->GetAttr("dropout_implementation"));
} else if (dropout_op_desc->GetAttrType("dropout_implementation") ==
proto::AttrType::STRING) {
upscale_in_train = boost::get<std::string>(dropout_op_desc->GetAttr(
"dropout_implementation")) == "upscale_in_train";
upscale_in_train =
BOOST_GET_CONST(std::string,
dropout_op_desc->GetAttr("dropout_implementation")) ==
"upscale_in_train";
}
}
......@@ -129,7 +131,7 @@ bool SimplifyWithBasicOpsPass::SimplifyDropout(
// \|/
// dropout_x -> scale_op -> dropout_out -> next_op -> next_out
float scale =
1.0f - boost::get<float>(dropout_op_desc->GetAttr("dropout_prob"));
1.0f - BOOST_GET_CONST(float, dropout_op_desc->GetAttr("dropout_prob"));
framework::OpDesc new_op_desc;
new_op_desc.SetType("scale");
......
......@@ -65,7 +65,7 @@ TEST(IsTestPass, basic) {
for (auto* node : graph->Nodes()) {
if (node->IsOp()) {
auto* op = node->Op();
auto op_name = boost::get<std::string>(op->GetAttr("name"));
auto op_name = BOOST_GET_CONST(std::string, op->GetAttr("name"));
if (op_name == "bn") {
ASSERT_EQ(op->Type(), "sync_batch_norm");
}
......
......@@ -75,11 +75,11 @@ void RunTransposeFlattenConcatFuse(ir::Graph *graph, int times) {
Node *concat_op = subgraph.at(pattern.GetPDNode("concat"));
Node *concat_out = subgraph.at(pattern.GetPDNode("concat_out"));
std::vector<std::string> input_names;
std::vector<int> trans_axis = boost::get<std::vector<int>>(
nodes[kTransOffset]->Op()->GetAttr("axis"));
std::vector<int> trans_axis = BOOST_GET_CONST(
std::vector<int>, nodes[kTransOffset]->Op()->GetAttr("axis"));
int flatten_axis =
boost::get<int>(nodes[kFlattenOffset]->Op()->GetAttr("axis"));
int concat_axis = boost::get<int>(concat_op->Op()->GetAttr("axis"));
BOOST_GET_CONST(int, nodes[kFlattenOffset]->Op()->GetAttr("axis"));
int concat_axis = BOOST_GET_CONST(int, concat_op->Op()->GetAttr("axis"));
std::string output_name = concat_out->Name();
for (int i = 0; i < times; i++) {
......
......@@ -197,7 +197,7 @@ class Vector {
return gpu_ == nullptr
? boost::none
: boost::optional<platform::CUDAPlace>(
boost::get<platform::CUDAPlace>(gpu_->place()));
BOOST_GET_CONST(platform::CUDAPlace, gpu_->place()));
}
private:
......@@ -386,7 +386,7 @@ class Vector {
std::lock_guard<std::mutex> guard(mtx);
auto cuda_place = m_.Data().CUDAPlace();
if (cuda_place == boost::none ||
cuda_place == boost::get<platform::CUDAPlace>(place)) {
cuda_place == BOOST_GET(platform::CUDAPlace, place)) {
return m_.Data().CUDAData(place);
}
}
......@@ -402,7 +402,7 @@ class Vector {
std::lock_guard<std::mutex> guard(mtx);
auto cuda_place = m_.Data().CUDAPlace();
if (cuda_place == boost::none ||
cuda_place == boost::get<platform::CUDAPlace>(place)) {
cuda_place == BOOST_GET(platform::CUDAPlace, place)) {
return m_.MutableData()->CUDAMutableData(place);
}
}
......
......@@ -30,7 +30,7 @@ TEST(test_no_need_buffer_vars_inference, test_static_graph) {
ASSERT_TRUE(ctx.HasOutput("Out"));
ASSERT_FALSE(ctx.HasOutput("X"));
ASSERT_TRUE(boost::get<bool>(ctx.GetAttr("is_test")));
ASSERT_TRUE(BOOST_GET_CONST(bool, ctx.GetAttr("is_test")));
}
TEST(test_no_need_buffer_vars_inference, test_dygraph) {
......@@ -45,7 +45,7 @@ TEST(test_no_need_buffer_vars_inference, test_dygraph) {
ASSERT_TRUE(ctx.HasOutput("Out"));
ASSERT_FALSE(ctx.HasOutput("X"));
ASSERT_TRUE(boost::get<bool>(ctx.GetAttr("is_test")));
ASSERT_TRUE(BOOST_GET_CONST(bool, ctx.GetAttr("is_test")));
}
DECLARE_NO_NEED_BUFFER_VARS_INFERER(TestNoNeedBufferVarsInferer, "X1", "X2");
......
......@@ -30,7 +30,7 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs,
const std::vector<std::string> *callstack = nullptr;
auto iter = attrs.find(OpProtoAndCheckerMaker::OpCreationCallstackAttrName());
if (iter != attrs.end()) {
callstack = &boost::get<std::vector<std::string>>(iter->second);
callstack = &BOOST_GET_CONST(std::vector<std::string>, iter->second);
if (callstack->empty()) callstack = nullptr;
}
......
......@@ -422,7 +422,7 @@ void OpDesc::SetAttr(const std::string &name, const Attribute &v) {
// here if we meet this issue
proto::AttrType attr_type = static_cast<proto::AttrType>(v.which() - 1);
if (attr_type == proto::AttrType::INTS &&
boost::get<std::vector<int>>(v).size() == 0u) {
BOOST_GET_CONST(std::vector<int>, v).size() == 0u) {
// Find current attr via attr name and set the correct attribute value
const proto::OpProto::Attr &attr = GetProtoAttr(name);
switch (attr.type()) {
......@@ -472,7 +472,7 @@ void OpDesc::SetAttr(const std::string &name, const Attribute &v) {
// In order to set bool attr properly
if (attr_type == proto::AttrType::INT && HasProtoAttr(name) &&
GetProtoAttr(name).type() == proto::AttrType::BOOLEAN) {
this->attrs_[name] = static_cast<bool>(boost::get<int>(v));
this->attrs_[name] = static_cast<bool>(BOOST_GET_CONST(int, v));
need_update_ = true;
return;
}
......@@ -529,7 +529,7 @@ Attribute OpDesc::GetNullableAttr(const std::string &name) const {
std::vector<int> OpDesc::GetBlocksAttrIds(const std::string &name) const {
auto it = attrs_.find(name);
PADDLE_ENFORCE(it != attrs_.end(), "Attribute %s is not found", name);
auto blocks = boost::get<std::vector<BlockDesc *>>(it->second);
auto blocks = BOOST_GET_CONST(std::vector<BlockDesc *>, it->second);
std::vector<int> ids;
for (auto n : blocks) {
......@@ -542,7 +542,7 @@ std::vector<int> OpDesc::GetBlocksAttrIds(const std::string &name) const {
int OpDesc::GetBlockAttrId(const std::string &name) const {
auto it = attrs_.find(name);
PADDLE_ENFORCE(it != attrs_.end(), "Attribute %s is not found", name);
return boost::get<BlockDesc *>(it->second)->ID();
return BOOST_GET_CONST(BlockDesc *, it->second)->ID();
}
const std::unordered_map<std::string, Attribute> &OpDesc::GetAttrMap() const {
......@@ -564,7 +564,7 @@ void OpDesc::RenameOutput(const std::string &old_name,
auto it = attrs_.find(framework::OpProtoAndCheckerMaker::OpRoleVarAttrName());
if (it != attrs_.end()) {
auto &op_vars = boost::get<std::vector<std::string>>(it->second);
auto &op_vars = BOOST_GET(std::vector<std::string>, it->second);
std::replace(op_vars.begin(), op_vars.end(), old_name, new_name);
}
......@@ -579,7 +579,7 @@ void OpDesc::RenameInput(const std::string &old_name,
auto it = attrs_.find(framework::OpProtoAndCheckerMaker::OpRoleVarAttrName());
if (it != attrs_.end()) {
auto &op_vars = boost::get<std::vector<std::string>>(it->second);
auto &op_vars = BOOST_GET(std::vector<std::string>, it->second);
std::replace(op_vars.begin(), op_vars.end(), old_name, new_name);
}
......
......@@ -85,7 +85,7 @@ class OpDesc {
T GetAttrIfExists(const std::string &name) const {
T result{};
if (HasAttr(name)) {
result = boost::get<T>(GetAttr(name));
result = BOOST_GET_CONST(T, GetAttr(name));
}
return result;
}
......
......@@ -161,7 +161,7 @@ void OperatorBase::Run(const Scope& scope, const platform::Place& place) {
#ifndef PADDLE_WITH_CUDA
PADDLE_THROW("Cannot run operator on place %s", place);
#else
auto dev_id = boost::get<platform::CUDAPlace>(place).device;
auto dev_id = BOOST_GET_CONST(platform::CUDAPlace, place).device;
platform::SetDeviceId(dev_id);
#endif
}
......
......@@ -157,7 +157,7 @@ class OperatorBase {
inline const T& Attr(const std::string& name) const {
PADDLE_ENFORCE(attrs_.find(name) != attrs_.end(),
"%s should be in AttributeMap", name);
return boost::get<T>(attrs_.at(name));
return BOOST_GET_CONST(T, attrs_.at(name));
}
const AttributeMap& Attrs() const { return attrs_; }
......@@ -256,7 +256,7 @@ class ExecutionContext {
template <typename T>
inline const T& Attr(const std::string& name) const {
return boost::get<T>(GetAttr(name));
return BOOST_GET_CONST(T, GetAttr(name));
}
virtual const Attribute& GetAttr(const std::string& name) const {
......
......@@ -533,7 +533,7 @@ TEST(ExecutionContextAttrAndInOut, new_api) {
ASSERT_EQ(exe_context.OutputSize("output"), 1u);
auto attr_map = exe_context.Attrs();
ASSERT_EQ(boost::get<float>(attr_map["scale"]), 3.14f);
ASSERT_EQ(BOOST_GET(float, attr_map["scale"]), 3.14f);
ASSERT_EQ(exe_context.Type(), "test_operator");
}
......
......@@ -363,17 +363,17 @@ ir::Graph *ParallelExecutorPrivate::ApplyMemoryOptimizePass(ir::Graph *graph) {
if (platform::is_gpu_place(place)) {
if (IsFastEagerDeletionModeEnabled()) {
gc.reset(new UnsafeFastGPUGarbageCollector(
boost::get<platform::CUDAPlace>(place), max_memory_size));
BOOST_GET_CONST(platform::CUDAPlace, place), max_memory_size));
} else {
gc.reset(new StreamGarbageCollector(
boost::get<platform::CUDAPlace>(place), max_memory_size));
BOOST_GET_CONST(platform::CUDAPlace, place), max_memory_size));
}
VLOG(10) << "Created " << i << "-th GarbageCollector at " << place;
} else {
#endif
if (platform::is_cpu_place(place)) {
gc.reset(new CPUGarbageCollector(boost::get<platform::CPUPlace>(place),
max_memory_size));
gc.reset(new CPUGarbageCollector(
BOOST_GET_CONST(platform::CPUPlace, place), max_memory_size));
VLOG(10) << "Created GarbageCollector at " << place;
} else {
PADDLE_THROW(platform::errors::PreconditionNotMet(
......
......@@ -135,7 +135,7 @@ const std::vector<std::string> ProgramDesc::GetFeedTargetNames() {
std::vector<std::string> feed_target_names;
for (auto *op : global_block.AllOps()) {
if (op->Type() == kFeedOpType) {
size_t col = boost::get<int>(op->GetAttr("col"));
size_t col = BOOST_GET_CONST(int, op->GetAttr("col"));
if (col >= feed_target_names.size()) {
feed_target_names.resize(col + 1);
}
......@@ -152,7 +152,7 @@ const std::vector<std::string> ProgramDesc::GetFetchTargetNames() {
std::vector<std::string> fetch_target_names;
for (auto *op : global_block.AllOps()) {
if (op->Type() == kFetchOpType) {
size_t col = boost::get<int>(op->GetAttr("col"));
size_t col = BOOST_GET_CONST(int, op->GetAttr("col"));
if (col >= fetch_target_names.size()) {
fetch_target_names.resize(col + 1);
}
......
......@@ -405,8 +405,8 @@ std::tuple<framework::ProgramDesc, std::map<int, int>> PruneBackward(
for (size_t i = 0; i < origin_clone.Size(); i++) {
auto block_ops = origin_clone.Block(i).AllOps();
for (auto op : block_ops) {
int op_role = boost::get<int>(
op->GetAttr(OpProtoAndCheckerMaker::OpRoleAttrName()));
int op_role = BOOST_GET_MUTABLE(
int, op->GetAttr(OpProtoAndCheckerMaker::OpRoleAttrName()));
if (op_role == (static_cast<int>(OpRole::kBackward) |
static_cast<int>(OpRole::kLoss))) {
op->SetIsTarget(false);
......
......@@ -51,36 +51,36 @@ void TensorCopy(const Tensor& src, const platform::Place& dst_place,
auto size = src.numel() * SizeOfType(src.type());
if (platform::is_cpu_place(src_place) && platform::is_cpu_place(dst_place)) {
memory::Copy(boost::get<platform::CPUPlace>(dst_place), dst_ptr,
boost::get<platform::CPUPlace>(src_place), src_ptr, size);
memory::Copy(BOOST_GET_CONST(platform::CPUPlace, dst_place), dst_ptr,
BOOST_GET_CONST(platform::CPUPlace, src_place), src_ptr, size);
}
#ifdef PADDLE_WITH_CUDA
else if (platform::is_gpu_place(src_place) && // NOLINT
platform::is_cpu_place(dst_place)) {
auto src_gpu_place = boost::get<platform::CUDAPlace>(src_place);
auto dst_cpu_place = boost::get<platform::CPUPlace>(dst_place);
auto src_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, src_place);
auto dst_cpu_place = BOOST_GET_CONST(platform::CPUPlace, dst_place);
auto ctx_place = ctx.GetPlace();
PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx_place), true);
auto ctx_gpu_place = boost::get<platform::CUDAPlace>(ctx_place);
auto ctx_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, ctx_place);
PADDLE_ENFORCE_EQ(src_gpu_place, ctx_gpu_place);
auto stream =
reinterpret_cast<const platform::CUDADeviceContext&>(ctx).stream();
memory::Copy(dst_cpu_place, dst_ptr, src_gpu_place, src_ptr, size, stream);
} else if (platform::is_cpu_place(src_place) &&
platform::is_gpu_place(dst_place)) {
auto src_cpu_place = boost::get<platform::CPUPlace>(src_place);
auto dst_gpu_place = boost::get<platform::CUDAPlace>(dst_place);
auto src_cpu_place = BOOST_GET_CONST(platform::CPUPlace, src_place);
auto dst_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, dst_place);
auto ctx_place = ctx.GetPlace();
PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx_place), true);
auto ctx_gpu_place = boost::get<platform::CUDAPlace>(ctx_place);
auto ctx_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, ctx_place);
PADDLE_ENFORCE_EQ(dst_gpu_place, ctx_gpu_place);
auto stream =
reinterpret_cast<const platform::CUDADeviceContext&>(ctx).stream();
memory::Copy(dst_gpu_place, dst_ptr, src_cpu_place, src_ptr, size, stream);
} else if (platform::is_gpu_place(src_place) &&
platform::is_gpu_place(dst_place)) {
auto src_gpu_place = boost::get<platform::CUDAPlace>(src_place);
auto dst_gpu_place = boost::get<platform::CUDAPlace>(dst_place);
auto src_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, src_place);
auto dst_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, dst_place);
auto ctx_place = ctx.GetPlace();
PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx_place), true);
auto stream =
......@@ -144,29 +144,30 @@ void TensorCopySync(const Tensor& src, const platform::Place& dst_place,
auto size = src.numel() * SizeOfType(src.type());
if (platform::is_cpu_place(src_place) && platform::is_cpu_place(dst_place)) {
memory::Copy(boost::get<platform::CPUPlace>(dst_place), dst_ptr,
boost::get<platform::CPUPlace>(src_place), src_ptr, size);
memory::Copy(BOOST_GET_CONST(platform::CPUPlace, dst_place), dst_ptr,
BOOST_GET_CONST(platform::CPUPlace, src_place), src_ptr, size);
}
#ifdef PADDLE_WITH_CUDA
else if (platform::is_gpu_place(src_place) && // NOLINT
platform::is_cpu_place(dst_place)) {
auto src_gpu_place = boost::get<platform::CUDAPlace>(src_place);
auto dst_cpu_place = boost::get<platform::CPUPlace>(dst_place);
auto src_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, src_place);
auto dst_cpu_place = BOOST_GET_CONST(platform::CPUPlace, dst_place);
memory::Copy(dst_cpu_place, dst_ptr, src_gpu_place, src_ptr, size, nullptr);
} else if (platform::is_cpu_place(src_place) &&
platform::is_gpu_place(dst_place)) {
auto src_cpu_place = boost::get<platform::CPUPlace>(src_place);
auto dst_gpu_place = boost::get<platform::CUDAPlace>(dst_place);
auto src_cpu_place = BOOST_GET_CONST(platform::CPUPlace, src_place);
auto dst_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, dst_place);
memory::Copy(dst_gpu_place, dst_ptr, src_cpu_place, src_ptr, size, nullptr);
} else if (platform::is_gpu_place(src_place) &&
platform::is_gpu_place(dst_place)) {
auto src_gpu_place = boost::get<platform::CUDAPlace>(src_place);
auto dst_gpu_place = boost::get<platform::CUDAPlace>(dst_place);
auto src_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, src_place);
auto dst_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, dst_place);
memory::Copy(dst_gpu_place, dst_ptr, src_gpu_place, src_ptr, size, nullptr);
} else if (platform::is_cuda_pinned_place(src_place) &&
platform::is_gpu_place(dst_place)) {
auto src_pinned_place = boost::get<platform::CUDAPinnedPlace>(src_place);
auto dst_gpu_place = boost::get<platform::CUDAPlace>(dst_place);
auto src_pinned_place =
BOOST_GET_CONST(platform::CUDAPinnedPlace, src_place);
auto dst_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, dst_place);
memory::Copy(dst_gpu_place, dst_ptr, src_pinned_place, src_ptr, size,
nullptr);
} else {
......@@ -419,7 +420,7 @@ void TensorToStream(std::ostream& os, const Tensor& tensor,
while (size != 0) {
size_t size_to_write = std::min(kBufSize, static_cast<size_t>(size));
memory::Copy(cpu, buf.get(),
boost::get<platform::CUDAPlace>(tensor.place()),
BOOST_GET_CONST(platform::CUDAPlace, tensor.place()),
reinterpret_cast<const void*>(data), size_to_write,
gpu_dev_ctx.stream());
gpu_dev_ctx.Wait();
......
......@@ -94,14 +94,14 @@ void TensorFromArray(const T* src, const size_t& array_size,
auto size = array_size * sizeof(T);
if (platform::is_cpu_place(dst_place)) {
memory::Copy(boost::get<platform::CPUPlace>(dst_place), dst_ptr, src_place,
src_ptr, size);
memory::Copy(BOOST_GET_CONST(platform::CPUPlace, dst_place), dst_ptr,
src_place, src_ptr, size);
}
#ifdef PADDLE_WITH_CUDA
else if (platform::is_gpu_place(dst_place)) { // NOLINT
memory::Copy(
boost::get<platform::CUDAPlace>(dst_place), dst_ptr, src_place, src_ptr,
size,
BOOST_GET_CONST(platform::CUDAPlace, dst_place), dst_ptr, src_place,
src_ptr, size,
reinterpret_cast<const platform::CUDADeviceContext&>(ctx).stream());
}
#endif
......@@ -117,14 +117,14 @@ void TensorFromVector(const std::vector<T>& src,
auto size = src.size() * sizeof(T);
if (platform::is_cpu_place(dst_place)) {
memory::Copy(boost::get<platform::CPUPlace>(dst_place), dst_ptr, src_place,
src_ptr, size);
memory::Copy(BOOST_GET_CONST(platform::CPUPlace, dst_place), dst_ptr,
src_place, src_ptr, size);
}
#ifdef PADDLE_WITH_CUDA
else if (platform::is_gpu_place(dst_place)) { // NOLINT
memory::Copy(
boost::get<platform::CUDAPlace>(dst_place), dst_ptr, src_place, src_ptr,
size,
BOOST_GET_CONST(platform::CUDAPlace, dst_place), dst_ptr, src_place,
src_ptr, size,
reinterpret_cast<const platform::CUDADeviceContext&>(ctx).stream());
}
#endif
......@@ -154,12 +154,13 @@ void TensorToVector(const Tensor& src, const platform::DeviceContext& ctx,
if (platform::is_cpu_place(src.place())) {
memory::Copy(dst_place, dst_ptr,
boost::get<platform::CPUPlace>(src.place()), src_ptr, size);
BOOST_GET_CONST(platform::CPUPlace, src.place()), src_ptr,
size);
}
#ifdef PADDLE_WITH_CUDA
else if (platform::is_gpu_place(src.place())) { // NOLINT
memory::Copy(
dst_place, dst_ptr, boost::get<platform::CUDAPlace>(src.place()),
dst_place, dst_ptr, BOOST_GET_CONST(platform::CUDAPlace, src.place()),
src_ptr, size,
reinterpret_cast<const platform::CUDADeviceContext&>(ctx).stream());
}
......@@ -177,8 +178,8 @@ void TensorToVector(const Tensor& src, std::vector<T>* dst) {
PADDLE_ENFORCE_EQ(platform::is_cpu_place(src.place()), true);
memory::Copy(dst_place, dst_ptr, boost::get<platform::CPUPlace>(src.place()),
src_ptr, size);
memory::Copy(dst_place, dst_ptr,
BOOST_GET_CONST(platform::CPUPlace, src.place()), src_ptr, size);
}
std::ostream& operator<<(std::ostream& os, const Tensor& t);
......
......@@ -25,9 +25,9 @@ TEST(Tuple, Make) {
paddle::framework::Tuple* tuple = paddle::framework::make_tuple(element_type);
EXPECT_EQ(boost::get<int>(tuple->get(0)), 12);
EXPECT_EQ(boost::get<float>(tuple->get(1)), 12.0f);
EXPECT_EQ(boost::get<std::string>(tuple->get(2)), "ElementVar");
EXPECT_EQ(BOOST_GET(int, tuple->get(0)), 12);
EXPECT_EQ(BOOST_GET(float, tuple->get(1)), 12.0f);
EXPECT_EQ(BOOST_GET(std::string, tuple->get(2)), "ElementVar");
delete tuple;
}
......
......@@ -123,7 +123,7 @@ class GradOpBaseMakerBase {
template <typename T>
inline const T& Attr(const std::string& name) const {
return boost::get<T>(GetAttr(name));
return BOOST_GET_CONST(T, GetAttr(name));
}
const std::string& ForwardOpType() const { return type_; }
......
......@@ -123,7 +123,7 @@ void NCCLParallelContext::Init() {
} else {
BcastNCCLId(&nccl_id, 0);
}
int gpu_id = boost::get<platform::CUDAPlace>(place_).device;
int gpu_id = BOOST_GET_CONST(platform::CUDAPlace, place_).device;
VLOG(0) << "init nccl context nranks: " << strategy_.nranks_
<< " local rank: " << strategy_.local_rank_ << " gpu id: " << gpu_id;
......
......@@ -120,7 +120,7 @@ class OpBase {
template <typename T>
inline const T& Attr(const std::string& name) const {
return boost::get<T>(GetAttr(name));
return BOOST_GET_CONST(T, GetAttr(name));
}
size_t id() const { return id_; }
......
......@@ -362,7 +362,7 @@ TEST(test_layer, test_dygraph_execution_context) {
ASSERT_EQ(dy_exe_context.InputName("X"), "vin");
ASSERT_EQ(dy_exe_context.HasAttr("axis"), true);
auto attr_map = dy_exe_context.Attrs();
ASSERT_EQ(boost::get<int>(attr_map["axis"]), 1);
ASSERT_EQ(BOOST_GET(int, attr_map["axis"]), 1);
ASSERT_EQ(dy_exe_context.OutputSize("Out"), 1u);
ASSERT_EQ(dy_exe_context.HasOutput("Out"), true);
}
......
......@@ -177,9 +177,9 @@ void RenameAndGetOutputs(
auto out_var_name = op_desc.Output("Output").front();
auto filter_shape = in_vars[filter_var_name]->Var()->GetShape();
const std::vector<int> strides =
boost::get<std::vector<int>>(op_desc.GetAttr("strides"));
BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("strides"));
const std::vector<int> paddings =
boost::get<std::vector<int>>(op_desc.GetAttr("paddings"));
BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("paddings"));
if (same_hierarchy_conv2d_num_map[input_var_name] > 0) {
(*output_names_with_id)
.insert(out_var_name + std::to_string(var2id[out_var_name]));
......
......@@ -109,7 +109,7 @@ bool PaddleTensorToLoDTensor(const PaddleTensor &pt, framework::LoDTensor *t,
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
auto *dev_ctx =
static_cast<const platform::CUDADeviceContext *>(pool.Get(place));
auto dst_gpu_place = boost::get<platform::CUDAPlace>(place);
auto dst_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, place);
memory::Copy(dst_gpu_place, static_cast<void *>(input_ptr),
platform::CPUPlace(), pt.data.data(), pt.data.length(),
dev_ctx->stream());
......@@ -366,7 +366,7 @@ bool AnalysisPredictor::SetFeed(const std::vector<PaddleTensor> &inputs,
}
idx = feed_names_[name];
} else {
idx = boost::get<int>(feeds_[i]->GetAttr("col"));
idx = BOOST_GET_CONST(int, feeds_[i]->GetAttr("col"));
}
framework::SetFeedVariable(scope, *input, "feed", idx);
}
......@@ -398,11 +398,11 @@ bool AnalysisPredictor::GetFetch(std::vector<PaddleTensor> *outputs,
VLOG(3) << "Predictor::get_fetch";
outputs->resize(fetches_.size());
for (size_t i = 0; i < fetches_.size(); ++i) {
int idx = boost::get<int>(fetches_[i]->GetAttr("col"));
int idx = BOOST_GET_CONST(int, fetches_[i]->GetAttr("col"));
PADDLE_ENFORCE((size_t)idx == i);
framework::FetchType &fetch_var =
framework::GetFetchVariable(*scope, "fetch", idx);
auto &fetch = boost::get<framework::LoDTensor>(fetch_var);
auto &fetch = BOOST_GET(framework::LoDTensor, fetch_var);
auto type = fetch.type();
auto output = &(outputs->at(i));
output->name = fetches_[idx]->Input("X")[0];
......@@ -619,7 +619,7 @@ void AnalysisPredictor::PrepareFeedFetch() {
CreateFeedFetchVar(sub_scope_);
for (auto *op : inference_program_->Block(0).AllOps()) {
if (op->Type() == "feed") {
int idx = boost::get<int>(op->GetAttr("col"));
int idx = BOOST_GET_CONST(int, op->GetAttr("col"));
if (feeds_.size() <= static_cast<size_t>(idx)) {
feeds_.resize(idx + 1);
}
......@@ -627,7 +627,7 @@ void AnalysisPredictor::PrepareFeedFetch() {
feed_names_[op->Output("Out")[0]] = idx;
idx2feeds_[idx] = op->Output("Out")[0];
} else if (op->Type() == "fetch") {
int idx = boost::get<int>(op->GetAttr("col"));
int idx = BOOST_GET_CONST(int, op->GetAttr("col"));
if (fetches_.size() <= static_cast<size_t>(idx)) {
fetches_.resize(idx + 1);
}
......@@ -683,7 +683,7 @@ std::unique_ptr<ZeroCopyTensor> AnalysisPredictor::GetInputTensor(
if (platform::is_cpu_place(place_)) {
res->SetPlace(PaddlePlace::kCPU);
} else {
auto gpu_place = boost::get<platform::CUDAPlace>(place_);
auto gpu_place = BOOST_GET_CONST(platform::CUDAPlace, place_);
res->SetPlace(PaddlePlace::kGPU, gpu_place.GetDeviceId());
}
......@@ -700,7 +700,7 @@ std::unique_ptr<ZeroCopyTensor> AnalysisPredictor::GetOutputTensor(
if (platform::is_cpu_place(place_)) {
res->SetPlace(PaddlePlace::kCPU);
} else {
auto gpu_place = boost::get<platform::CUDAPlace>(place_);
auto gpu_place = BOOST_GET_CONST(platform::CUDAPlace, place_);
res->SetPlace(PaddlePlace::kGPU, gpu_place.GetDeviceId());
}
return res;
......@@ -834,7 +834,7 @@ bool AnalysisPredictor::SaveTrtCalibToDisk() {
for (auto &op_desc : block.AllOps()) {
if (op_desc->Type() == "tensorrt_engine") {
std::string engine_name =
boost::get<std::string>(op_desc->GetAttr("engine_key"));
BOOST_GET_CONST(std::string, op_desc->GetAttr("engine_key"));
if (!Singleton<TRTCalibratorEngineManager>::Global().Has(engine_name)) {
LOG(ERROR) << "You should run the predictor(with trt) on the real data "
"to generate calibration info";
......
......@@ -46,14 +46,14 @@ std::string num2str(T a) {
void NativePaddlePredictor::PrepareFeedFetch() {
for (auto *op : inference_program_->Block(0).AllOps()) {
if (op->Type() == "feed") {
int idx = boost::get<int>(op->GetAttr("col"));
int idx = BOOST_GET_CONST(int, op->GetAttr("col"));
if (feeds_.size() <= static_cast<size_t>(idx)) {
feeds_.resize(idx + 1);
}
feeds_[idx] = op;
feed_names_[op->Output("Out")[0]] = idx;
} else if (op->Type() == "fetch") {
int idx = boost::get<int>(op->GetAttr("col"));
int idx = BOOST_GET_CONST(int, op->GetAttr("col"));
if (fetchs_.size() <= static_cast<size_t>(idx)) {
fetchs_.resize(idx + 1);
}
......@@ -234,7 +234,7 @@ bool NativePaddlePredictor::SetFeed(const std::vector<PaddleTensor> &inputs,
platform::DeviceContextPool::Instance();
auto *dev_ctx =
static_cast<const platform::CUDADeviceContext *>(pool.Get(place_));
auto dst_gpu_place = boost::get<platform::CUDAPlace>(place_);
auto dst_gpu_place = BOOST_GET_CONST(platform::CUDAPlace, place_);
memory::Copy(dst_gpu_place, static_cast<void *>(input_ptr),
platform::CPUPlace(), inputs[i].data.data(),
inputs[i].data.length(), dev_ctx->stream());
......@@ -253,7 +253,7 @@ bool NativePaddlePredictor::SetFeed(const std::vector<PaddleTensor> &inputs,
if (config_.specify_input_name) {
idx = feed_names_[inputs[i].name];
} else {
idx = boost::get<int>(feeds_[i]->GetAttr("col"));
idx = BOOST_GET_CONST(int, feeds_[i]->GetAttr("col"));
}
framework::SetFeedVariable(scope, input, "feed", idx);
}
......@@ -284,11 +284,11 @@ bool NativePaddlePredictor::GetFetch(std::vector<PaddleTensor> *outputs,
VLOG(3) << "Predictor::get_fetch";
outputs->resize(fetchs_.size());
for (size_t i = 0; i < fetchs_.size(); ++i) {
int idx = boost::get<int>(fetchs_[i]->GetAttr("col"));
int idx = BOOST_GET_CONST(int, fetchs_[i]->GetAttr("col"));
PADDLE_ENFORCE((size_t)idx == i);
framework::FetchType &fetch_var =
framework::GetFetchVariable(*scope, "fetch", idx);
auto fetch = boost::get<framework::LoDTensor>(fetch_var);
auto fetch = BOOST_GET_CONST(framework::LoDTensor, fetch_var);
auto type = fetch.type();
auto output = &(outputs->at(i));
output->name = fetchs_[idx]->Input("X")[0];
......
......@@ -108,7 +108,7 @@ void MainWord2Vec(bool use_gpu) {
TestInference<platform::CPUPlace>(config.model_dir, cpu_feeds, cpu_fetchs1);
auto output1_tensor = boost::get<paddle::framework::LoDTensor>(output1);
auto output1_tensor = BOOST_GET(paddle::framework::LoDTensor, output1);
float* lod_data = output1_tensor.data<float>();
for (int i = 0; i < output1_tensor.numel(); ++i) {
EXPECT_LT(lod_data[i] - data[i], ACC_DIFF);
......@@ -155,7 +155,7 @@ void MainImageClassification(bool use_gpu) {
size_t len = outputs[0].data.length();
float* data = static_cast<float*>(outputs[0].data.data());
float* lod_data =
boost::get<paddle::framework::LoDTensor>(output1).data<float>();
BOOST_GET(paddle::framework::LoDTensor, output1).data<float>();
for (size_t j = 0; j < len / sizeof(float); ++j) {
EXPECT_NEAR(lod_data[j], data[j], ACC_DIFF);
}
......@@ -209,7 +209,7 @@ void MainThreadsWord2Vec(bool use_gpu) {
}
// check outputs correctness
auto ref_tensor = boost::get<paddle::framework::LoDTensor>(refs[tid]);
auto ref_tensor = BOOST_GET(paddle::framework::LoDTensor, refs[tid]);
float* ref_data = ref_tensor.data<float>();
EXPECT_EQ(ref_tensor.numel(), static_cast<int64_t>(len / sizeof(float)));
for (int i = 0; i < ref_tensor.numel(); ++i) {
......@@ -262,7 +262,7 @@ void MainThreadsImageClassification(bool use_gpu) {
ASSERT_EQ(local_outputs.size(), 1UL);
const size_t len = local_outputs[0].data.length();
float* data = static_cast<float*>(local_outputs[0].data.data());
auto ref_tensor = boost::get<paddle::framework::LoDTensor>(refs[tid]);
auto ref_tensor = BOOST_GET(paddle::framework::LoDTensor, refs[tid]);
float* ref_data = ref_tensor.data<float>();
EXPECT_EQ((size_t)ref_tensor.numel(), len / sizeof(float));
for (int i = 0; i < ref_tensor.numel(); ++i) {
......
......@@ -133,7 +133,7 @@ void ZeroCopyTensor::copy_to_cpu(T *data) {
} else {
#ifdef PADDLE_WITH_CUDA
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
auto gpu_place = boost::get<platform::CUDAPlace>(t_place);
auto gpu_place = BOOST_GET_CONST(platform::CUDAPlace, t_place);
auto *dev_ctx =
static_cast<const platform::CUDADeviceContext *>(pool.Get(gpu_place));
memory::Copy(platform::CPUPlace(), static_cast<void *>(data), gpu_place,
......
......@@ -51,7 +51,7 @@ bool AnalysisPredictor::MkldnnQuantizer::CalculateScales() {
std::map<std::string, std::map<std::string, LoDTensor>> gathered_data;
for (const auto* op : predictor_.inference_program_->Block(0).AllOps()) {
if (op->HasAttr("use_quantizer") &&
boost::get<bool>(op->GetAttr("use_quantizer"))) {
BOOST_GET_CONST(bool, op->GetAttr("use_quantizer"))) {
const VariableNameMap& connections_in = op->Inputs();
const VariableNameMap& connections_out = op->Outputs();
......
......@@ -118,7 +118,7 @@ void MemoryCopyAsync(const platform::Place& dst_place, void* dst_data,
LOG(FATAL) << "lite::MemoryCopy CPU->GPU is not yet implemented.";
} else if (platform::is_gpu_place(dst_place) &&
platform::is_gpu_place(src_place)) {
auto gpu_place = boost::get<platform::CUDAPlace>(src_place);
auto gpu_place = BOOST_GET_CONST(platform::CUDAPlace, src_place);
memory::Copy(
gpu_place, dst_data, gpu_place, src_data, size,
static_cast<const platform::CUDADeviceContext&>(ctx).stream());
......
......@@ -58,7 +58,7 @@ class ActivationOpConverter : public OpConverter {
RreplenishLayerAndOutput(layer, op_type_, {output_name}, test_mode);
if (op_desc.HasAttr("out_scale")) {
#if IS_TRT_VERSION_GE(5130)
float out_scale = boost::get<float>(op_desc.GetAttr("out_scale"));
float out_scale = BOOST_GET_CONST(float, op_desc.GetAttr("out_scale"));
engine_->SetTensorDynamicRange(layer->getOutput(0), out_scale);
#endif
}
......
......@@ -64,7 +64,7 @@ class BatchNormOpConverter : public OpConverter {
auto* Mean_v = scope.FindVar(op_desc.Input("Mean").front());
auto* Scale_v = scope.FindVar(op_desc.Input("Scale").front());
auto* Variance_v = scope.FindVar(op_desc.Input("Variance").front());
const float eps = boost::get<float>(op_desc.GetAttr("epsilon"));
const float eps = BOOST_GET_CONST(float, op_desc.GetAttr("epsilon"));
PADDLE_ENFORCE_NOT_NULL(
Bias_v,
......
......@@ -33,7 +33,7 @@ class ConcatOpConverter : public OpConverter {
for (auto& input_name : op_desc.Input("X")) {
itensors.push_back(engine_->GetITensor(input_name));
}
int axis = boost::get<int>(op_desc.GetAttr("axis"));
int axis = BOOST_GET_CONST(int, op_desc.GetAttr("axis"));
PADDLE_ENFORCE(axis > 0,
"The axis attr of Concat op should be large than 0 for trt");
......
......@@ -47,14 +47,14 @@ void ConvertConv2d(TensorRTEngine* engine, const framework::proto::OpDesc& op,
"Can not find %s presistale var in scope.", filter_var_name));
auto* Y_t = Y_v->GetMutable<framework::LoDTensor>();
float* weight_data = nullptr;
bool enable_int8 = boost::get<bool>(op_desc.HasAttr("enable_int8"));
bool enable_int8 = op_desc.HasAttr("enable_int8");
if (enable_int8) {
#if IS_TRT_VERSION_GE(5000)
CHECK(op_desc.HasAttr("Input_scale"));
float in_scale = boost::get<float>(op_desc.GetAttr("Input_scale"));
float in_scale = BOOST_GET_CONST(float, op_desc.GetAttr("Input_scale"));
auto weight_scale =
boost::get<std::vector<float>>(op_desc.GetAttr("weight_scale"));
BOOST_GET_CONST(std::vector<float>, op_desc.GetAttr("weight_scale"));
weight_data = engine->GetWeightCPUData(op_desc.Input("Filter").front(), Y_t,
true, weight_scale);
engine->SetTensorDynamicRange(X, in_scale);
......@@ -73,13 +73,13 @@ void ConvertConv2d(TensorRTEngine* engine, const framework::proto::OpDesc& op,
const int n_input = Y_t->dims()[1];
const int filter_h = Y_t->dims()[2];
const int filter_w = Y_t->dims()[3];
const int groups = boost::get<int>(op_desc.GetAttr("groups"));
const int groups = BOOST_GET_CONST(int, op_desc.GetAttr("groups"));
const std::vector<int> dilations =
boost::get<std::vector<int>>(op_desc.GetAttr("dilations"));
BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("dilations"));
const std::vector<int> strides =
boost::get<std::vector<int>>(op_desc.GetAttr("strides"));
BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("strides"));
const std::vector<int> paddings =
boost::get<std::vector<int>>(op_desc.GetAttr("paddings"));
BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("paddings"));
nvinfer1::DimsHW nv_ksize(filter_h, filter_w);
nvinfer1::DimsHW nv_dilations(dilations[0], dilations[1]);
......
......@@ -29,12 +29,13 @@ class DropoutOpConverter : public OpConverter {
framework::OpDesc op_desc(op, nullptr);
// Declare inputs
auto* input1 = engine_->GetITensor(op_desc.Input("X")[0]);
float dropout_prob = boost::get<float>(op_desc.GetAttr("dropout_prob"));
float dropout_prob =
BOOST_GET_CONST(float, op_desc.GetAttr("dropout_prob"));
std::string downgrade_in_infer = "";
if (op_desc.HasAttr("dropout_implementation")) {
downgrade_in_infer =
boost::get<std::string>(op_desc.GetAttr("dropout_implementation"));
downgrade_in_infer = BOOST_GET_CONST(
std::string, op_desc.GetAttr("dropout_implementation"));
}
if (!downgrade_in_infer.empty() &&
......
......@@ -82,7 +82,7 @@ class ElementwiseWeightOpConverter : public OpConverter {
if (op_desc.HasAttr("enable_int8")) {
#if IS_TRT_VERSION_GE(5000)
CHECK(op_desc.HasAttr("X_scale"));
float x_scale = boost::get<float>(op_desc.GetAttr("X_scale"));
float x_scale = BOOST_GET_CONST(float, op_desc.GetAttr("X_scale"));
engine_->SetTensorDynamicRange(X, x_scale);
#endif
}
......@@ -188,7 +188,7 @@ class ElementwiseTensorOpConverter : public OpConverter {
nvinfer1::Dims dims_x = X->getDimensions();
nvinfer1::Dims dims_y = Y->getDimensions();
int axis = boost::get<int>(op_desc.GetAttr("axis"));
int axis = BOOST_GET_CONST(int, op_desc.GetAttr("axis"));
auto output_name = op_desc.Output("Out")[0];
auto common_func = [&](nvinfer1::ILayer* layer) {
......@@ -197,8 +197,8 @@ class ElementwiseTensorOpConverter : public OpConverter {
#if IS_TRT_VERSION_GE(5000)
CHECK(op_desc.HasAttr("X_scale"));
CHECK(op_desc.HasAttr("Y_scale"));
float x_scale = boost::get<float>(op_desc.GetAttr("X_scale"));
float y_scale = boost::get<float>(op_desc.GetAttr("Y_scale"));
float x_scale = BOOST_GET_CONST(float, op_desc.GetAttr("X_scale"));
float y_scale = BOOST_GET_CONST(float, op_desc.GetAttr("Y_scale"));
engine_->SetTensorDynamicRange(X, x_scale);
engine_->SetTensorDynamicRange(Y, y_scale);
#endif
......
......@@ -79,7 +79,7 @@ class EmbEltwiseLayerNormOpConverter : public OpConverter {
get_persistable_data(op_desc.Input("Scale").front(), &scale_dims);
int64_t bias_size = framework::product(bias_dims);
int64_t scale_size = framework::product(scale_dims);
float eps = boost::get<float>(op_desc.GetAttr("epsilon"));
float eps = BOOST_GET_CONST(float, op_desc.GetAttr("epsilon"));
nvinfer1::ILayer* layer = nullptr;
if (engine_->with_dynamic_shape()) {
......
......@@ -46,24 +46,25 @@ class FcOpConverter : public OpConverter {
auto* Y_t = Y_v->GetMutable<framework::LoDTensor>();
const int x_num_col_dims =
op_desc.HasAttr("x_num_col_dims")
? boost::get<int>(op_desc.GetAttr("x_num_col_dims"))
? BOOST_GET_CONST(int, op_desc.GetAttr("x_num_col_dims"))
: (op_desc.HasAttr("in_num_col_dims")
? boost::get<int>(op_desc.GetAttr("in_num_col_dims"))
? BOOST_GET_CONST(int, op_desc.GetAttr("in_num_col_dims"))
: 1);
const std::string activation_type =
op_desc.HasAttr("activation_type")
? boost::get<std::string>(op_desc.GetAttr("activation_type"))
? BOOST_GET_CONST(std::string, op_desc.GetAttr("activation_type"))
: "";
// This may trigger a GPU->CPU copy, because TRT's weight can only be
// assigned from CPU memory, which can't be avoided.
float* weight_data = nullptr;
bool enable_int8 = boost::get<bool>(op_desc.HasAttr("enable_int8"));
bool enable_int8 = op_desc.HasAttr("enable_int8");
if (enable_int8) {
#if IS_TRT_VERSION_GE(5000)
CHECK(op_desc.HasAttr(i_name + "_scale"));
float in_scale = boost::get<float>(op_desc.GetAttr(i_name + "_scale"));
float in_scale =
BOOST_GET_CONST(float, op_desc.GetAttr(i_name + "_scale"));
auto weight_scale =
boost::get<std::vector<float>>(op_desc.GetAttr("weight_scale"));
BOOST_GET_CONST(std::vector<float>, op_desc.GetAttr("weight_scale"));
weight_data = engine_->GetWeightCPUData(op_desc.Input(w_name).front(),
Y_t, true, weight_scale);
engine_->SetTensorDynamicRange(X, in_scale);
......
......@@ -31,8 +31,8 @@ class HardSigmoidOpConverter : public OpConverter {
framework::OpDesc op_desc(op, nullptr);
// Declare inputs
auto* input = engine_->GetITensor(op_desc.Input("X")[0]);
float slope = boost::get<float>(op_desc.GetAttr("slope"));
float offset = boost::get<float>(op_desc.GetAttr("offset"));
float slope = BOOST_GET_CONST(float, op_desc.GetAttr("slope"));
float offset = BOOST_GET_CONST(float, op_desc.GetAttr("offset"));
auto* layer = TRT_ENGINE_ADD_LAYER(engine_, Activation, *input,
nvinfer1::ActivationType::kHARD_SIGMOID);
layer->setAlpha(slope);
......
......@@ -45,13 +45,13 @@ class HardSwishOpConverter : public OpConverter {
const float threshold =
op_desc.HasAttr("threshold")
? boost::get<float>(op_desc.GetAttr("threshold"))
? BOOST_GET_CONST(float, op_desc.GetAttr("threshold"))
: 6.0f;
const float scale = op_desc.HasAttr("scale")
? boost::get<float>(op_desc.GetAttr("scale"))
? BOOST_GET_CONST(float, op_desc.GetAttr("scale"))
: 6.0f;
const float offset = op_desc.HasAttr("offset")
? boost::get<float>(op_desc.GetAttr("offset"))
? BOOST_GET_CONST(float, op_desc.GetAttr("offset"))
: 3.0f;
nvinfer1::ILayer* layer = nullptr;
......
......@@ -28,7 +28,7 @@ class InstanceNormOpConverter : public OpConverter {
framework::OpDesc op_desc(op, nullptr);
auto* input = engine_->GetITensor(op_desc.Input("X")[0]);
float eps = boost::get<float>(op_desc.GetAttr("epsilon"));
float eps = BOOST_GET_CONST(float, op_desc.GetAttr("epsilon"));
auto* scale_var = scope.FindVar(op_desc.Input("Scale")[0]);
auto* bias_var = scope.FindVar(op_desc.Input("Bias")[0]);
......
......@@ -50,10 +50,10 @@ class LayerNormOpConverter : public OpConverter {
auto* Scale_v = scope.FindVar(op_desc.Input("Scale").front());
const int begin_norm_axis =
op_desc.HasAttr("begin_norm_axis")
? boost::get<int>(op_desc.GetAttr("begin_norm_axis"))
? BOOST_GET_CONST(int, op_desc.GetAttr("begin_norm_axis"))
: 1;
const float eps = op_desc.HasAttr("epsilon")
? boost::get<float>(op_desc.GetAttr("epsilon"))
? BOOST_GET_CONST(float, op_desc.GetAttr("epsilon"))
: 1e-5f;
PADDLE_ENFORCE_NOT_NULL(
Bias_v, platform::errors::InvalidArgument(
......
......@@ -42,7 +42,7 @@ class LeakyReluOpConverter : public OpConverter {
"outputs. Expected 1, but received %d",
output_num));
// Get attrs
float alpha = boost::get<float>(op_desc.GetAttr("alpha"));
float alpha = BOOST_GET_CONST(float, op_desc.GetAttr("alpha"));
nvinfer1::ILayer* output_layer = nullptr;
#if IS_TRT_VERSION_GE(5100)
......@@ -51,10 +51,10 @@ class LeakyReluOpConverter : public OpConverter {
layer->setAlpha(alpha);
output_layer = layer;
bool enable_int8 = boost::get<bool>(op_desc.HasAttr("enable_int8"));
bool enable_int8 = op_desc.HasAttr("enable_int8");
if (enable_int8) {
CHECK(op_desc.HasAttr("X_scale"));
float in_scale = boost::get<float>(op_desc.GetAttr("X_scale"));
float in_scale = BOOST_GET_CONST(float, op_desc.GetAttr("X_scale"));
engine_->SetTensorDynamicRange(input, in_scale);
}
#else
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册