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/jit/xla_device_ops.h"
17
18 #include <memory>
19
20 #include "tensorflow/compiler/jit/xla_device_context.h"
21 #include "tensorflow/compiler/jit/xla_tensor.h"
22
23 namespace tensorflow {
24
XlaDeviceDummyOp(OpKernelConstruction * ctx)25 XlaDeviceDummyOp::XlaDeviceDummyOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
26
Compute(OpKernelContext * ctx)27 void XlaDeviceDummyOp::Compute(OpKernelContext* ctx) {
28 LOG(FATAL) << "Attempted to execute Op " << name() << " type "
29 << type_string() << " on an XLA device. This should never happen.";
30 }
31
XlaAssignVariableOp(OpKernelConstruction * c)32 XlaAssignVariableOp::XlaAssignVariableOp(OpKernelConstruction* c)
33 : OpKernel(c) {
34 OP_REQUIRES_OK(c, c->GetAttr("dtype", &dtype_));
35 }
36
Compute(OpKernelContext * context)37 void XlaAssignVariableOp::Compute(OpKernelContext* context) {
38 OP_REQUIRES(context, dtype_ == context->input(1).dtype(),
39 errors::InvalidArgument(
40 "Variable and value dtypes don't match; respectively, ",
41 DataTypeString(dtype_), " and ",
42 DataTypeString(context->input(1).dtype())));
43 Var* variable = nullptr;
44 const Tensor& value = context->input(1);
45 // Note: every resource-variable-manipulating op assumes copy-on-write
46 // semantics, and creates a copy of the variable's Tensor if its refcount is
47 // bigger than 1 when we try to modify it. This means we never need to copy
48 // the original tensor for AssignVariableOp; even if there are other live
49 // users of it we know none can modify it so this is always safe (even in
50 // esoteric cases where the same tensor is used to initialize multiple
51 // variables or the tensor is a constant this is safe, as future writes will
52 // trigger copies).
53 OP_REQUIRES_OK(context, LookupOrCreateResource<Var>(
54 context, HandleFromInput(context, 0), &variable,
55 [this, &value](Var** ptr) {
56 *ptr = new Var(dtype_);
57 *(*ptr)->tensor() = value;
58 (*ptr)->is_initialized = true;
59 return Status::OK();
60 }));
61 core::ScopedUnref s(variable);
62 mutex_lock ml(*variable->mu());
63 OP_REQUIRES(context, variable->tensor()->dtype() == dtype_,
64 errors::InvalidArgument(
65 "Trying to assign variable with wrong dtype. Expected ",
66 DataTypeString(variable->tensor()->dtype()), " got ",
67 DataTypeString(dtype_)));
68 variable->is_initialized = true;
69 *variable->tensor() = value;
70 }
71
72 } // namespace tensorflow
73