| 162 | } |
| 163 | |
| 164 | void StackOp::Compute(OpKernelContext* ctx) { |
| 165 | int32 size = std::numeric_limits<int32>::max(); |
| 166 | if (ctx->num_inputs() > 0) { |
| 167 | const Tensor* tensor_size; |
| 168 | OP_REQUIRES_OK(ctx, ctx->input("max_size", &tensor_size)); |
| 169 | |
| 170 | OP_REQUIRES( |
| 171 | ctx, TensorShapeUtils::IsScalar(tensor_size->shape()), |
| 172 | errors::InvalidArgument("Stack size must be a scalar, but had shape: ", |
| 173 | tensor_size->shape().DebugString())); |
| 174 | |
| 175 | int32 size_value = tensor_size->scalar<int32>()(); |
| 176 | if (size_value >= 0) { |
| 177 | size = size_value; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static const char kContainer[] = "_stacks"; |
| 182 | auto stack_id = Stack::stack_counter.fetch_add(1); |
| 183 | string stack_name = strings::StrCat(stack_name_, "_", stack_id); |
| 184 | // Store the handle in a per-step container. |
| 185 | ResourceMgr* rm = ctx->resource_manager(); |
| 186 | OP_REQUIRES(ctx, rm != nullptr, errors::Internal("No resource manager.")); |
| 187 | string key = strings::StrCat(kContainer, stack_name); |
| 188 | auto* step_container = ctx->step_container(); |
| 189 | OP_REQUIRES(ctx, step_container != nullptr, |
| 190 | errors::Internal("No step container.")); |
| 191 | Stack* stack = new Stack(elem_type_, stack_name, size); |
| 192 | OP_REQUIRES_OK(ctx, step_container->Create(rm, key, stack)); |
| 193 | if (IsRefType(ctx->expected_output_dtype(0))) { |
| 194 | // Create the stack handle. |
| 195 | AllocatorAttributes alloc_attr; |
| 196 | alloc_attr.set_on_host(true); |
| 197 | OP_REQUIRES_OK(ctx, ctx->allocate_temp(tensorflow::DT_STRING, |
| 198 | tensorflow::TensorShape({2}), |
| 199 | &stack->handle_, alloc_attr)); |
| 200 | auto handle = stack->handle_.flat<tstring>(); |
| 201 | handle(0) = kContainer; |
| 202 | handle(1) = std::move(stack_name); |
| 203 | ctx->set_output_ref(0, stack->mu(), &stack->handle_); |
| 204 | } else { |
| 205 | Tensor* handle; |
| 206 | OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape({}), &handle)); |
| 207 | handle->flat<ResourceHandle>()(0) = |
| 208 | ctx->step_container()->MakeResourceHandle<Stack>(key, *ctx->device()); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // StackPushOp |
| 213 |
nothing calls this directly
no test coverage detected