aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/ops/stateless_random_ops.cc
blob: 742709fb1836a0f7e3f0bd94f3dbc3e15423a271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Copyright 2015 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.
==============================================================================*/

#include "tensorflow/core/framework/common_shape_fns.h"
#include "tensorflow/core/framework/op.h"

namespace tensorflow {

using shape_inference::DimensionHandle;
using shape_inference::ShapeHandle;

static Status StatelessShape(shape_inference::InferenceContext* context) {
  // Check seed shape
  ShapeHandle seed;
  TF_RETURN_IF_ERROR(context->WithRank(context->input(1), 1, &seed));
  DimensionHandle unused;
  TF_RETURN_IF_ERROR(context->WithValue(context->Dim(seed, 0), 2, &unused));

  // Set output shape
  ShapeHandle out;
  TF_RETURN_IF_ERROR(context->MakeShapeFromShapeTensor(0, &out));
  context->set_output(0, out);
  return Status::OK();
}

#define REGISTER_STATELESS_OP(name)                  \
  REGISTER_OP(name)                                  \
      .Input("shape: T")                             \
      .Input("seed: Tseed")                          \
      .Output("output: dtype")                       \
      .Attr("dtype: {half,float,double} = DT_FLOAT") \
      .Attr("T: {int32, int64} = DT_INT32")          \
      .Attr("Tseed: {int32, int64} = DT_INT64")      \
      .SetShapeFn(StatelessShape)

// This op is exposed through contrib/stateless only.  The interface may change.
REGISTER_STATELESS_OP("StatelessRandomUniform");

// This op is exposed through contrib/stateless only.  The interface may change.
REGISTER_STATELESS_OP("StatelessRandomNormal");

// This op is exposed through contrib/stateless only.  The interface may change.
REGISTER_STATELESS_OP("StatelessTruncatedNormal");

// This op is exposed through contrib/stateless only.  The interface may change.
REGISTER_OP("StatelessMultinomial")
    .Input("logits: T")
    .Input("num_samples: int32")
    .Input("seed: Tseed")
    .Output("output: output_dtype")
    .Attr("T: realnumbertype")
    .Attr("Tseed: {int32, int64} = DT_INT64")
    .Attr("output_dtype: {int32, int64} = DT_INT64")
    .SetShapeFn([](shape_inference::InferenceContext* c) {
      // Check seed shape
      ShapeHandle seed;
      TF_RETURN_IF_ERROR(c->WithRank(c->input(2), 1, &seed));
      DimensionHandle unused_dim;
      TF_RETURN_IF_ERROR(c->WithValue(c->Dim(seed, 0), 2, &unused_dim));

      ShapeHandle logits_shape;
      ShapeHandle unused;
      DimensionHandle num_samples;
      TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 2, &logits_shape));
      TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &unused));
      TF_RETURN_IF_ERROR(c->MakeDimForScalarInput(1, &num_samples));
      c->set_output(0, c->Matrix(c->Dim(logits_shape, 0), num_samples));
      return Status::OK();
    });

#undef REGISTER_STATELESS_OP

}  // namespace tensorflow