| 102 | } |
| 103 | |
| 104 | Maybe<void> TensorProcessor::Apply() { |
| 105 | if (promote_inputs_to_common_dtype_) { |
| 106 | bool has_different_input_dtype = CheckHasDifferentInputDType(tensor_tuple_); |
| 107 | if (has_different_input_dtype) { |
| 108 | if (promote_dtype_.has_value()) { |
| 109 | common_dtype_ = CHECK_JUST(promote_dtype_); |
| 110 | } else { |
| 111 | common_dtype_ = ComputeCommonDType(tensor_tuple_); |
| 112 | } |
| 113 | if (promote_integer_inputs_to_float_ && common_dtype_->is_integer()) { |
| 114 | // Promotes common dtype to the default float scalar type, if needed. |
| 115 | // same to pytorch's computeTypes() in torch/csrc/jit/codegen/cuda/type_promotion.cpp |
| 116 | common_dtype_ = DType::Float(); |
| 117 | } |
| 118 | JUST(CastToSameType(tensor_tuple_, common_dtype_)); |
| 119 | } else { |
| 120 | if (tensor_tuple_.size() == 1 |
| 121 | && !((tensor_tuple_[0]->dtype()->is_floating_point()) |
| 122 | || tensor_tuple_[0]->dtype()->is_complex())) { |
| 123 | Symbol<DType> cast_dtype = (inputs_lowest_dtype_vec_[0] == DType::InvalidDataType()) |
| 124 | ? DType::Float() |
| 125 | : inputs_lowest_dtype_vec_[0]; |
| 126 | JUST(CastToSameType(tensor_tuple_, cast_dtype)); |
| 127 | } |
| 128 | } |
| 129 | } else { |
| 130 | for (int i = 0; i < tensor_tuple_.size(); ++i) { |
| 131 | // Cast all the inputs to it's attribute `lowest_dtype` if the input tensor dtype is lower |
| 132 | // than attribute `lowest_dtype`. |
| 133 | Symbol<DType> base_dtype = inputs_lowest_dtype_vec_.at(i); |
| 134 | if (base_dtype->data_type() |
| 135 | && DType::priority_order[base_dtype->data_type()] |
| 136 | > DType::priority_order[tensor_tuple_.at(i)->dtype()->data_type()]) { |
| 137 | tensor_tuple_[i] = |
| 138 | JUST(one::functional::Cast(tensor_tuple_[i], base_dtype, /*pin_memory=*/false)); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | return Maybe<void>::Ok(); |
| 143 | } |
| 144 | |
| 145 | static bool IsAllContiguous(const TensorTuple& tensors) { |
| 146 | for (const auto& t : tensors) { |
no test coverage detected