/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #include "tensorflow/compiler/xla/literal_util.h" #include #include #include #include #include #include #include #include #include #include "absl/strings/str_cat.h" #include "absl/strings/str_join.h" #include "tensorflow/compiler/xla/index_util.h" #include "tensorflow/compiler/xla/shape_util.h" #include "tensorflow/compiler/xla/status_macros.h" #include "tensorflow/compiler/xla/types.h" #include "tensorflow/compiler/xla/util.h" #include "tensorflow/core/lib/core/errors.h" #include "tensorflow/core/platform/logging.h" namespace xla { namespace { using absl::StrCat; // Return a literal with all arrays of type FromNativeT converted to type // ToNativeT in the given literal. template Literal ConvertType(LiteralSlice literal) { // First construct shape of the result. Shape result_shape(literal.shape()); ShapeUtil::ForEachMutableSubshape( &result_shape, [](Shape* subshape, const ShapeIndex&) { if (subshape->element_type() == primitive_util::NativeToPrimitiveType()) { subshape->set_element_type( primitive_util::NativeToPrimitiveType()); } }); Literal result(result_shape); // Then copy over the data from 'literal' converting FromNativeT values to // ToNativeT values as necessary. ShapeUtil::ForEachSubshape( literal.shape(), [&](const Shape& subshape, const ShapeIndex& shape_index) { if (subshape.IsArray()) { if (subshape.element_type() == primitive_util::NativeToPrimitiveType()) { auto src = literal.data(shape_index); auto dest = result.data(shape_index); for (int64_t i = 0, end = src.size(); i < end; ++i) { dest[i] = static_cast(src[i]); } } else { TF_CHECK_OK(result.CopyFrom(literal, /*dest_shape_index=*/shape_index, /*src_shape_index=*/shape_index)); } } }); return result; } template using NativeT = typename primitive_util::PrimitiveTypeToNative::type; template Literal CreateScalarImpl(F&& value_provider, Args... args) { return LiteralUtil::CreateR0>( value_provider(std::forward(args)...)); } template