| 782 | TensorListFromTensor(OpKernelConstruction* c) : OpKernel(c) {} |
| 783 | |
| 784 | void Compute(OpKernelContext* c) override { |
| 785 | Tensor* output_tensor; |
| 786 | AllocatorAttributes attr; |
| 787 | attr.set_on_host(true); |
| 788 | OP_REQUIRES_OK(c, c->allocate_output(0, {}, &output_tensor, attr)); |
| 789 | PartialTensorShape element_shape; |
| 790 | OP_REQUIRES_OK(c, TensorShapeFromTensor(c->input(1), &element_shape)); |
| 791 | TensorList output_list; |
| 792 | const Tensor& t = c->input(0); |
| 793 | output_list.element_dtype = t.dtype(); |
| 794 | OP_REQUIRES(c, TensorShapeUtils::IsVectorOrHigher(t.shape()), |
| 795 | errors::InvalidArgument( |
| 796 | "Tensor must be at least a vector, but saw shape: ", |
| 797 | t.shape().DebugString())); |
| 798 | TensorShape output_shape(t.shape()); |
| 799 | output_shape.RemoveDim(0); |
| 800 | OP_REQUIRES(c, element_shape.IsCompatibleWith(output_shape), |
| 801 | errors::InvalidArgument( |
| 802 | "Specified a list with shape ", element_shape.DebugString(), |
| 803 | " from a tensor with shape ", output_shape.DebugString())); |
| 804 | output_list.element_shape = element_shape; |
| 805 | output_list.tensors().reserve(t.shape().dim_size(0)); |
| 806 | for (int i = 0; i < t.shape().dim_size(0); ++i) { |
| 807 | Tensor tmp = t.Slice(i, i + 1); |
| 808 | TensorShape tmp_shape = tmp.shape(); |
| 809 | tmp_shape.RemoveDim(0); |
| 810 | OP_REQUIRES(c, tmp.CopyFrom(tmp, tmp_shape), |
| 811 | errors::Unknown("Unexpected shape error.")); |
| 812 | // TODO(apassos) maybe not always align; but weird compiler bugs seem to |
| 813 | // prevent this. |
| 814 | Tensor aligned; |
| 815 | OP_REQUIRES_OK(c, c->allocate_temp(tmp.dtype(), tmp.shape(), &aligned)); |
| 816 | aligned.flat<T>().device(c->eigen_device<Device>()) = |
| 817 | tmp.unaligned_flat<T>(); |
| 818 | output_list.tensors().push_back(aligned); |
| 819 | } |
| 820 | output_tensor->scalar<Variant>()() = std::move(output_list); |
| 821 | } |
| 822 | }; |
| 823 | |
| 824 | // Scatters values in `value` into `list`. Assumes that `indices` are valid. |
nothing calls this directly
no test coverage detected