|
| 1 | +/* Copyright 2021 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 | +#include "tensorflow/compiler/jit/kernels/xla_ops.h" |
| 16 | +#include "tensorflow/compiler/plugin/poplar/driver/poplar_executable.h" |
| 17 | +#include "tensorflow/compiler/plugin/poplar/driver/poplar_platform.h" |
| 18 | +#include "tensorflow/compiler/plugin/poplar/kernels/ipu_kernels_common.h" |
| 19 | +#include "tensorflow/compiler/tf2xla/xla_compiler.h" |
| 20 | +#include "tensorflow/compiler/xla/client/client_library.h" |
| 21 | +#include "tensorflow/compiler/xla/statusor.h" |
| 22 | +#include "tensorflow/core/framework/function.h" |
| 23 | +#include "tensorflow/core/framework/op_kernel.h" |
| 24 | +#include "tensorflow/core/lib/core/status.h" |
| 25 | + |
| 26 | +namespace tensorflow { |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +Status BuildCompilationCache(OpKernelContext* ctx, se::Platform* platform, |
| 31 | + XlaCompilationCache** out_cache) { |
| 32 | + xla::LocalClientOptions client_options; |
| 33 | + client_options.set_platform(platform); |
| 34 | + client_options.set_intra_op_parallelism_threads( |
| 35 | + ctx->device()->tensorflow_cpu_worker_threads()->num_threads); |
| 36 | + TF_ASSIGN_OR_RETURN( |
| 37 | + auto* client, xla::ClientLibrary::GetOrCreateLocalClient(client_options)); |
| 38 | + const XlaOpRegistry::DeviceRegistration* registration; |
| 39 | + if (!XlaOpRegistry::GetCompilationDevice("IPU", ®istration)) { |
| 40 | + return errors::InvalidArgument("No JIT device registered for IPU"); |
| 41 | + } |
| 42 | + |
| 43 | + *out_cache = new XlaCompilationCache( |
| 44 | + client, DeviceType(registration->compilation_device_name)); |
| 45 | + return Status::OK(); |
| 46 | +} |
| 47 | + |
| 48 | +xla::StatusOr<xla::LocalExecutable*> CompileExecutable( |
| 49 | + OpKernelContext* ctx, const NameAttrList& function, se::Platform* platform, |
| 50 | + absl::Span<const Tensor* const> inputs, |
| 51 | + absl::Span<const VariableInfo> variable_infos, |
| 52 | + absl::Span<const int> constants) { |
| 53 | + auto* resource_manager = ctx->resource_manager(); |
| 54 | + if (!resource_manager) { |
| 55 | + return errors::Internal("Resource manager not found"); |
| 56 | + } |
| 57 | + |
| 58 | + XlaCompilationCache* cache; |
| 59 | + TF_RETURN_IF_ERROR(resource_manager->LookupOrCreate<XlaCompilationCache>( |
| 60 | + resource_manager->default_container(), "ipu_application_compile_cache", |
| 61 | + &cache, [&](XlaCompilationCache** cache) { |
| 62 | + return BuildCompilationCache(ctx, platform, cache); |
| 63 | + })); |
| 64 | + core::ScopedUnref cache_ref(cache); |
| 65 | + |
| 66 | + const auto* function_library = ctx->function_library(); |
| 67 | + if (!function_library) { |
| 68 | + return errors::Internal("Function library not found"); |
| 69 | + } |
| 70 | + |
| 71 | + const auto* flib_def = function_library->GetFunctionLibraryDefinition(); |
| 72 | + const auto* func_def = CHECK_NOTNULL(flib_def)->Find(function.name()); |
| 73 | + if (!func_def) { |
| 74 | + return errors::Internal("Function not found: " + function.name()); |
| 75 | + } |
| 76 | + |
| 77 | + VLOG(1) << "Compiling function: " << DebugString(*func_def); |
| 78 | + |
| 79 | + XlaCompiler::Options options; |
| 80 | + options.client = cache->client(); |
| 81 | + options.device_type = cache->device_type(); |
| 82 | + options.flib_def = flib_def; |
| 83 | + options.graph_def_version = function_library->graph_def_version(); |
| 84 | + |
| 85 | + se::TfAllocatorAdapter tf_allocator_adapter(ctx->device()->GetAllocator({}), |
| 86 | + platform); |
| 87 | + options.device_allocator = &tf_allocator_adapter; |
| 88 | + |
| 89 | + XlaCompiler::CompileOptions compile_options; |
| 90 | + compile_options.is_entry_computation = true; |
| 91 | + compile_options.always_return_tuple = false; |
| 92 | + |
| 93 | + // IPU Specific - store the names of all inputs. |
| 94 | + std::vector<std::string> mangled_input_names(inputs.size()); |
| 95 | + for (int64 i = 0; i != inputs.size(); ++i) { |
| 96 | + mangled_input_names[i] = ctx->op_kernel().requested_input(i); |
| 97 | + } |
| 98 | + |
| 99 | + TF_ASSIGN_OR_RETURN( |
| 100 | + std::vector<XlaCompiler::Argument> arguments, |
| 101 | + XlaComputationLaunchContext::BuildXlaCompilerArguments( |
| 102 | + constants, inputs, variable_infos, mangled_input_names)); |
| 103 | + |
| 104 | + const XlaCompiler::CompilationResult* compilation_result; |
| 105 | + xla::LocalExecutable* executable; |
| 106 | + TF_RETURN_IF_ERROR(cache->Compile(options, function, arguments, |
| 107 | + compile_options, |
| 108 | + XlaCompilationCache::CompileMode::kStrict, |
| 109 | + &compilation_result, &executable)); |
| 110 | + return executable; |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace |
| 114 | + |
| 115 | +class IPUApplicationCompile : public OpKernel { |
| 116 | + public: |
| 117 | + explicit IPUApplicationCompile(OpKernelConstruction* ctx) : OpKernel(ctx) { |
| 118 | + OP_REQUIRES_OK(ctx, ctx->GetAttr("function", &function_)); |
| 119 | + OP_REQUIRES_OK(ctx, ctx->GetAttr("resource_indices", &resource_indices_)); |
| 120 | + OP_REQUIRES_OK(ctx, ctx->GetAttr("constant_indices", &constant_indices_)); |
| 121 | + OP_REQUIRES_OK( |
| 122 | + ctx, ctx->GetAttr("executable_output_path", &executable_output_path_)); |
| 123 | + } |
| 124 | + |
| 125 | + void Compute(OpKernelContext* ctx) { |
| 126 | + auto platform_or_status = |
| 127 | + se::MultiPlatformManager::PlatformWithName("Poplar"); |
| 128 | + OP_REQUIRES_OK(ctx, platform_or_status.status()); |
| 129 | + auto* platform = platform_or_status.ValueOrDie(); |
| 130 | + |
| 131 | + std::vector<const Tensor*> inputs = InputsFromContext(ctx); |
| 132 | + std::vector<VariableInfo> variable_infos; |
| 133 | + OP_REQUIRES_OK(ctx, GetVariableInfosFromInputs( |
| 134 | + ctx->resource_manager(), ctx->device(), inputs, |
| 135 | + resource_indices_, &variable_infos)); |
| 136 | + |
| 137 | + OP_REQUIRES_OK(ctx, LockVariables(absl::MakeSpan(variable_infos))); |
| 138 | + |
| 139 | + auto executable_or_status = CompileExecutable( |
| 140 | + ctx, function_, platform, inputs, variable_infos, constant_indices_); |
| 141 | + OP_REQUIRES_OK(ctx, executable_or_status.status()); |
| 142 | + |
| 143 | + auto* poplar_executable = |
| 144 | + dynamic_cast<xla::poplarplugin::PoplarExecutable*>( |
| 145 | + executable_or_status.ValueOrDie()->executable()); |
| 146 | + OP_REQUIRES(ctx, poplar_executable != nullptr, |
| 147 | + errors::Internal("Missing Poplar executable")); |
| 148 | + |
| 149 | + OP_REQUIRES_OK(ctx, poplar_executable->Serialize(executable_output_path_)); |
| 150 | + ctx->set_output(0, Tensor(executable_output_path_)); |
| 151 | + } |
| 152 | + |
| 153 | + private: |
| 154 | + NameAttrList function_; |
| 155 | + std::string executable_output_path_; |
| 156 | + std::vector<int> constant_indices_; |
| 157 | + std::vector<int> resource_indices_; |
| 158 | + |
| 159 | + TF_DISALLOW_COPY_AND_ASSIGN(IPUApplicationCompile); |
| 160 | +}; |
| 161 | + |
| 162 | +// We register the op both for CPU and IPU to make it easier to use, as we then |
| 163 | +// can handle any colocation requirements from variables etc. The function will |
| 164 | +// be compiled for IPU regardless of the device placement of the op itself. |
| 165 | +REGISTER_KERNEL_BUILDER(Name("IPUApplicationCompile").Device(DEVICE_CPU), |
| 166 | + IPUApplicationCompile); |
| 167 | +REGISTER_KERNEL_BUILDER(Name("IPUApplicationCompile").Device(DEVICE_XLA_IPU), |
| 168 | + IPUApplicationCompile); |
| 169 | + |
| 170 | +} // namespace tensorflow |
0 commit comments