| 394 | namespace { |
| 395 | |
| 396 | Status ReadVariableInputTensor(const Tensor& tensor, DataType type, |
| 397 | const XlaOpKernelContext* ctx, |
| 398 | TensorShape* shape, xla::XlaOp* value) { |
| 399 | const XlaExpression* expression = CastExpressionFromTensor(tensor); |
| 400 | XlaResource* variable = expression->resource(); |
| 401 | TF_RET_CHECK(variable != nullptr); |
| 402 | TF_RET_CHECK(variable->kind() == XlaResource::kVariable); |
| 403 | if (!variable->initialized()) { |
| 404 | return errors::FailedPrecondition("Read of uninitialized variable ", |
| 405 | variable->name()); |
| 406 | } |
| 407 | if (variable->type() != type) { |
| 408 | return errors::InvalidArgument( |
| 409 | "Type mismatch for read of variable ", variable->name(), ". Expected ", |
| 410 | DataTypeString(type), "; got ", DataTypeString(variable->type())); |
| 411 | } |
| 412 | if (shape) { |
| 413 | *shape = variable->shape(); |
| 414 | } |
| 415 | |
| 416 | TF_ASSIGN_OR_RETURN(xla::Shape representation_shape, |
| 417 | ctx->compiler()->options().shape_representation_fn( |
| 418 | variable->shape(), variable->type(), |
| 419 | /*use_fast_memory=*/false)); |
| 420 | xla::Shape xla_shape; |
| 421 | TF_RETURN_IF_ERROR( |
| 422 | TensorShapeToXLAShape(variable->type(), variable->shape(), &xla_shape)); |
| 423 | if (xla::ShapeUtil::Compatible(xla_shape, representation_shape)) { |
| 424 | *value = variable->value(); |
| 425 | } else { |
| 426 | *value = xla::Reshape(variable->value(), variable->shape().dim_sizes()); |
| 427 | } |
| 428 | return Status::OK(); |
| 429 | } |
| 430 | |
| 431 | } // namespace |
| 432 |
no test coverage detected