2929
3030namespace paddle {
3131namespace imperative {
32- static inline phi:: DataType GetDataType (const pir::Value& value) {
32+ static inline DataType GetDataType (const pir::Value& value) {
3333 return paddle::dialect::GetValueDataType (value);
3434}
3535
36- static inline phi:: DataType GetDataType (const paddle::Tensor& tensor) {
36+ static inline DataType GetDataType (const paddle::Tensor& tensor) {
3737 return tensor.dtype ();
3838}
3939
4040template <class T >
41- static inline phi:: DataType GetPromoteType (
41+ static inline DataType GetPromoteType (
4242 const std::string& op_name,
4343 const paddle::small_vector<std::vector<T>, egr::kSlotSmallVectorSize >&
4444 amp_tensors_vector,
45- const phi:: DataType& amp_dtype) {
45+ const DataType& amp_dtype) {
4646 auto dst_type = amp_dtype;
4747 // only consider the dtype of input(X).
4848 if (op_name == " batch_norm" || op_name == " layer_norm" ||
4949 op_name == " sync_batch_norm" ||
5050 op_name == " moving_average_abs_max_scale" ) {
51- if (GetDataType (amp_tensors_vector[0 ][0 ]) == phi:: DataType::FLOAT32 ) {
52- dst_type = phi:: DataType::FLOAT32 ;
51+ if (GetDataType (amp_tensors_vector[0 ][0 ]) == DataType::FLOAT32 ) {
52+ dst_type = DataType::FLOAT32 ;
5353 }
5454 return dst_type;
5555 }
@@ -59,17 +59,17 @@ static inline phi::DataType GetPromoteType(
5959 if (op_name == " fused_attention" ) {
6060 for (size_t i = 0 ; i < amp_tensors_vector.size (); i++) {
6161 if (i < 3 || (i > 4 && i < 9 ) || i > 10 ) {
62- if (GetDataType (amp_tensors_vector[i][0 ]) == phi:: DataType::FLOAT32 ) {
63- dst_type = phi:: DataType::FLOAT32 ;
62+ if (GetDataType (amp_tensors_vector[i][0 ]) == DataType::FLOAT32 ) {
63+ dst_type = DataType::FLOAT32 ;
6464 return dst_type;
6565 }
6666 }
6767 }
6868 } else if (op_name == " fused_feedforward" ) {
6969 for (size_t i = 0 ; i < amp_tensors_vector.size (); i++) {
7070 if (i < 7 || i > 10 ) {
71- if (GetDataType (amp_tensors_vector[i][0 ]) == phi:: DataType::FLOAT32 ) {
72- dst_type = phi:: DataType::FLOAT32 ;
71+ if (GetDataType (amp_tensors_vector[i][0 ]) == DataType::FLOAT32 ) {
72+ dst_type = DataType::FLOAT32 ;
7373 return dst_type;
7474 }
7575 }
@@ -79,7 +79,7 @@ static inline phi::DataType GetPromoteType(
7979
8080 for (const auto & tensors : amp_tensors_vector) {
8181 for (const auto & tensor : tensors) {
82- if (GetDataType (tensor) == phi:: DataType::FLOAT32 ) {
82+ if (GetDataType (tensor) == DataType::FLOAT32 ) {
8383 dst_type = GetDataType (tensor);
8484 break ;
8585 }
@@ -89,12 +89,12 @@ static inline phi::DataType GetPromoteType(
8989 return dst_type;
9090}
9191
92- static inline phi:: DataType GetDtypeWithPlace (
92+ static inline DataType GetDtypeWithPlace (
9393 const std::string& op_name,
9494 const paddle::small_vector<std::vector<paddle::Tensor>,
9595 egr::kSlotSmallVectorSize >& amp_tensors_vector,
96- const phi:: DataType amp_dtype) {
97- if (amp_dtype == phi:: DataType::FLOAT32 ) {
96+ const DataType amp_dtype) {
97+ if (amp_dtype == DataType::FLOAT32 ) {
9898 return amp_dtype;
9999 }
100100 bool is_right_place = false ;
@@ -119,22 +119,22 @@ static inline phi::DataType GetDtypeWithPlace(
119119 if (!is_right_place) {
120120 VLOG (6 ) << " Change " << op_name << " 's AMP type from " << amp_dtype
121121 << " to FP32" ;
122- return phi:: DataType::FLOAT32 ;
122+ return DataType::FLOAT32 ;
123123 }
124124 return amp_dtype;
125125}
126126
127- static inline phi:: DataType GetDtypeWithPlace (
127+ static inline DataType GetDtypeWithPlace (
128128 const std::string& op_name UNUSED ,
129129 const paddle::small_vector<std::vector<pir::Value>,
130130 egr::kSlotSmallVectorSize >& amp_tensors_vector
131131 UNUSED ,
132- const phi:: DataType amp_dtype) {
132+ const DataType amp_dtype) {
133133 return amp_dtype;
134134}
135135
136136template <class T >
137- inline phi:: DataType GetAmpDestDtype (
137+ inline DataType GetAmpDestDtype (
138138 const std::string& op_name,
139139 const paddle::small_vector<std::vector<T>, egr::kSlotSmallVectorSize >&
140140 amp_tensors_vector) {
@@ -157,10 +157,10 @@ inline phi::DataType GetAmpDestDtype(
157157 } else if (paddle::imperative::AmpOperators::Instance ()
158158 .GetMutableBlockOps ()
159159 ->count (op_name)) {
160- dst_type = phi:: DataType::FLOAT32 ;
160+ dst_type = DataType::FLOAT32 ;
161161 } else {
162162 if (amp_level == paddle::imperative::AmpLevel::OD ) {
163- dst_type = phi:: DataType::FLOAT32 ;
163+ dst_type = DataType::FLOAT32 ;
164164 } else {
165165 dst_type =
166166 GetPromoteType (op_name, amp_tensors_vector, amp_setting_dtype);
@@ -171,15 +171,15 @@ inline phi::DataType GetAmpDestDtype(
171171 if (paddle::imperative::AmpOperators::Instance ()
172172 .GetMutableBlockOps ()
173173 ->count (op_name)) {
174- dst_type = phi:: DataType::FLOAT32 ;
174+ dst_type = DataType::FLOAT32 ;
175175 }
176176 }
177177
178178 if (dst_type == amp_setting_dtype &&
179179 (paddle::imperative::AmpOperators::Instance ()
180180 .GetMutableUnsupportedOps (amp_setting_dtype)
181181 ->count (op_name))) {
182- dst_type = phi:: DataType::FLOAT32 ;
182+ dst_type = DataType::FLOAT32 ;
183183 }
184184
185185 dst_type = GetDtypeWithPlace (op_name, amp_tensors_vector, dst_type);
@@ -190,7 +190,7 @@ inline phi::DataType GetAmpDestDtype(
190190}
191191
192192static inline bool NeedCast (const paddle::Tensor& tensor,
193- const phi:: DataType& dst_dtype) {
193+ const DataType& dst_dtype) {
194194 auto place = tensor.place ();
195195 auto data_type = tensor.dtype ();
196196 // Except CPU judgment, other conditions should be consistent with
@@ -201,9 +201,8 @@ static inline bool NeedCast(const paddle::Tensor& tensor,
201201 // CudaPinnedPlace is added for varbase created by dataloader
202202 // Cpu place is for different place tensor, when input1 is cpu and input2
203203 // is gpu
204- if ((data_type == phi::DataType::FLOAT32 ||
205- data_type == phi::DataType::FLOAT16 ||
206- data_type == phi::DataType::BFLOAT16 ) &&
204+ if ((data_type == DataType::FLOAT32 || data_type == DataType::FLOAT16 ||
205+ data_type == DataType::BFLOAT16 ) &&
207206 (data_type != dst_dtype)) {
208207 return true ;
209208 }
@@ -212,11 +211,10 @@ static inline bool NeedCast(const paddle::Tensor& tensor,
212211}
213212
214213static inline bool NeedCast (const pir::Value& value,
215- const phi:: DataType& dst_dtype) {
214+ const DataType& dst_dtype) {
216215 auto data_type = paddle::dialect::GetValueDataType (value);
217- if ((data_type == phi::DataType::FLOAT32 ||
218- data_type == phi::DataType::FLOAT16 ||
219- data_type == phi::DataType::BFLOAT16 ) &&
216+ if ((data_type == DataType::FLOAT32 || data_type == DataType::FLOAT16 ||
217+ data_type == DataType::BFLOAT16 ) &&
220218 (data_type != dst_dtype)) {
221219 return true ;
222220 }
@@ -225,14 +223,14 @@ static inline bool NeedCast(const pir::Value& value,
225223
226224#if !(defined(PADDLE_NO_PYTHON) && defined(PADDLE_ON_INFERENCE))
227225static inline paddle::Tensor Cast (const paddle::Tensor& input,
228- const phi:: DataType& dst_dtype,
226+ const DataType& dst_dtype,
229227 const bool trace_backward = true ) {
230228 if (input.is_sparse_coo_tensor () || input.is_sparse_csr_tensor ()) {
231229 if (trace_backward) {
232- return sparse::cast_ad_func (input, phi:: DataType::UNDEFINED , dst_dtype);
230+ return sparse::cast_ad_func (input, DataType::UNDEFINED , dst_dtype);
233231 } else {
234232 return paddle::experimental::sparse::cast (
235- input, phi:: DataType::UNDEFINED , dst_dtype);
233+ input, DataType::UNDEFINED , dst_dtype);
236234 }
237235 } else {
238236 if (trace_backward) {
@@ -245,7 +243,7 @@ static inline paddle::Tensor Cast(const paddle::Tensor& input,
245243#endif
246244
247245static inline pir::Value Cast (const pir::Value& input,
248- const phi:: DataType& dst_dtype,
246+ const DataType& dst_dtype,
249247 const bool trace_backward UNUSED = true ) {
250248 paddle::imperative::AutoCastGuard guard (
251249 egr::Controller::Instance ().GetCurrentAmpAttrs (),
@@ -256,12 +254,12 @@ static inline pir::Value Cast(const pir::Value& input,
256254template <class T >
257255inline std::vector<T> AmpAutoCasts (const std::string& inputs_name,
258256 const std::vector<T>& inputs,
259- const phi:: DataType& dst_dtype,
257+ const DataType& dst_dtype,
260258 std::string op_name UNUSED ,
261259 bool trace_backward UNUSED = true ) {
262260 VLOG (6 ) << " AMP AmpAutoCasts:"
263261 << " inputs(" << inputs_name << " ) dst_dtype("
264- << phi:: DataTypeToString (dst_dtype) << " )." ;
262+ << DataTypeToString (dst_dtype) << " )." ;
265263 std::vector<T> inputs_casted;
266264 for (auto & input : inputs) {
267265 if (NeedCast (input, dst_dtype)) {
@@ -276,13 +274,13 @@ inline std::vector<T> AmpAutoCasts(const std::string& inputs_name,
276274template <class T >
277275inline T AmpAutoCast (const std::string& input_name,
278276 const T& input,
279- const phi:: DataType& dst_dtype,
277+ const DataType& dst_dtype,
280278 const std::string& op_name,
281279 bool trace_backward = true ) {
282280 VLOG (6 ) << " AMP AmpAutoCasts: op_name(" << op_name << " )input(" << input_name
283- << " ) dst_dtype(" << phi:: DataTypeToString (dst_dtype) << " )." ;
281+ << " ) dst_dtype(" << DataTypeToString (dst_dtype) << " )." ;
284282
285- if (dst_dtype == phi:: DataType::FLOAT16 ) {
283+ if (dst_dtype == DataType::FLOAT16 ) {
286284 if (op_name == " run_program" ) {
287285 return input;
288286 }
@@ -304,7 +302,7 @@ inline T AmpAutoCast(const std::string& input_name,
304302 input_name != " x" ) {
305303 return input;
306304 }
307- } else if (dst_dtype == phi:: DataType::BFLOAT16 ) {
305+ } else if (dst_dtype == DataType::BFLOAT16 ) {
308306 if ((op_name == " batch_norm" || op_name == " layer_norm" ||
309307 op_name == " sync_batch_norm" || op_name == " weight_only_linear" ) &&
310308 input_name != " x" ) {
@@ -321,7 +319,7 @@ inline T AmpAutoCast(const std::string& input_name,
321319template <class T >
322320inline paddle::optional<T> AmpAutoCast (const std::string& input_name,
323321 const paddle::optional<T>& input,
324- const phi:: DataType& dst_dtype,
322+ const DataType& dst_dtype,
325323 const std::string& op_name,
326324 bool trace_backward = true ) {
327325 if (input) {
@@ -334,7 +332,7 @@ template <class T>
334332inline paddle::optional<std::vector<T>> AmpAutoCasts (
335333 const std::string& inputs_name,
336334 const paddle::optional<std::vector<T>>& inputs,
337- const phi:: DataType& dst_dtype,
335+ const DataType& dst_dtype,
338336 std::string op_name,
339337 bool trace_backward = true ) {
340338 if (inputs) {
0 commit comments