Skip to content

Commit

Permalink
[Cherry-pick] Fix amp error cp (#46272)
Browse files Browse the repository at this point in the history
* [Eager] Fix ocr (#46124)

* fix linspace error in amp

* fix log

* fix amp error

* fix ocr error which caused by amp

* add more check

* rename dtype ns

* [Eager Bug fix]Fix Detection (#46147)

* fix linspace error in amp

* fix log

* fix amp error

* Revert "Simplify size op impl (#45808)"

This reverts commit c252b1d.

* fix_seg

* fix detection

Co-authored-by: Chen Weihang <sunny_cwh@163.com>

Co-authored-by: Chen Weihang <sunny_cwh@163.com>
  • Loading branch information
JiabinYang and chenwhql authored Sep 20, 2022
1 parent 759736d commit da173c4
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
13 changes: 13 additions & 0 deletions paddle/fluid/eager/auto_code_generator/eager_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,15 @@ static std::pair<std::string, std::string> GenerateForwardFunctionContents(
generated_function_body += amp_context;
generated_function_body += "\n";
}

if (!forward_inplace_map.empty()) {
generated_function_body +=
" auto current_level = egr::Controller::Instance().GetAMPLevel();\n";
generated_function_body +=
" "
"egr::Controller::Instance().SetAMPLevel(paddle::imperative::AmpLevel::"
"O0);\n";
}
// forward ins insert
const char* FWD_INS_MAP_TEMPLATE =
" std::map<std::string, "
Expand Down Expand Up @@ -1999,6 +2008,10 @@ static std::pair<std::string, std::string> GenerateForwardFunctionContents(
}
trace_op_body_str += out_tensor_str;
}
if (!forward_inplace_map.empty()) {
trace_op_body_str +=
" egr::Controller::Instance().SetAMPLevel(current_level);\n";
}
trace_op_body_str += "\n";
VLOG(6) << "Converted Output VarBase to EagerVariable(s)";
/* ------ END Generate TraceOp ----- */
Expand Down
13 changes: 13 additions & 0 deletions paddle/fluid/pybind/eager_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ typedef SSIZE_T ssize_t;
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#include "paddle/fluid/eager/amp_utils.h"
#include "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h"
#include "paddle/fluid/eager/eager_amp_auto_cast.h"
#include "paddle/fluid/framework/python_headers.h"
#include "paddle/fluid/memory/allocation/mmap_allocator.h"
#include "paddle/fluid/pybind/tensor_py.h"
Expand Down Expand Up @@ -1171,6 +1173,17 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self,
// Release gil and do tracing
py::gil_scoped_release release;
// use inplace set_value_ operator
if (value_tensor.initialized() &&
(self->tensor.dtype() != value_tensor.dtype())) {
paddle::small_vector<std::vector<paddle::experimental::Tensor>,
egr::kSlotSmallVectorSize>
tmps = {{self->tensor}, {value_tensor}};
auto amp_dtype = egr::GetAmpDestDtype("set_value", tmps);
self->tensor = egr::EagerAmpAutoCast(
self->tensor.name(), self->tensor, amp_dtype, "set_value");
value_tensor = egr::EagerAmpAutoCast(
value_tensor.name(), value_tensor, amp_dtype, "set_value");
}
self->tensor = set_value__dygraph_function(
self->tensor, value_tensor, {}, {}, {}, attrs);
}
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/cpu/scale_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ PD_REGISTER_KERNEL(scale,
float,
double,
phi::dtype::bfloat16,
phi::dtype::float16,
uint8_t,
int8_t,
int16_t,
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/kernels/cpu/set_value_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ PD_REGISTER_KERNEL(set_value_grad,
double,
int,
int64_t,
bool) {}
bool,
phi::dtype::float16) {}
6 changes: 4 additions & 2 deletions paddle/phi/kernels/cpu/set_value_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ PD_REGISTER_KERNEL(set_value,
double,
int,
int64_t,
bool) {}
bool,
phi::dtype::float16) {}
PD_REGISTER_KERNEL(set_value_with_tensor,
CPU,
ALL_LAYOUT,
Expand All @@ -35,4 +36,5 @@ PD_REGISTER_KERNEL(set_value_with_tensor,
double,
int,
int64_t,
bool) {}
bool,
phi::dtype::float16) {}
2 changes: 2 additions & 0 deletions paddle/phi/kernels/funcs/eigen/scale.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/common/complex.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/kernels/funcs/eigen/eigen_function.h"

namespace phi {
Expand Down Expand Up @@ -41,6 +42,7 @@ struct EigenScale<Eigen::DefaultDevice, T> {
template struct EigenScale<Eigen::DefaultDevice, float>;
template struct EigenScale<Eigen::DefaultDevice, double>;
template struct EigenScale<Eigen::DefaultDevice, dtype::bfloat16>;
template struct EigenScale<Eigen::DefaultDevice, dtype::float16>;
template struct EigenScale<Eigen::DefaultDevice, uint8_t>;
template struct EigenScale<Eigen::DefaultDevice, int8_t>;
template struct EigenScale<Eigen::DefaultDevice, int16_t>;
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/kernels/gpu/set_value_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ PD_REGISTER_KERNEL(set_value_grad,
double,
int,
int64_t,
bool) {}
bool,
phi::dtype::float16) {}
6 changes: 4 additions & 2 deletions paddle/phi/kernels/gpu/set_value_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ PD_REGISTER_KERNEL(set_value,
double,
int,
int64_t,
bool) {}
bool,
paddle::platform::float16) {}
PD_REGISTER_KERNEL(set_value_with_tensor,
GPU,
ALL_LAYOUT,
Expand All @@ -35,4 +36,5 @@ PD_REGISTER_KERNEL(set_value_with_tensor,
double,
int,
int64_t,
bool) {}
bool,
paddle::platform::float16) {}

0 comments on commit da173c4

Please sign in to comment.