| 86 | |
| 87 | template <typename Context> |
| 88 | phi::DenseTensor CastDataType(const Context& dev_ctx, |
| 89 | const phi::DenseTensor& tensor, |
| 90 | DataType dtype) { |
| 91 | switch (tensor.dtype()) { |
| 92 | case DataType::FLOAT32: |
| 93 | return phi::Cast<float>(dev_ctx, tensor, dtype); |
| 94 | case DataType::FLOAT64: |
| 95 | return phi::Cast<double>(dev_ctx, tensor, dtype); |
| 96 | case DataType::INT32: |
| 97 | return phi::Cast<int32_t>(dev_ctx, tensor, dtype); |
| 98 | case DataType::INT64: |
| 99 | return phi::Cast<int64_t>(dev_ctx, tensor, dtype); |
| 100 | case DataType::FLOAT16: |
| 101 | return phi::Cast<phi::dtype::float16>(dev_ctx, tensor, dtype); |
| 102 | case DataType::BFLOAT16: |
| 103 | return phi::Cast<phi::dtype::bfloat16>(dev_ctx, tensor, dtype); |
| 104 | case DataType::BOOL: |
| 105 | return phi::Cast<bool>(dev_ctx, tensor, dtype); |
| 106 | case DataType::INT16: |
| 107 | return phi::Cast<int16_t>(dev_ctx, tensor, dtype); |
| 108 | case DataType::UINT8: |
| 109 | return phi::Cast<uint8_t>(dev_ctx, tensor, dtype); |
| 110 | default: |
| 111 | PADDLE_THROW(common::errors::Unimplemented( |
| 112 | "Data type (%s) is not supported when casting data type.", |
| 113 | tensor.dtype())); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) |
| 118 | phi::DenseTensor CastDataType(const phi::GPUContext& dev_ctx, |