| 70 | |
| 71 | template <typename Device> |
| 72 | Status BinaryAddTensors(OpKernelContext* ctx, const Tensor& a, const Tensor& b, |
| 73 | Tensor* out) { |
| 74 | if (a.dtype() == DT_INVALID) { |
| 75 | *out = b; |
| 76 | return Status::OK(); |
| 77 | } |
| 78 | if (b.dtype() == DT_INVALID) { |
| 79 | *out = a; |
| 80 | return Status::OK(); |
| 81 | } |
| 82 | if (a.dtype() != b.dtype()) { |
| 83 | return errors::InvalidArgument( |
| 84 | "Trying to add two tensors with incompatible element types. ", |
| 85 | "One is ", DataTypeString(a.dtype()), " and the other is ", |
| 86 | DataTypeString(b.dtype())); |
| 87 | } |
| 88 | if (a.shape() != b.shape()) { |
| 89 | // TODO(apassos) support broadcasting additions here? |
| 90 | return errors::InvalidArgument( |
| 91 | "Trying to add two tensors with incompatible element shapes. ", |
| 92 | "One is ", a.shape().DebugString(), " and the other is ", |
| 93 | b.shape().DebugString()); |
| 94 | } |
| 95 | |
| 96 | AllocatorAttributes attr; |
| 97 | if (a.dtype() == DT_VARIANT) { |
| 98 | attr.set_on_host(true); |
| 99 | } |
| 100 | TF_RETURN_IF_ERROR(ctx->allocate_temp(a.dtype(), a.shape(), out, attr)); |
| 101 | |
| 102 | switch (out->dtype()) { |
| 103 | #define DTYPE_CASE(dtype) \ |
| 104 | case DataTypeToEnum<dtype>::value: \ |
| 105 | out->flat<dtype>().device(ctx->eigen_device<Device>()) = \ |
| 106 | a.flat<dtype>() + b.flat<dtype>(); \ |
| 107 | break; |
| 108 | |
| 109 | TF_CALL_NUMBER_TYPES(DTYPE_CASE) |
| 110 | #undef DTYPE_CASE |
| 111 | |
| 112 | case DataTypeToEnum<Variant>::value: { |
| 113 | Variant* out_variant = out->scalar<Variant>().data(); |
| 114 | TF_RETURN_IF_ERROR(BinaryOpVariants<Device>( |
| 115 | ctx, ADD_VARIANT_BINARY_OP, a.scalar<Variant>()(), |
| 116 | b.scalar<Variant>()(), out_variant)); |
| 117 | break; |
| 118 | } |
| 119 | default: |
| 120 | return errors::InvalidArgument("Trying to add unsupported dtype ", |
| 121 | out->dtype()); |
| 122 | } |
| 123 | return Status::OK(); |
| 124 | } |
| 125 | |
| 126 | } // namespace tensorflow |
| 127 |
nothing calls this directly
no test coverage detected