for (auto index : LayoutUtil::MinorToMajor(shape())) {
layout->add_minor_to_major(inverse_permutation[index]);
}
- std::unique_ptr<Literal> new_literal = CreateFromShape(permuted_shape);
- DCHECK_GE(ShapeUtil::ByteSizeOf(new_literal->shape()),
+ auto new_literal = MakeUnique<Literal>(permuted_shape);
+ DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal->shape()),
ShapeUtil::ByteSizeOf(shape()));
std::memcpy(new_literal->untyped_data(), untyped_data(), size_bytes());
return new_literal;
// Creates a new Literal object with the shape specified as parameter.
// The content of the literal values is the default value of the primitive
// type of literal itself (0 for numeric types, and false for predicates).
+ //
+ // Note: It's an antipattern to use this method then immediately call
+ // Literal::Populate on the result (since that results in zero initialization,
+ // then reinitialization. Conside if a call to MakeUnique<Literal>(shape),
+ // followed by the call to Literal::Populate can be used instead.
static std::unique_ptr<Literal> CreateFromShape(const Shape& shape);
protected:
const std::function<T(tensorflow::gtl::ArraySlice<int64>)>& generator) {
using NativeT = typename primitive_util::PrimitiveTypeToNative<type>::type;
TF_RET_CHECK(shape.element_type() == type);
- std::unique_ptr<Literal> literal = Literal::CreateFromShape(shape);
+ auto literal = MakeUnique<Literal>(shape);
TF_RETURN_IF_ERROR(literal.get()->Populate<NativeT>(
[&](tensorflow::gtl::ArraySlice<int64> indexes) {
return generator(indexes);
Shape shape = ShapeUtil::MakeShapeWithLayout(
primitive_util::NativeToPrimitiveType<uint32>(), data.dimensions,
data.layout);
- auto literal = Literal::CreateFromShape(shape);
+ auto literal = MakeUnique<Literal>(shape);
auto generator = [&](ArraySlice<int64> indexes) -> uint32 {
// Offsets from linear index just to avoid R0 literals to be initialized
// with zero.
Shape shape = ShapeUtil::MakeShapeWithLayout(
primitive_util::NativeToPrimitiveType<uint32>(), data.dimensions,
data.layout);
- auto literal = Literal::CreateFromShape(shape);
+ auto literal = MakeUnique<Literal>(shape);
auto generator = [&](ArraySlice<int64> indexes) -> uint32 {
// Offsets from linear index just to avoid R0 literals to be initialized
// with zero.
<< HloOpcodeString(opcode);
}
- auto result = Literal::CreateFromShape(shape);
+ auto result = MakeUnique<Literal>(shape);
TF_RETURN_IF_ERROR(result->Populate<bool>([&](ArraySlice<int64> multi_index) {
return compare_op(lhs_literal.Get<OperandT>(multi_index),
rhs_literal.Get<OperandT>(multi_index));
<< HloOpcodeString(opcode);
}
- auto result = Literal::CreateFromShape(shape);
+ auto result = MakeUnique<Literal>(shape);
TF_RETURN_IF_ERROR(result->Populate<bool>([&](ArraySlice<int64> multi_index) {
return compare_op(lhs_literal.Get<complex64>(multi_index),
rhs_literal.Get<complex64>(multi_index));
auto* true_computation = conditional->true_computation();
auto* false_computation = conditional->false_computation();
- auto result = Literal::CreateFromShape(conditional->shape());
HloEvaluator embedded_evaluator;
+ std::unique_ptr<Literal> result;
if (pred.Get<bool>({})) {
result = embedded_evaluator
.Evaluate<const Literal*>(*true_computation,
#include <memory>
+#include "tensorflow/compiler/xla/ptr_util.h"
#include "tensorflow/compiler/xla/service/dfs_hlo_visitor_with_default.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
ShapeUtil::HumanString(operand->shape()).c_str());
}
- auto result = Literal::CreateFromShape(shape);
-
+ auto result = MakeUnique<Literal>(shape);
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
[&](tensorflow::gtl::ArraySlice<int64> multi_index) {
return unary_op(operand_literal.Get<NativeT>(multi_index));
}
Status HandleBroadcast(HloInstruction* broadcast) override {
- parent_->evaluated_[broadcast] =
- Literal::CreateFromShape(broadcast->shape());
- auto output = parent_->evaluated_[broadcast].get();
const Literal& operand_to_broadcast =
parent_->GetEvaluatedLiteralFor(broadcast->operand(0));
std::vector<int64> broadcast_indices(
operand_to_broadcast.shape().dimensions(i));
}
- return output->Populate<ReturnT>(
+ auto output = MakeUnique<Literal>(broadcast->shape());
+ TF_RETURN_IF_ERROR(output->Populate<ReturnT>(
[&](tensorflow::gtl::ArraySlice<int64> multi_index) {
for (int64 i = 0; i < broadcast->dimensions().size(); ++i) {
broadcast_indices[i] = multi_index[broadcast->dimensions(i)];
}
return operand_to_broadcast.Get<ReturnT>(broadcast_indices);
- });
+ }));
+ parent_->evaluated_[broadcast] = std::move(output);
+ return Status::OK();
}
template <
<< ShapeUtil::HumanString(inferred_return_shape);
const Literal& operand_literal = parent_->GetEvaluatedLiteralFor(operand);
- auto result = Literal::CreateFromShape(result_shape);
+ auto result = MakeUnique<Literal>(result_shape);
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
[&](tensorflow::gtl::ArraySlice<int64> out_index) {
return static_cast<ReturnT>(result_val);
};
- auto result = Literal::CreateFromShape(result_shape);
+ auto result = MakeUnique<Literal>(result_shape);
TF_RETURN_IF_ERROR(result->PopulateParallel<ReturnT>(func));
parent_->evaluated_[conv] = std::move(result);
const Literal& lhs_literal = parent_->GetEvaluatedLiteralFor(lhs);
const Literal& rhs_literal = parent_->GetEvaluatedLiteralFor(rhs);
- auto result = Literal::CreateFromShape(dot->shape());
-
CHECK_EQ(dnums.lhs_batch_dimensions_size(),
dnums.rhs_batch_dimensions_size());
DimensionVector lhs_index(lhs_rank);
DimensionVector rhs_index(rhs_rank);
+ auto result = MakeUnique<Literal>(dot->shape());
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
[&](tensorflow::gtl::ArraySlice<int64> result_index) {
ElementwiseT result_val = static_cast<ElementwiseT>(0);
// Create new HLO of padded shape with padding value.
ReturnT scalar =
parent_->GetEvaluatedLiteralFor(pad->operand(1)).Get<ReturnT>({});
- auto result = Literal::CreateFromShape(pad->shape());
+ auto result = MakeUnique<Literal>(pad->shape());
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
[&scalar](tensorflow::gtl::ArraySlice<int64> multi_index) {
return scalar;
auto operands = map->operands();
HloComputation* computation = map->to_apply();
- auto result = Literal::CreateFromShape(map->shape());
+ auto result = MakeUnique<Literal>(map->shape());
HloEvaluator embedded_evaluator(parent_->max_loop_iterations_);
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
TF_RET_CHECK(ShapeUtil::IsScalar(init_literal.shape()));
auto init_scalar = init_literal.Get<ReturnT>({});
- auto result = Literal::CreateFromShape(reduce->shape());
-
const auto arg_dimensions = AsInt64Slice(arg_literal.shape().dimensions());
std::vector<int64> arg_dim_steps(arg_dimensions.size());
std::vector<int64> arg_dim_counts(arg_dimensions.size());
}
HloEvaluator embedded_evaluator(parent_->max_loop_iterations_);
+ auto result = MakeUnique<Literal>(reduce->shape());
// For each resulting dimension, calculate and assign computed value.
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
[&](tensorflow::gtl::ArraySlice<int64> multi_index) {
TF_RET_CHECK(ShapeUtil::IsScalar(init_literal.shape()));
auto init_scalar = init_literal.Get<ReturnT>({});
- auto result = Literal::CreateFromShape(select_and_scatter->shape());
+ auto result = MakeUnique<Literal>(select_and_scatter->shape());
// Initialize result array with the init value.
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
TF_RET_CHECK(ShapeUtil::IsScalar(init_literal.shape()));
auto init_scalar = init_literal.Get<ReturnT>({});
- auto result = Literal::CreateFromShape(reduce_window->shape());
-
// Creates a Shape object from window, for iteration below.
std::vector<int64> window_dimension_sizes;
for (const auto& window_dimension : window.dimensions()) {
DimensionVector operand_index(ShapeUtil::Rank(operand_literal.shape()));
HloEvaluator embedded_evaluator(parent_->max_loop_iterations_);
+ auto result = MakeUnique<Literal>(reduce_window->shape());
// For each resulting dimension, calculate and assign computed value.
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
[&](tensorflow::gtl::ArraySlice<int64> output_index) {
std::vector<int64> operand_indices(start.size());
- auto result = Literal::CreateFromShape(result_shape);
+ auto result = MakeUnique<Literal>(result_shape);
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
[&](tensorflow::gtl::ArraySlice<int64> multi_index) {
for (int64 i = 0; i < operand_indices.size(); ++i) {
const Literal& lhs_literal = parent_->GetEvaluatedLiteralFor(lhs);
const Literal& rhs_literal = parent_->GetEvaluatedLiteralFor(rhs);
- auto result = Literal::CreateFromShape(shape);
+ auto result = MakeUnique<Literal>(shape);
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
[&](tensorflow::gtl::ArraySlice<int64> multi_index) {
const Literal& rhs_literal = parent_->GetEvaluatedLiteralFor(rhs);
const Literal& ehs_literal = parent_->GetEvaluatedLiteralFor(ehs);
- auto result = Literal::CreateFromShape(shape);
+ auto result = MakeUnique<Literal>(shape);
TF_RETURN_IF_ERROR(result->Populate<ReturnT>(
[&](tensorflow::gtl::ArraySlice<int64> multi_index) {
std::vector<int64> input_dims(6, 8);
auto shape = ShapeUtil::MakeShape(F32, input_dims);
- std::unique_ptr<Literal> arg_literal = Literal::CreateFromShape(shape);
- auto generator = [&](tensorflow::gtl::ArraySlice<int64> indexes) -> float {
- return 1.0f;
- };
- TF_EXPECT_OK(arg_literal->Populate<float>(generator));
-
+ auto arg_literal = MakeUnique<Literal>(shape);
+ arg_literal->PopulateWithValue(1.0f);
const auto input = CreateConstantFromLiteral(*arg_literal, &builder_);
Padding padding = Padding::kValid;
std::vector<int64> output_dims = {6, 8, 6, 6, 8, 8};
Shape result_shape =
ShapeUtil::MakeShapeWithLayout(F32, output_dims, output_layout);
- std::unique_ptr<Literal> expected = Literal::CreateFromShape(result_shape);
- auto out_generator =
- [&](tensorflow::gtl::ArraySlice<int64> indexes) -> float {
- return 27.0f;
- };
- TF_EXPECT_OK(expected->Populate<float>(out_generator));
-
+ auto expected = MakeUnique<Literal>(result_shape);
+ expected->PopulateWithValue(27.0f);
ComputeAndCompareLiteral(&builder_, *expected, {}, DefaultErrorSpec());
}
}
return Literal::MakeTupleOwned(std::move(elements));
}
- std::unique_ptr<Literal> literal = Literal::CreateFromShape(shape);
+ auto literal = MakeUnique<Literal>(shape);
switch (shape.element_type()) {
case BF16:
PopulateWithRandomFloatingPointData<bfloat16>(literal.get(), engine);
auto sum = Literal::CreateR2<complex64>({{{111, 222}, {331, 442}},
{{1011, 2022}, {3031, 4042}},
{{10011, 20022}, {30031, 40042}}});
- auto prod = Literal::CreateFromShape(sum->shape());
+ auto prod = MakeUnique<Literal>(sum->shape());
ASSERT_TRUE(prod->Populate<complex64>(
[&sum](tensorflow::gtl::ArraySlice<int64> indexes) {
return sum->Get<complex64>(indexes) *