• 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 
29 namespace xla {
30 namespace {
31 
32 // Calculates the number of bytes required to store the data within the
33 // specified shape. In case of a (nested) tuple shape this is the total byte
34 // size of all sub-shapes within the tuple.
DataSizeOfShape(const Shape & shape)35 int64_t DataSizeOfShape(const Shape& shape) {
36   if (shape.IsArray()) {
37     return ShapeUtil::ByteSizeOf(shape);
38   }
39 
40   int64_t total_size = 0;
41   for (const Shape& s : shape.tuple_shapes()) {
42     total_size += DataSizeOfShape(s);
43   }
44   return total_size;
45 }
46 
47 // Creates a XlaOp for an op what generates fake data with the given shape.
BuildFakeDataOpOnDevice(const Shape & shape,XlaBuilder * builder)48 XlaOp BuildFakeDataOpOnDevice(const Shape& shape, XlaBuilder* builder) {
49   if (shape.IsArray()) {
50     return Broadcast(
51         ConstantLiteral(builder, LiteralUtil::One(shape.element_type())),
52         shape.dimensions());
53   }
54   std::vector<XlaOp> parts;
55   const auto& tuple_shapes = shape.tuple_shapes();
56   parts.reserve(tuple_shapes.size());
57   for (const Shape& s : tuple_shapes) {
58     parts.push_back(BuildFakeDataOpOnDevice(s, builder));
59   }
60   return Tuple(builder, parts);
61 }
62 
MakeFakeDataViaDeviceOrDie(const Shape & shape,Client * client,DebugOptions * debug_opts)63 std::unique_ptr<GlobalData> MakeFakeDataViaDeviceOrDie(
64     const Shape& shape, Client* client, DebugOptions* debug_opts) {
65   XlaBuilder b(absl::StrCat("make_fake_", ShapeUtil::HumanString(shape)));
66   BuildFakeDataOpOnDevice(shape, &b);
67   XlaComputation computation = b.Build().value();
68 
69   auto execution_options = CreateDefaultExecutionOptions();
70   *execution_options.mutable_shape_with_output_layout() = shape.ToProto();
71   if (debug_opts) {
72     *execution_options.mutable_debug_options() = *debug_opts;
73   }
74   return client->Execute(computation, /*arguments=*/{}, &execution_options)
75       .value();
76 }
77 
78 }  // namespace
79 
MakeFakeDataOrDie(const Shape & shape,Client * client,DebugOptions * debug_opts)80 std::unique_ptr<GlobalData> MakeFakeDataOrDie(
81     const Shape& shape, Client* client, DebugOptions* debug_opts /*=nullptr*/) {
82   if (DataSizeOfShape(shape) < (1LL << 20)) {
83     StatusOr<Literal> literal_status = MakeFakeLiteral(shape);
84     if (!literal_status.ok()) {
85       // If we got an Unimplemented error, fall back to making the fake data via
86       // an on-device computation.
87       CHECK_EQ(literal_status.status().code(),
88                tensorflow::error::UNIMPLEMENTED);
89       return MakeFakeDataViaDeviceOrDie(shape, client, debug_opts);
90     }
91     return client->TransferToServer(literal_status.ValueOrDie()).ValueOrDie();
92   }
93 
94   // If the data is large, generate it on-device.
95   return MakeFakeDataViaDeviceOrDie(shape, client, debug_opts);
96 }
97 
MakeFakeArgumentsOrDie(const XlaComputation & computation,Client * client,DebugOptions * debug_opts)98 std::vector<std::unique_ptr<GlobalData>> MakeFakeArgumentsOrDie(
99     const XlaComputation& computation, Client* client,
100     DebugOptions* debug_opts /*=nullptr*/) {
101   CHECK(computation.proto().has_host_program_shape())
102       << "Computation should have program shape.";
103   auto program_shape = computation.proto().host_program_shape();
104 
105   std::vector<std::unique_ptr<GlobalData>> results;
106   for (const ShapeProto& shape : program_shape.parameters()) {
107     results.push_back(MakeFakeDataOrDie(Shape(shape), client, debug_opts));
108   }
109   return results;
110 }
111 
112 }  // namespace xla
113