Add support for AssertOp in XLA

Since Assert is not supported in the IR, we still have to keep the dummy
implementation of Assert and limit it to compilation mode only.

PiperOrigin-RevId: 249108399
This commit is contained in:
Gaurav Jain 2019-05-20 12:57:15 -07:00 committed by TensorFlower Gardener
parent 17e070371e
commit 499823969a
7 changed files with 70 additions and 28 deletions
tensorflow
compiler
core/kernels
python/kernel_tests

View File

@ -200,6 +200,7 @@ cc_library(
"//tensorflow/core/kernels:host_constant_op",
"//tensorflow/core/kernels:identity_n_op",
"//tensorflow/core/kernels:identity_op",
"//tensorflow/core/kernels:logging_ops",
"//tensorflow/core/kernels:no_op",
"//tensorflow/core/kernels:queue_op",
"//tensorflow/core/kernels:resource_variable_ops",

View File

@ -32,6 +32,7 @@ limitations under the License.
#include "tensorflow/core/kernels/host_constant_op.h"
#include "tensorflow/core/kernels/identity_n_op.h"
#include "tensorflow/core/kernels/identity_op.h"
#include "tensorflow/core/kernels/logging_ops.h"
#include "tensorflow/core/kernels/no_op.h"
#include "tensorflow/core/kernels/queue_op.h"
#include "tensorflow/core/kernels/resource_variable_ops.h"
@ -81,6 +82,11 @@ class XlaAssignVariableOp : public OpKernel {
REGISTER_KERNEL_BUILDER(Name("_XlaRun").Device(DEVICE), KERNEL);
#define REGISTER_XLA_DEVICE_KERNELS(DEVICE, TYPES) \
REGISTER_KERNEL_BUILDER(Name("Assert") \
.Device(DEVICE) \
.HostMemory("condition") \
.HostMemory("data"), \
AssertOp); \
REGISTER_KERNEL_BUILDER(Name("_Send").Device(DEVICE), SendOp); \
REGISTER_KERNEL_BUILDER(Name("_Recv").Device(DEVICE), RecvOp); \
REGISTER_KERNEL_BUILDER( \

View File

@ -43,7 +43,7 @@ class AssertOp : public XlaOpKernel {
TF_DISALLOW_COPY_AND_ASSIGN(AssertOp);
};
REGISTER_XLA_OP(Name("Assert"), AssertOp);
REGISTER_XLA_OP(Name("Assert").CompilationOnly(), AssertOp);
} // anonymous namespace
} // namespace tensorflow

View File

@ -6090,6 +6090,7 @@ filegroup(
"in_topk_op.h",
"initializable_lookup_table.cc",
"logging_ops.cc",
"logging_ops.h",
"lookup_table_init_op.cc",
"lookup_table_op.cc",
"lookup_util.cc",

View File

@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/core/kernels/logging_ops.h"
#include <iostream>
#include "absl/strings/str_cat.h"
@ -48,35 +50,33 @@ Status AppendStringToFile(const std::string& fname, StringPiece data,
} // namespace
class AssertOp : public OpKernel {
public:
explicit AssertOp(OpKernelConstruction* ctx) : OpKernel(ctx) {
OP_REQUIRES_OK(ctx, ctx->GetAttr("summarize", &summarize_));
AssertOp::AssertOp(OpKernelConstruction* ctx) : OpKernel(ctx) {
OP_REQUIRES_OK(ctx, ctx->GetAttr("summarize", &summarize_));
}
void AssertOp::Compute(OpKernelContext* ctx) {
const Tensor& cond = ctx->input(0);
OP_REQUIRES(ctx, IsLegacyScalar(cond.shape()),
errors::InvalidArgument("In[0] should be a scalar: ",
cond.shape().DebugString()));
if (cond.scalar<bool>()()) {
return;
}
void Compute(OpKernelContext* ctx) override {
const Tensor& cond = ctx->input(0);
OP_REQUIRES(ctx, IsLegacyScalar(cond.shape()),
errors::InvalidArgument("In[0] should be a scalar: ",
cond.shape().DebugString()));
if (cond.scalar<bool>()()) {
return;
}
string msg = "assertion failed: ";
for (int i = 1; i < ctx->num_inputs(); ++i) {
strings::StrAppend(&msg, "[", ctx->input(i).SummarizeValue(summarize_),
"]");
if (i < ctx->num_inputs() - 1) strings::StrAppend(&msg, " ");
}
ctx->SetStatus(errors::InvalidArgument(msg));
string msg = "assertion failed: ";
for (int i = 1; i < ctx->num_inputs(); ++i) {
strings::StrAppend(&msg, "[", ctx->input(i).SummarizeValue(summarize_),
"]");
if (i < ctx->num_inputs() - 1) strings::StrAppend(&msg, " ");
}
ctx->SetStatus(errors::InvalidArgument(msg));
}
private:
int32 summarize_ = 0;
};
REGISTER_KERNEL_BUILDER(Name("Assert").Device(DEVICE_CPU), AssertOp);
REGISTER_KERNEL_BUILDER(Name("Assert")
.Device(DEVICE_CPU)
.HostMemory("condition")
.HostMemory("data"),
AssertOp);
#if GOOGLE_CUDA
REGISTER_KERNEL_BUILDER(Name("Assert")

View File

@ -0,0 +1,33 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_CORE_KERNELS_LOGGING_OPS_H_
#define TENSORFLOW_CORE_KERNELS_LOGGING_OPS_H_
#include "tensorflow/core/framework/op_kernel.h"
namespace tensorflow {
class AssertOp : public OpKernel {
public:
explicit AssertOp(OpKernelConstruction* c);
void Compute(OpKernelContext* ctx) override;
private:
int32 summarize_ = 0;
};
} // namespace tensorflow
#endif // TENSORFLOW_CORE_KERNELS_LOGGING_OPS_H_

View File

@ -600,7 +600,7 @@ tf_py_test(
],
)
tf_py_test(
cuda_py_test(
name = "logging_ops_test",
size = "small",
srcs = ["logging_ops_test.py"],
@ -613,6 +613,7 @@ tf_py_test(
"//tensorflow/python:logging_ops",
"//tensorflow/python:math_ops",
],
xla_enable_strict_auto_jit = True,
)
tf_py_test(