Skip to content

Commit b2db981

Browse files
Sourabh Bajajtensorflower-gardener
authored andcommitted
Merge changes from github.
PiperOrigin-RevId: 177526301
1 parent 0438ac7 commit b2db981

171 files changed

Lines changed: 4446 additions & 578 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ Pods
2222
Podfile.lock
2323
*.pbxproj
2424
*.xcworkspacedata
25+
/tensorflow/contrib/lite/downloads/**
26+
/tensorflow/contrib/lite/gen/**
27+
/tensorflow/contrib/lite/examples/ios/simple/data/*.txt
28+
/tensorflow/contrib/lite/examples/ios/simple/data/*.tflite
29+
xcuserdata/**

tensorflow/compiler/tf2xla/xla_op_kernel.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ void XlaOpKernelContext::SetConstantOutput(int index, const Tensor& constant) {
346346
}
347347

348348
void XlaOpKernelContext::SetInvalidOutput(int index) {
349-
const TensorShape shape;
350349
Tensor* output = nullptr;
351-
OP_REQUIRES_OK(context_, context_->allocate_output(index, shape, &output));
350+
OP_REQUIRES_OK(context_,
351+
context_->allocate_output(index, TensorShape({}), &output));
352352
XlaExpression* expression = CastExpressionFromUninitializedTensor(output);
353353
xla::ComputationDataHandle handle;
354354
handle.set_handle(0);

tensorflow/compiler/xla/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ cc_library(
175175
":types",
176176
":xla_data_proto",
177177
"//tensorflow/core:lib",
178+
"//tensorflow/core:ptr_util",
178179
],
179180
)
180181

tensorflow/compiler/xla/ptr_util.h

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,63 +16,36 @@ limitations under the License.
1616
#ifndef TENSORFLOW_COMPILER_XLA_PTR_UTIL_H_
1717
#define TENSORFLOW_COMPILER_XLA_PTR_UTIL_H_
1818

19-
// Utility functions for pointers.
19+
// As this was moved to tensorflow/core/util, provide indirections here to
20+
// maintain current functionality of the library.
2021

2122
#include <stddef.h>
2223

2324
#include <memory>
2425
#include <type_traits>
2526
#include <utility>
2627

27-
namespace xla {
28-
29-
namespace internal {
30-
31-
// Trait to select overloads and return types for MakeUnique.
32-
template <typename T>
33-
struct MakeUniqueResult {
34-
using scalar = std::unique_ptr<T>;
35-
};
36-
template <typename T>
37-
struct MakeUniqueResult<T[]> {
38-
using array = std::unique_ptr<T[]>;
39-
};
40-
template <typename T, size_t N>
41-
struct MakeUniqueResult<T[N]> {
42-
using invalid = void;
43-
};
28+
#include "tensorflow/core/util/ptr_util.h"
4429

45-
} // namespace internal
30+
namespace xla {
4631

47-
// Transfers ownership of a raw pointer to a std::unique_ptr of deduced type.
48-
// Example:
49-
// X* NewX(int, int);
50-
// auto x = WrapUnique(NewX(1, 2)); // 'x' is std::unique_ptr<X>.
51-
//
52-
// WrapUnique is useful for capturing the output of a raw pointer factory.
53-
// However, prefer 'MakeUnique<T>(args...) over 'WrapUnique(new T(args...))'.
54-
// auto x = WrapUnique(new X(1, 2)); // works, but nonideal.
55-
// auto x = MakeUnique<X>(1, 2); // safer, standard, avoids raw 'new'.
56-
//
57-
// Note: Cannot wrap pointers to array of unknown bound (i.e. U(*)[]).
5832
template <typename T>
5933
std::unique_ptr<T> WrapUnique(T* ptr) {
60-
static_assert(!std::is_array<T>::value || std::extent<T>::value != 0,
61-
"types T[0] or T[] are unsupported");
62-
return std::unique_ptr<T>(ptr);
34+
return tensorflow::WrapUnique<T>(ptr);
6335
}
6436

6537
template <typename T, typename... Args>
66-
typename internal::MakeUniqueResult<T>::scalar MakeUnique(Args&&... args) {
67-
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
38+
typename tensorflow::helper::MakeUniqueResult<T>::scalar MakeUnique(
39+
Args&&... args) {
40+
return tensorflow::MakeUnique<T, Args...>(std::forward<Args>(args)...);
6841
}
6942

7043
// Overload for array of unknown bound.
7144
// The allocation of arrays needs to use the array form of new,
7245
// and cannot take element constructor arguments.
7346
template <typename T>
74-
typename internal::MakeUniqueResult<T>::array MakeUnique(size_t n) {
75-
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
47+
typename tensorflow::helper::MakeUniqueResult<T>::array MakeUnique(size_t n) {
48+
return tensorflow::MakeUnique<T>(n);
7649
}
7750

7851
} // namespace xla

tensorflow/compiler/xla/service/buffer_assignment_test.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class BufferAssignmentTest : public HloTestBase {
8585
std::unique_ptr<BufferAssignment> RunBufferAssignment(HloModule* module,
8686
int64 alignment = 1) {
8787
return BufferAssigner::Run(
88-
module, MakeUnique<DependencyHloOrdering>(module),
88+
module, xla::MakeUnique<DependencyHloOrdering>(module),
8989
backend().compiler()->BufferSizeBytesFunction(),
9090
[alignment](LogicalBuffer::Color) { return alignment; })
9191
.ConsumeValueOrDie();
@@ -94,7 +94,7 @@ class BufferAssignmentTest : public HloTestBase {
9494
std::unique_ptr<BufferAssignment> RunColoredBufferAssignment(
9595
HloModule* module, BufferLiveness::Colorer colorer, int64 alignment = 1) {
9696
return BufferAssigner::Run(
97-
module, MakeUnique<DependencyHloOrdering>(module),
97+
module, xla::MakeUnique<DependencyHloOrdering>(module),
9898
backend().compiler()->BufferSizeBytesFunction(),
9999
[alignment](LogicalBuffer::Color) { return alignment; }, false,
100100
std::move(colorer))
@@ -1451,7 +1451,7 @@ class WhileBufferAssignmentTest : public HloTestBase {
14511451
auto sequence =
14521452
CreateMemoryMinimizingSequence(*module, ByteSizeOf).ConsumeValueOrDie();
14531453
return BufferAssigner::Run(
1454-
module, MakeUnique<SequentialHloOrdering>(module, sequence),
1454+
module, xla::MakeUnique<SequentialHloOrdering>(module, sequence),
14551455
ByteSizeOf,
14561456
[alignment](LogicalBuffer::Color) { return alignment; })
14571457
.ConsumeValueOrDie();
@@ -1472,7 +1472,7 @@ static void RunCopyInsertion(HloModule* module) {
14721472
}
14731473

14741474
TEST_F(WhileBufferAssignmentTest, TwoForwardWhileLoops) {
1475-
auto module = MakeUnique<HloModule>(TestName());
1475+
auto module = xla::MakeUnique<HloModule>(TestName());
14761476
auto builder = HloComputation::Builder("entry");
14771477

14781478
auto input0 = builder.AddInstruction(
@@ -1529,7 +1529,7 @@ TEST_F(WhileBufferAssignmentTest, TwoForwardWhileLoops) {
15291529
}
15301530

15311531
TEST_F(WhileBufferAssignmentTest, OneForwardBackwardWhileLoopSet) {
1532-
auto module = MakeUnique<HloModule>(TestName());
1532+
auto module = xla::MakeUnique<HloModule>(TestName());
15331533
auto builder = HloComputation::Builder("entry");
15341534

15351535
auto input0 = builder.AddInstruction(
@@ -1574,7 +1574,7 @@ TEST_F(WhileBufferAssignmentTest, OneForwardBackwardWhileLoopSet) {
15741574
}
15751575

15761576
TEST_F(BufferAssignmentTest, TwoCalls) {
1577-
auto module = MakeUnique<HloModule>(TestName());
1577+
auto module = xla::MakeUnique<HloModule>(TestName());
15781578
Shape r0f32 = ShapeUtil::MakeShape(xla::F32, {});
15791579
HloComputation* sub_computation;
15801580
{
@@ -1639,7 +1639,7 @@ static bool IsPostOrderTraversal(
16391639
}
16401640

16411641
TEST_F(WhileBufferAssignmentTest, WhileLoopsInterferingResultRange) {
1642-
auto module = MakeUnique<HloModule>(TestName());
1642+
auto module = xla::MakeUnique<HloModule>(TestName());
16431643
auto builder = HloComputation::Builder(TestName());
16441644

16451645
auto zero = builder.AddInstruction(
@@ -1710,15 +1710,15 @@ TEST_F(WhileBufferAssignmentTest, WhileLoopsInterferingResultRange) {
17101710
auto assignment =
17111711
BufferAssigner::Run(
17121712
module.get(),
1713-
MakeUnique<SequentialHloOrdering>(module.get(), sequence), ByteSizeOf,
1714-
[](LogicalBuffer::Color) { return 1; })
1713+
xla::MakeUnique<SequentialHloOrdering>(module.get(), sequence),
1714+
ByteSizeOf, [](LogicalBuffer::Color) { return 1; })
17151715
.ConsumeValueOrDie();
17161716

17171717
EXPECT_TRUE(BuffersDistinct({while0}, {while1}, *assignment));
17181718
}
17191719

17201720
TEST_F(WhileBufferAssignmentTest, WhilesDontShareEntryParamIfLiveOut) {
1721-
auto module = MakeUnique<HloModule>(TestName());
1721+
auto module = xla::MakeUnique<HloModule>(TestName());
17221722
auto builder = HloComputation::Builder("entry");
17231723

17241724
auto input0 = builder.AddInstruction(

tensorflow/compiler/xla/service/buffer_liveness_test.cc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ TEST_F(BufferLivenessTest, ElementwiseChain) {
120120

121121
auto liveness =
122122
BufferLiveness::Run(module.get(),
123-
MakeUnique<DependencyHloOrdering>(module.get()))
123+
xla::MakeUnique<DependencyHloOrdering>(module.get()))
124124
.ConsumeValueOrDie();
125125

126126
EXPECT_FALSE(InstructionsMayInterfere(*liveness, param, negate));
@@ -167,10 +167,10 @@ TEST_F(BufferLivenessTest, MultipleEntryParameters_Sequential) {
167167

168168
SequentialHloOrdering::HloModuleSequence sequence;
169169
sequence.insert({entry, {param0, negate, param1, exp, add}});
170-
auto liveness = BufferLiveness::Run(
171-
module.get(),
172-
MakeUnique<SequentialHloOrdering>(module.get(), sequence))
173-
.ConsumeValueOrDie();
170+
auto liveness =
171+
BufferLiveness::Run(module.get(), xla::MakeUnique<SequentialHloOrdering>(
172+
module.get(), sequence))
173+
.ConsumeValueOrDie();
174174

175175
// Entry parameters interfere as if they are defined simultaneously at
176176
// the very beginning.
@@ -216,7 +216,7 @@ TEST_F(BufferLivenessTest, NonElementwiseOperand) {
216216

217217
auto liveness =
218218
BufferLiveness::Run(module.get(),
219-
MakeUnique<DependencyHloOrdering>(module.get()))
219+
xla::MakeUnique<DependencyHloOrdering>(module.get()))
220220
.ConsumeValueOrDie();
221221

222222
EXPECT_FALSE(InstructionsMayInterfere(*liveness, param, exp));
@@ -250,7 +250,7 @@ TEST_F(BufferLivenessTest, OverlappedBuffers) {
250250

251251
auto liveness =
252252
BufferLiveness::Run(module.get(),
253-
MakeUnique<DependencyHloOrdering>(module.get()))
253+
xla::MakeUnique<DependencyHloOrdering>(module.get()))
254254
.ConsumeValueOrDie();
255255

256256
EXPECT_TRUE(InstructionsMayInterfere(*liveness, param, negate));
@@ -294,7 +294,7 @@ TEST_F(BufferLivenessTest, OverlappedBuffersSequentialOrder) {
294294
std::vector<const HloInstruction*> order = {param, negate, exp, add};
295295
module_sequence.emplace(computation, order);
296296
auto liveness =
297-
BufferLiveness::Run(module.get(), MakeUnique<SequentialHloOrdering>(
297+
BufferLiveness::Run(module.get(), xla::MakeUnique<SequentialHloOrdering>(
298298
module.get(), module_sequence))
299299
.ConsumeValueOrDie();
300300

@@ -334,7 +334,7 @@ TEST_F(BufferLivenessTest, TupleLiveOut) {
334334

335335
auto liveness =
336336
BufferLiveness::Run(module.get(),
337-
MakeUnique<DependencyHloOrdering>(module.get()))
337+
xla::MakeUnique<DependencyHloOrdering>(module.get()))
338338
.ConsumeValueOrDie();
339339

340340
// All buffers should be live out except the param
@@ -370,7 +370,7 @@ TEST_F(BufferLivenessTest, EmbeddedComputation) {
370370

371371
auto liveness =
372372
BufferLiveness::Run(module.get(),
373-
MakeUnique<DependencyHloOrdering>(module.get()))
373+
xla::MakeUnique<DependencyHloOrdering>(module.get()))
374374
.ConsumeValueOrDie();
375375

376376
// Buffers in different computations should always interfere.
@@ -409,7 +409,7 @@ TEST_F(BufferLivenessTest, TupleConstantLiveOut) {
409409

410410
auto liveness =
411411
BufferLiveness::Run(module.get(),
412-
MakeUnique<DependencyHloOrdering>(module.get()))
412+
xla::MakeUnique<DependencyHloOrdering>(module.get()))
413413
.ConsumeValueOrDie();
414414

415415
// Only the element buffers of the tuple constant which are pointed to by
@@ -474,7 +474,7 @@ TEST_F(BufferLivenessTest, IndependentTupleElements) {
474474

475475
auto liveness =
476476
BufferLiveness::Run(module.get(),
477-
MakeUnique<DependencyHloOrdering>(module.get()))
477+
xla::MakeUnique<DependencyHloOrdering>(module.get()))
478478
.ConsumeValueOrDie();
479479

480480
// We compare tuple element pairs that are input/output to the computation:
@@ -536,7 +536,7 @@ TEST_F(BufferLivenessTest, DependentTupleElements) {
536536

537537
auto liveness =
538538
BufferLiveness::Run(module.get(),
539-
MakeUnique<DependencyHloOrdering>(module.get()))
539+
xla::MakeUnique<DependencyHloOrdering>(module.get()))
540540
.ConsumeValueOrDie();
541541

542542
// We compare tuple element pairs that are input/output to the computation:
@@ -624,8 +624,8 @@ class FusedDynamicUpdateSliceLivenessTest : public BufferLivenessTest {
624624

625625
// Run BufferLiveness on 'module'.
626626
auto liveness =
627-
BufferLiveness::Run(module.get(),
628-
MakeUnique<DependencyHloOrdering>(module.get()))
627+
BufferLiveness::Run(
628+
module.get(), xla::MakeUnique<DependencyHloOrdering>(module.get()))
629629
.ConsumeValueOrDie();
630630
// Return whether or not buffers interference is detected between
631631
// 'tuple_param0' and 'tuple_root' at shape index '{1}'.
@@ -736,8 +736,8 @@ class DynamicUpdateSliceLivenessTest : public BufferLivenessTest {
736736
module->AddEmbeddedComputation(builder.Build());
737737
// Run BufferLiveness on 'module'.
738738
auto liveness =
739-
BufferLiveness::Run(module.get(),
740-
MakeUnique<DependencyHloOrdering>(module.get()))
739+
BufferLiveness::Run(
740+
module.get(), xla::MakeUnique<DependencyHloOrdering>(module.get()))
741741
.ConsumeValueOrDie();
742742
// Return whether or not buffers interference is detected between
743743
// 'tuple_param0' and 'tuple_root' at shape index '{1}'.

tensorflow/compiler/xla/service/cpu/cpu_compiler.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,11 @@ StatusOr<std::unique_ptr<Executable>> CpuCompiler::RunBackend(
469469
&pre_optimization_ir_hook, &post_optimization_ir_hook));
470470

471471
// Compile must be thread-safe so create a new LLVM context for the module.
472-
auto llvm_context = MakeUnique<llvm::LLVMContext>();
472+
auto llvm_context = xla::MakeUnique<llvm::LLVMContext>();
473473
auto llvm_module =
474-
MakeUnique<llvm::Module>("__compute_module", *llvm_context);
474+
xla::MakeUnique<llvm::Module>("__compute_module", *llvm_context);
475475

476-
auto jit = MakeUnique<SimpleOrcJIT>(
476+
auto jit = xla::MakeUnique<SimpleOrcJIT>(
477477
CompilerTargetOptions(module->config()),
478478
CodeGenOptLevel(module->config()),
479479
options::OptimizeForSizeRequested(module->config()),
@@ -528,9 +528,9 @@ StatusOr<std::unique_ptr<Executable>> CpuCompiler::RunBackend(
528528
// uses data dependencies for determining order.
529529
TF_ASSIGN_OR_RETURN(
530530
std::unique_ptr<BufferAssignment> assignment,
531-
BufferAssigner::Run(module.get(),
532-
MakeUnique<DependencyHloOrdering>(module.get()),
533-
BufferSizeBytesFunction(), memory_alignment));
531+
BufferAssigner::Run(
532+
module.get(), xla::MakeUnique<DependencyHloOrdering>(module.get()),
533+
BufferSizeBytesFunction(), memory_alignment));
534534
// BufferAssignment::ToString() includes a header, so no need for us to
535535
// print one ourselves.
536536
XLA_VLOG_LINES(2, assignment->ToString());
@@ -557,7 +557,7 @@ StatusOr<std::unique_ptr<Executable>> CpuCompiler::RunBackend(
557557
const void* data = instruction->literal().InternalData();
558558
int64 size = CpuExecutable::ShapeSizeBytes(instruction->shape());
559559
auto iter = aligned_constants.emplace(
560-
instruction, MakeUnique<unsigned char[]>(size));
560+
instruction, xla::MakeUnique<unsigned char[]>(size));
561561
CHECK_EQ(iter.second, true);
562562
unsigned char* aligned_data = iter.first->second.get();
563563
memcpy(aligned_data, data, size);
@@ -642,10 +642,10 @@ StatusOr<std::unique_ptr<Executable>> CpuCompiler::RunBackend(
642642
// temporary buffers are required to run the computation.
643643
TF_ASSIGN_OR_RETURN(
644644
std::unique_ptr<BufferAssignment> assignment,
645-
BufferAssigner::Run(
646-
module.get(),
647-
MakeUnique<SequentialHloOrdering>(module.get(), module_sequence),
648-
BufferSizeBytesFunction(), memory_alignment));
645+
BufferAssigner::Run(module.get(),
646+
xla::MakeUnique<SequentialHloOrdering>(
647+
module.get(), module_sequence),
648+
BufferSizeBytesFunction(), memory_alignment));
649649
// BufferAssignment::ToString() includes a header, so no need for us to
650650
// print one ourselves.
651651
XLA_VLOG_LINES(2, assignment->ToString());
@@ -824,7 +824,8 @@ CpuCompiler::CompileAheadOfTime(std::vector<std::unique_ptr<HloModule>> modules,
824824
TF_ASSIGN_OR_RETURN(
825825
std::unique_ptr<BufferAssignment> assignment,
826826
BufferAssigner::Run(
827-
module, MakeUnique<SequentialHloOrdering>(module, module_sequence),
827+
module,
828+
xla::MakeUnique<SequentialHloOrdering>(module, module_sequence),
828829
BufferSizeBytesFunction(), memory_alignment));
829830
// BufferAssignment::ToString() includes a header, so no need for us to
830831
// print one ourselves.

0 commit comments

Comments
 (0)