# tensorflow::ops::QuantizedReshape

`#include <array_ops.h>`

Packs a list of `N` rank-`R` tensors into one rank-`(R+1)` tensor.

## Summary

Packs the `N` tensors in `values` into a tensor with rank one higher than each tensor in `values`, by packing them along the `axis` dimension. Given a list of tensors of shape `(A, B, C)`;

if `axis == 0` then the `output` tensor will have the shape `(N, A, B, C)`. if `axis == 1` then the `output` tensor will have the shape `(A, N, B, C)`. Etc.

For example:

# 'z' is [3, 6]

pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim. pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]]

```

This is the opposite of `unpack`.

Arguments:
* scope: A Scope object
* values: Must be of same shape and type.

Optional attributes (see `Attrs`):
* axis: Dimension along which to pack.  Negative values wrap around, so the
valid range is `[-(R+1), R+1)`.

Returns:
* `Output`: The packed tensor. */
class Stack {
public:
/// Optional attribute setters for Stack
struct Attrs {
/** Dimension along which to pack.  Negative values wrap around, so the
valid range is `[-(R+1), R+1)`.

Defaults to 0 */
TF_MUST_USE_RESULT Attrs Axis(int64 x) {
Attrs ret = *this;
ret.axis_ = x;
return ret;
}

int64 axis_ = 0;
};
Stack(const ::tensorflow::Scope& scope, ::tensorflow::InputList values);
Stack(const tensorflow::Scope& scope, tensorflow::InputList values, const
Stack::Attrs& attrs);
operator ::tensorflow::Output() const { return output; }
operator ::tensorflow::Input() const { return output; }
::tensorflow::Node* node() const { return output.node(); }

static Attrs Axis(int64 x) {
return Attrs().Axis(x);
}

Operation operation;
tensorflow::Output output;
};

/** Pads a tensor with zeros.

This operation pads a `input` with zeros according to the `paddings` you
specify. `paddings` is an integer tensor with shape `[Dn, 2]`, where n is the
rank of `input`. For each dimension D of `input`, `paddings[D, 0]` indicates
how many zeros to add before the contents of `input` in that dimension, and
`paddings[D, 1]` indicates how many zeros to add after the contents of `input`
in that dimension.

The padded size of each dimension D of the output is:

For example:

't' is [[1, 1], [2, 2]]

'paddings' is [[1, 1], [2, 2]]

rank of 't' is 2

pad(t, paddings) ==> [[0, 0, 0, 0, 0, 0] [0, 0, 1, 1, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 0, 0, 0, 0]]

Arguments:
* scope: A Scope object

Returns:
* `Output`: The output tensor. */
public:
operator ::tensorflow::Output() const { return output; }
operator ::tensorflow::Input() const { return output; }
::tensorflow::Node* node() const { return output.node(); }

Operation operation;
tensorflow::Output output;
};

you specify. `paddings` is an integer tensor with shape `[Dn, 2]`, where n is
the rank of `input`. For each dimension D of `input`, `paddings[D, 0]` indicates
how many padding values to add before the contents of `input` in that dimension,
of `input` in that dimension. `constant_values` is a scalar tensor of the same
type as `input` that indicates the value to use for padding `input`.

The padded size of each dimension D of the output is:

For example:

't' is [[1, 1], [2, 2]]

'paddings' is [[1, 1], [2, 2]]

'constant_values' is 0

rank of 't' is 2

pad(t, paddings) ==> [[0, 0, 0, 0, 0, 0] [0, 0, 1, 1, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 0, 0, 0, 0]]

Arguments:
* scope: A Scope object

Returns:
* `Output`: The output tensor. */
public:
operator ::tensorflow::Output() const { return output; }
operator ::tensorflow::Input() const { return output; }
::tensorflow::Node* node() const { return output.node(); }

Operation operation;
tensorflow::Output output;
};

/** Concatenates a list of `N` tensors along the first dimension.

The input tensors are all required to have size 1 in the first dimension.

For example:

'x' is [[1, 4]]

'y' is [[2, 5]]

'z' is [[3, 6]]

parallel_concat([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim.

The difference between concat and parallel_concat is that concat requires all
of the inputs be computed before the operation will begin but doesn't require
that the input shapes be known during graph construction.  Parallel concat
will copy pieces of the input into the output as they become available, in
some situations this can provide a performance benefit.

Arguments:
* scope: A Scope object
* values: Tensors to be concatenated. All must have size 1 in the first dimension
and same shape.
* shape: the final shape of the result; should be equal to the shapes of any input
but with the number of input values in the first dimension.

Returns:
* `Output`: The concatenated tensor. */
class ParallelConcat {
public:
ParallelConcat(const tensorflow::Scope& scope, tensorflow::InputList
values, PartialTensorShape shape);
operator ::tensorflow::Output() const { return output; }
operator ::tensorflow::Input() const { return output; }
::tensorflow::Node* node() const { return output.node(); }

Operation operation;
tensorflow::Output output;
};

/** A placeholder op for a value that will be fed into the computation.

N.B. This operation will fail with an error if it is executed. It is
intended as a way to represent a value that will always be fed, and to
provide attrs that enable the fed value to be checked at runtime.

Arguments:
* scope: A Scope object
* dtype: The type of elements in the tensor.

Optional attributes (see `Attrs`):
* shape: (Optional) The shape of the tensor. If the shape has 0 dimensions, the
shape is unconstrained.

Returns:
* `Output`: A placeholder tensor that must be replaced using the feed mechanism. */
class Placeholder {
public:
/// Optional attribute setters for Placeholder
struct Attrs {
/** (Optional) The shape of the tensor. If the shape has 0 dimensions, the
shape is unconstrained.

Defaults to  */
TF_MUST_USE_RESULT Attrs Shape(PartialTensorShape x) {
Attrs ret = *this;
ret.shape_ = x;
return ret;
}

PartialTensorShape shape_ = ::tensorflow::PartialTensorShape() /* unknown */;
};
Placeholder(const ::tensorflow::Scope& scope, DataType dtype);
Placeholder(const tensorflow::Scope& scope, DataType dtype, const
Placeholder::Attrs& attrs);
operator ::tensorflow::Output() const { return output; }
operator ::tensorflow::Input() const { return output; }
::tensorflow::Node* node() const { return output.node(); }

static Attrs Shape(PartialTensorShape x) {
return Attrs().Shape(x);
}

Operation operation;
tensorflow::Output output;
};

/** A placeholder op that passes through `input` when its output is not fed.

Arguments:
* scope: A Scope object
* input: The default value to produce when `output` is not fed.
* shape: The (possibly partial) shape of the tensor.

Returns:
* `Output`: A placeholder tensor that defaults to `input` if it is not fed. */
class PlaceholderWithDefault {
public:
PlaceholderWithDefault(const tensorflow::Scope& scope, tensorflow::Input
input, PartialTensorShape shape);
operator ::tensorflow::Output() const { return output; }
operator ::tensorflow::Input() const { return output; }
::tensorflow::Node* node() const { return output.node(); }

Operation operation;
tensorflow::Output output;
};

/** An identity op that triggers an error if a gradient is requested.

When executed in a graph, this op outputs its input tensor as-is.

will return an error when trying to lookup the gradient of this op,
because no gradient must ever be registered for this function.  This
op exists to prevent subtle bugs from silently returning unimplemented

Arguments:
* scope: A Scope object
* input: any tensor.

Optional attributes (see `Attrs`):
* message: Will be printed in the error when anyone tries to differentiate
this operation.

Returns:
* `Output`: the same input tensor. */
public:
/// Optional attribute setters for PreventGradient
struct Attrs {
/** Will be printed in the error when anyone tries to differentiate
this operation.

Defaults to "" */
TF_MUST_USE_RESULT Attrs Message(StringPiece x) {
Attrs ret = *this;
ret.message_ = x;
return ret;
}

StringPiece message_ = "";
};
operator ::tensorflow::Output() const { return output; }
operator ::tensorflow::Input() const { return output; }
::tensorflow::Node* node() const { return output.node(); }

static Attrs Message(StringPiece x) {
return Attrs().Message(x);
}

Operation operation;
tensorflow::Output output;
};

/** Quantizes then dequantizes a tensor.

This op simulates the precision loss from the quantized forward pass by:

1. Quantizing the tensor to fixed point numbers, which should match the target
quantization method when it is used in inference.
2. Dequantizing it back to floating point numbers for the following ops, most
likely matmul.

There are different ways to quantize. This version uses only scaling, so 0.0
maps to 0.

From the specified 'num_bits' in the quantized output type, it determines
minimum and maximum representable quantized values.

e.g.

*   [-128, 127] for signed, num_bits = 8, or
*   [0, 255] for unsigned, num_bits = 8.

If range_given == False, the initial input_min, input_max will be determined
automatically as the minimum and maximum values in the input tensor, otherwise
the specified values of input_min, input_max are used.

Note: If the input_min, input_max are specified, they do not need to equal the
actual minimum and maximum values in the tensor. e.g. in some cases it may be
beneficial to specify these values such that the low probability extremes of the
input distribution are clipped.

This op determines the maximum scale_factor that would map the initial
[input_min, input_max] range to a range that lies within the representable
quantized range.

It determines the scale from one of input_min and input_max, then updates the
other one to maximize the respresentable range.

e.g.

*   if the output is signed, num_bits = 8, [input_min, input_max] = [-10.0,
5.0]: it would use a scale_factor of -128 / -10.0 = 12.8 In this case, it
would update input_max to be 127 / 12.8 = 9.921875
*   if the output is signed, num_bits = 8, [input_min, input_max] = [-10.0,
10.0]: it would use a scale_factor of 127 / 10.0 = 12.7 In this case, it
would update input_min to be 128.0 / 12.7 = -10.07874
*   if the output is unsigned, input_min is forced to be 0, and only the
specified input_max is used.

After determining the scale_factor and updating the input range, it applies the
following to each value in the 'input' tensor.

output = round(clamp(value, input_min, input_max) * scale_factor) / scale_factor.

Arguments:
* scope: A Scope object
* input: Tensor to quantize and then dequantize.
* input_min: If `range_given == True`, this specifies the minimum input value that needs to
be represented, otherwise it is determined from the min value of the `input`
tensor.
* input_max: If `range_given == True`, this specifies the maximum input value that needs to
be represented, otherwise it is determined from the max value of the `input`
tensor.

Optional attributes (see `Attrs`):
* signed_input: Whether the quantization is signed or unsigned. (actually this parameter should
have been called `signed_output`)
* num_bits: The bitwidth of the quantization.
* range_given: Whether the range is given or should be determined from the `input` tensor.

Returns:
* `Output`: The output tensor. */
class QuantizeAndDequantizeV2 {
public:
/// Optional attribute setters for QuantizeAndDequantizeV2
struct Attrs {
/** Whether the quantization is signed or unsigned. (actually this parameter should
have been called `signed_output`)

Defaults to true */
TF_MUST_USE_RESULT Attrs SignedInput(bool x) {
Attrs ret = *this;
ret.signed_input_ = x;
return ret;
}

/** The bitwidth of the quantization.

Defaults to 8 */
TF_MUST_USE_RESULT Attrs NumBits(int64 x) {
Attrs ret = *this;
ret.num_bits_ = x;
return ret;
}

/** Whether the range is given or should be determined from the `input` tensor.

Defaults to false */
TF_MUST_USE_RESULT Attrs RangeGiven(bool x) {
Attrs ret = *this;
ret.range_given_ = x;
return ret;
}

bool signed_input_ = true;
int64 num_bits_ = 8;
bool range_given_ = false;
};
QuantizeAndDequantizeV2(const tensorflow::Scope& scope, tensorflow::Input
input, tensorflow::Input input_min,
tensorflow::Input input_max);
QuantizeAndDequantizeV2(const tensorflow::Scope& scope, tensorflow::Input
input, tensorflow::Input input_min,
tensorflow::Input input_max, const
QuantizeAndDequantizeV2::Attrs& attrs);
operator ::tensorflow::Output() const { return output; }
operator ::tensorflow::Input() const { return output; }
::tensorflow::Node* node() const { return output.node(); }

static Attrs SignedInput(bool x) {
return Attrs().SignedInput(x);
}
static Attrs NumBits(int64 x) {
return Attrs().NumBits(x);
}
static Attrs RangeGiven(bool x) {
return Attrs().RangeGiven(x);
}

Operation operation;
tensorflow::Output output;
};

/** Quantizes then dequantizes a tensor.

This is almost identical to QuantizeAndDequantizeV2, except that num_bits is a
tensor, so its value can change during training.

Arguments:
* scope: A Scope object

Returns:
* `Output`: The output tensor. */
class QuantizeAndDequantizeV3 {
public:
/// Optional attribute setters for QuantizeAndDequantizeV3
struct Attrs {
/// Defaults to true
TF_MUST_USE_RESULT Attrs SignedInput(bool x) {
Attrs ret = *this;
ret.signed_input_ = x;
return ret;
}

/// Defaults to true
TF_MUST_USE_RESULT Attrs RangeGiven(bool x) {
Attrs ret = *this;
ret.range_given_ = x;
return ret;
}

bool signed_input_ = true;
bool range_given_ = true;
};
QuantizeAndDequantizeV3(const tensorflow::Scope& scope, tensorflow::Input
input, tensorflow::Input input_min,
tensorflow::Input input_max, tensorflow::Input
num_bits);
QuantizeAndDequantizeV3(const tensorflow::Scope& scope, tensorflow::Input
input, tensorflow::Input input_min,
tensorflow::Input input_max, tensorflow::Input
num_bits, const QuantizeAndDequantizeV3::Attrs& attrs);
operator ::tensorflow::Output() const { return output; }
operator ::tensorflow::Input() const { return output; }
::tensorflow::Node* node() const { return output.node(); }

static Attrs SignedInput(bool x) {
return Attrs().SignedInput(x);
}
static Attrs RangeGiven(bool x) {
return Attrs().RangeGiven(x);
}

Operation operation;
tensorflow::Output output;
};

/** Quantize the 'input' tensor of type float to 'output' tensor of type 'T'.

[min_range, max_range] are scalar floats that specify the range for
the 'input' data. The 'mode' attribute controls exactly which calculations are
used to convert the float values to their quantized equivalents.  The
'round_mode' attribute controls which rounding tie-breaking algorithm is used
when rounding float values to their quantized equivalents.

In 'MIN_COMBINED' mode, each value of the tensor will undergo the following:

out[i] = (in[i] - min_range) * range(T) / (max_range - min_range) if T == qint8, out[i] -= (range(T) + 1) / 2.0

here `range(T) = numeric_limits::max() - numeric_limits::min()`

*MIN_COMBINED Mode Example*

Assume the input is type float and has a possible range of [0.0, 6.0] and the
output type is quint8 ([0, 255]). The min_range and max_range values should be
specified as 0.0 and 6.0. Quantizing from float to quint8 will multiply each
value of the input by 255/6 and cast to quint8.

If the output type was qint8 ([-128, 127]), the operation will additionally
subtract each value by 128 prior to casting, so that the range of values aligns
with the range of qint8.

If the mode is 'MIN_FIRST', then this approach is used:

num_discrete_values = 1 << (# of bits in T) range_adjust = num_discrete_values / (num_discrete_values - 1) range = (range_max - range_min) * range_adjust range_scale = num_discrete_values / range quantized = round(input * range_scale) - round(range_min * range_scale) + numeric_limits::min() quantized = max(quantized, numeric_limits::min()) quantized = min(quantized, numeric_limits::max())

The biggest difference between this and MIN_COMBINED is that the minimum range
is rounded first, before it's subtracted from the rounded value. With
MIN_COMBINED, a small bias is introduced where repeated iterations of quantizing
and dequantizing will introduce a larger and larger error.

*SCALED mode Example*

`SCALED` mode matches the quantization approach used in
`QuantizeAndDequantize{V2|V3}`.

If the mode is `SCALED`, we do not use the full range of the output type,
choosing to elide the lowest possible value for symmetry (e.g., output range is
-127 to 127, not -128 to 127 for signed 8 bit quantization), so that 0.0 maps to
0.

We first find the range of values in our tensor. The
range we use is always centered on 0, so we find m such that

c++ m = max(abs(input_min), abs(input_max))

Our input tensor range is then `[-m, m]`.

Next, we choose our fixed-point quantization buckets, `[min_fixed, max_fixed]`.
If T is signed, this is

num_bits = sizeof(T) * 8 [min_fixed, max_fixed] = [-(1 << (num_bits - 1) - 1), (1 << (num_bits - 1)) - 1]

Otherwise, if T is unsigned, the fixed-point range is

[min_fixed, max_fixed] = [0, (1 << num_bits) - 1]

From this we compute our scaling factor, s:

c++ s = (max_fixed - min_fixed) / (2 * m)

Now we can quantize the elements of our tensor:

c++ result = round(input * s)

One thing to watch out for is that the operator may choose to adjust the
requested minimum and maximum values slightly during the quantization process,
so you should always use the output ports as the range for further calculations.
For example, if the requested minimum and maximum values are close to equal,
they will be separated by a small epsilon value to prevent ill-formed quantized
buffers from being created. Otherwise, you can end up with buffers where all the
quantized values map to the same float value, which causes problems for
operations that have to perform further calculations on them.

Arguments:
* scope: A Scope object
* min_range: The minimum scalar value possibly produced for the input.
* max_range: The maximum scalar value possibly produced for the input.

Returns:
* `Output` output: The quantized data produced from the float input.
* `Output` output_min: The actual minimum scalar value used for the output.
* `Output` output_max: The actual maximum scalar value used for the output. */
class QuantizeV2 {
public:
/// Optional attribute setters for QuantizeV2
struct Attrs {
/// Defaults to "MIN_COMBINED"
TF_MUST_USE_RESULT Attrs Mode(StringPiece x) {
Attrs ret = *this;
ret.mode_ = x;
return ret;
}

/// Defaults to "HALF_AWAY_FROM_ZERO"
TF_MUST_USE_RESULT Attrs RoundMode(StringPiece x) {
Attrs ret = *this;
ret.round_mode_ = x;
return ret;
}

StringPiece mode_ = "MIN_COMBINED";
StringPiece round_mode_ = "HALF_AWAY_FROM_ZERO";
};
QuantizeV2(const tensorflow::Scope& scope, tensorflow::Input input,
tensorflow::Input min_range, tensorflow::Input max_range,
DataType T);
QuantizeV2(const tensorflow::Scope& scope, tensorflow::Input input,
tensorflow::Input min_range, tensorflow::Input max_range,
DataType T, const QuantizeV2::Attrs& attrs);

static Attrs Mode(StringPiece x) {
return Attrs().Mode(x);
}
static Attrs RoundMode(StringPiece x) {
return Attrs().RoundMode(x);
}

Operation operation;
tensorflow::Output output;
tensorflow::Output output_min;
tensorflow::Output output_max;
};

/** Concatenates quantized tensors along one dimension.

Arguments:
* scope: A Scope object
* concat_dim: 0-D.  The dimension along which to concatenate.  Must be in the
range [0, rank(values)).
* values: The `N` Tensors to concatenate. Their ranks and types must match,
and their sizes must match in all dimensions except `concat_dim`.
* input_mins: The minimum scalar values for each of the input tensors.
* input_maxes: The maximum scalar values for each of the input tensors.

Returns:
* `Output` output: A `Tensor` with the concatenation of values stacked along the
`concat_dim` dimension.  This tensor's shape matches that of `values` except
in `concat_dim` where it has the sum of the sizes.
* `Output` output_min: The float value that the minimum quantized output value represents.
* `Output` output_max: The float value that the maximum quantized output value represents. */
class QuantizedConcat {
public:
QuantizedConcat(const tensorflow::Scope& scope, tensorflow::Input
concat_dim, tensorflow::InputList values,
tensorflow::InputList input_mins, tensorflow::InputList
input_maxes);

Operation operation;
tensorflow::Output output;
tensorflow::Output output_min;
tensorflow::Output output_max;
};

/** Quantized Instance normalization.

Arguments:
* scope: A Scope object
* x: A 4D input Tensor.
* x_min: The value represented by the lowest quantized input.
* x_max: The value represented by the highest quantized input.

Optional attributes (see `Attrs`):
* output_range_given: If True, `given_y_min` and `given_y_min`
and `given_y_max` are used as the output range. Otherwise,
the implementation computes the output range.
* given_y_min: Output in `y_min` if `output_range_given` is True.
* given_y_max: Output in `y_max` if `output_range_given` is True.
* variance_epsilon: A small float number to avoid dividing by 0.
* min_separation: Minimum value of `y_max - y_min`

Returns:
* `Output` y: A 4D Tensor.
* `Output` y_min: The value represented by the lowest quantized output.
* `Output` y_max: The value represented by the highest quantized output. */
class QuantizedInstanceNorm {
public:
/// Optional attribute setters for QuantizedInstanceNorm
struct Attrs {
/** If True, `given_y_min` and `given_y_min`
and `given_y_max` are used as the output range. Otherwise,
the implementation computes the output range.

Defaults to false */
TF_MUST_USE_RESULT Attrs OutputRangeGiven(bool x) {
Attrs ret = *this;
ret.output_range_given_ = x;
return ret;
}

/** Output in `y_min` if `output_range_given` is True.

Defaults to 0 */
TF_MUST_USE_RESULT Attrs GivenYMin(float x) {
Attrs ret = *this;
ret.given_y_min_ = x;
return ret;
}

/** Output in `y_max` if `output_range_given` is True.

Defaults to 0 */
TF_MUST_USE_RESULT Attrs GivenYMax(float x) {
Attrs ret = *this;
ret.given_y_max_ = x;
return ret;
}

/** A small float number to avoid dividing by 0.

Defaults to 1e-05 */
TF_MUST_USE_RESULT Attrs VarianceEpsilon(float x) {
Attrs ret = *this;
ret.variance_epsilon_ = x;
return ret;
}

/** Minimum value of `y_max - y_min`

Defaults to 0.001 */
TF_MUST_USE_RESULT Attrs MinSeparation(float x) {
Attrs ret = *this;
ret.min_separation_ = x;
return ret;
}

bool output_range_given_ = false;
float given_y_min_ = 0.0f;
float given_y_max_ = 0.0f;
float variance_epsilon_ = 1e-05f;
float min_separation_ = 0.001f;
};
QuantizedInstanceNorm(const tensorflow::Scope& scope, tensorflow::Input x,
tensorflow::Input x_min, tensorflow::Input x_max);
QuantizedInstanceNorm(const tensorflow::Scope& scope, tensorflow::Input x,
tensorflow::Input x_min, tensorflow::Input x_max,
const QuantizedInstanceNorm::Attrs& attrs);

static Attrs OutputRangeGiven(bool x) {
return Attrs().OutputRangeGiven(x);
}
static Attrs GivenYMin(float x) {
return Attrs().GivenYMin(x);
}
static Attrs GivenYMax(float x) {
return Attrs().GivenYMax(x);
}
static Attrs VarianceEpsilon(float x) {
return Attrs().VarianceEpsilon(x);
}
static Attrs MinSeparation(float x) {
return Attrs().MinSeparation(x);
}

Operation operation;
tensorflow::Output y;
tensorflow::Output y_min;
tensorflow::Output y_max;
};

/** Reshapes a quantized tensor as per the Reshape op.

Arguments:
scope: A Scope object
shape: Defines the shape of the output tensor.
input_min: The minimum value of the input.
input_max: The maximum value of the input.

Returns:
`Output` output
`Output` output_min: This value is copied from input_min.
`Output` output_max: This value is copied from input_max.

Constructors and Destructors

`QuantizedReshape(const ::tensorflow::Scope & scope, ::tensorflow::Input tensor, ::tensorflow::Input shape, ::tensorflow::Input input_min, ::tensorflow::Input input_max)`

Public attributes

`operation`

`Operation`

`output`

`::tensorflow::Output`

`output_max`

`::tensorflow::Output`

`output_min`

`::tensorflow::Output`

Public attributes

operation
Operation operation

output
::tensorflow::Output output

output_max
::tensorflow::Output output_max

output_min
::tensorflow::Output output_min

Public functions

QuantizedReshape
QuantizedReshape(
const ::tensorflow::Scope & scope,
::tensorflow::Input tensor,
::tensorflow::Input shape,
::tensorflow::Input input_min,
::tensorflow::Input input_max
)

```