• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/compiler/xla/client/lib/testing.h"
17 
18 #include "absl/strings/str_cat.h"
19 #include "tensorflow/compiler/xla/client/xla_builder.h"
20 #include "tensorflow/compiler/xla/execution_options_util.h"
21 #include "tensorflow/compiler/xla/literal.h"
22 #include "tensorflow/compiler/xla/shape_util.h"
23 #include "tensorflow/compiler/xla/statusor.h"
24 #include "tensorflow/compiler/xla/tests/test_utils.h"
25 #include "tensorflow/compiler/xla/types.h"
26 #include "tensorflow/compiler/xla/util.h"
27 #include "tensorflow/core/platform/protobuf.h"
28 #include "tensorflow/core/platform/types.h"
29 
30 namespace xla {
31 namespace {
32 
33 // Calculates the number of bytes required to store the data within the
34 // specified shape. In case of a (nested) tuple shape this is the total byte
35 // size of all sub-shapes within the tuple.
DataSizeOfShape(const Shape & shape)36 int64 DataSizeOfShape(const Shape& shape) {
37   if (shape.IsArray()) {
38     return ShapeUtil::ByteSizeOf(shape);
39   }
40 
41   int64 total_size = 0;
42   for (const Shape& s : shape.tuple_shapes()) {
43     total_size += DataSizeOfShape(s);
44   }
45   return total_size;
46 }
47 
48 // Creates a XlaOp for an op what generates fake data with the given shape.
BuildFakeDataOpOnDevice(const Shape & shape,XlaBuilder * builder)49 XlaOp BuildFakeDataOpOnDevice(const Shape& shape, XlaBuilder* builder) {
50   if (shape.IsArray()) {
51     return Broadcast(
52         ConstantLiteral(builder, LiteralUtil::One(shape.element_type())),
53         AsInt64Slice(shape.dimensions()));
54   }
55   std::vector<XlaOp> parts;
56   for (const Shape& s : shape.tuple_shapes()) {
57     parts.push_back(BuildFakeDataOpOnDevice(s, builder));
58   }
59   return Tuple(builder, parts);
60 }
61 
MakeFakeDataViaDeviceOrDie(const Shape & shape,Client * client,DebugOptions * debug_opts)62 std::unique_ptr<GlobalData> MakeFakeDataViaDeviceOrDie(
63     const Shape& shape, Client* client, DebugOptions* debug_opts) {
64   XlaBuilder b(absl::StrCat("make_fake_", ShapeUtil::HumanString(shape)));
65   BuildFakeDataOpOnDevice(shape, &b);
66   XlaComputation computation = b.Build().ConsumeValueOrDie();
67 
68   auto execution_options = CreateDefaultExecutionOptions();
69   *execution_options.mutable_shape_with_output_layout() = shape.ToProto();
70   if (debug_opts) {
71     *execution_options.mutable_debug_options() = *debug_opts;
72   }
73   return client->Execute(computation, /*arguments=*/{}, &execution_options)
74       .ConsumeValueOrDie();
75 }
76 
77 }  // namespace
78 
MakeFakeDataOrDie(const Shape & shape,Client * client,DebugOptions * debug_opts)79 std::unique_ptr<GlobalData> MakeFakeDataOrDie(
80     const Shape& shape, Client* client, DebugOptions* debug_opts /*=nullptr*/) {
81   if (DataSizeOfShape(shape) < (1LL << 20)) {
82     StatusOr<Literal> literal_status = MakeFakeLiteral(shape);
83     if (!literal_status.ok()) {
84       // If we got an Unimplemented error, fall back to making the fake data via
85       // an on-device computation.
86       CHECK_EQ(literal_status.status().code(),
87                tensorflow::error::UNIMPLEMENTED);
88       return MakeFakeDataViaDeviceOrDie(shape, client, debug_opts);
89     }
90     return client->TransferToServer(literal_status.ValueOrDie()).ValueOrDie();
91   }
92 
93   // If the data is large, generate it on-device.
94   return MakeFakeDataViaDeviceOrDie(shape, client, debug_opts);
95 }
96 
MakeFakeArgumentsOrDie(const XlaComputation & computation,Client * client,DebugOptions * debug_opts)97 std::vector<std::unique_ptr<GlobalData>> MakeFakeArgumentsOrDie(
98     const XlaComputation& computation, Client* client,
99     DebugOptions* debug_opts /*=nullptr*/) {
100   CHECK(computation.proto().has_host_program_shape())
101       << "Computation should have program shape.";
102   auto program_shape = computation.proto().host_program_shape();
103 
104   std::vector<std::unique_ptr<GlobalData>> results;
105   for (const ShapeProto& shape : program_shape.parameters()) {
106     results.push_back(MakeFakeDataOrDie(Shape(shape), client, debug_opts));
107   }
108   return results;
109 }
110 
111 }  // namespace xla
112