10000 Merge pull request #59590 from tensorflow/r2.11-2648c81f05f · IBMZ-Linux-OSS-Python/tensorflow@75f461c · GitHub
[go: up one dir, main page]

Skip to content

Commit 75f461c

Browse files
Merge pull request tensorflow#59590 from tensorflow/r2.11-2648c81f05f
r2.11 cherry-pick: 2648c81 "Check min-max values to be scalars."
2 parents f7ad74f + 49b7db4 commit 75f461c

File tree

2 files changed

+92
-48
lines changed

2 files changed

+92
-48
lines changed

tensorflow/core/kernels/mkl/mkl_qmatmul_op.cc

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,20 @@ class MklDnnQuantizedMatMulOp : public MklDnnMatMulOpBase<Tweight, Toutput> {
317317
// This is the case the inner-product and requantization are fused.
318318
// "min_freezed_output" and "max_freezed_output" are the requested range
319319
// for the output.
320-
min_output_value = context->input(7).flat<float>()(0);
321-
max_output_value = context->input(8).flat<float>()(0);
320+
const Tensor& min_freezed_tensor = context->input(7);
321+
const Tensor& max_freezed_tensor = context->input(8);
322+
OP_REQUIRES(context,
323+
TensorShapeUtils::IsScalar(min_freezed_tensor.shape()),
324+
errors::InvalidArgument(
325+
"`min_freezed_output` must be rank 0 but is rank ",
326+
min_freezed_tensor.dims()));
327+
OP_REQUIRES(context,
328+
TensorShapeUtils::IsScalar(max_freezed_tensor.shape()),
329+
errors::InvalidArgument(
330+
"`max_freezed_output` must be rank 0 but is rank ",
331+
max_freezed_tensor.dims()));
332+
min_output_value = min_freezed_tensor.scalar<float>()();
333+
max_output_value = max_freezed_tensor.scalar<float>()();
322334
} else {
323335
ComputeOutputRangeForInt32(context, &min_output_value, &max_output_value);
324336
}
@@ -344,10 +356,10 @@ class MklDnnQuantizedMatMulOp : public MklDnnMatMulOpBase<Tweight, Toutput> {
344356
void ComputeOutputRangeForInt32(OpKernelContext* context,
345357
float* min_output_value,
346358
float* max_output_value) {
347-
const float min_input = context->input(3).flat<float>()(0);
348-
const float max_input = context->input(4).flat<float>()(0);
349-
const float min_weight = context->input(5).flat<float>()(0);
350-
const float max_weight = context->input(6).flat<float>()(0);
359+
const float min_input = context->input(3).scalar<float>()();
360+
const float max_input = context->input(4).scalar<float>()();
361+
const float min_weight = context->input(5).scalar<float>()();
362+
const float max_weight = context->input(6).scalar<float>()();
351363
MklQuantizationRangeForMultiplication<quint8, qint8, qint32>(
352364
min_input, max_input, min_weight, max_weight, min_output_value,
353365
max_output_value);
@@ -361,6 +373,25 @@ class MklDnnQuantizedMatMulOp : public MklDnnMatMulOpBase<Tweight, Toutput> {
361373
params.dtypes.append(typeid(Tbias).name());
362374
params.dtypes.append(typeid(Toutput).name());
363375

376+
// min-max values for input and weight should be scalar.
377+
const Tensor& min_input_tensor = context->input(3);
378+
const Tensor& max_input_tensor = context->input(4);
379+
const Tensor& min_weight_tensor = context->input(5);
380+
const Tensor& max_weight_tensor = context->input(6);
381+
382+
OP_REQUIRES(context, TensorShapeUtils::IsScalar(min_input_tensor.shape()),
383+
errors::InvalidArgument("`min_a` must be rank 0 but is rank ",
384+
min_input_tensor.dims()));
385+
OP_REQUIRES(context, TensorShapeUtils::IsScalar(max_input_tensor.shape()),
386+
errors::InvalidArgument("`max_a` must be rank 0 but is rank ",
387+
max_input_tensor.dims()));
388+
OP_REQUIRES(context, TensorShapeUtils::IsScalar(min_weight_tensor.shape()),
389+
errors::InvalidArgument("`min_b` must be rank 0 but is rank ",
390+
min_weight_tensor.dims()));
391+
OP_REQUIRES(context, TensorShapeUtils::IsScalar(max_weight_tensor.shape()),
392+
errors::InvalidArgument("`max_b` must be rank 0 but is rank ",
393+
max_weight_tensor.dims()));
394+
364395
// When the output type is quint8, the output data is requantized into
365396
// quint8. A post_op "output_scale" is added to do the conversion.
366397
if (std::is_same<Toutput, quint8>::value ||
@@ -371,8 +402,21 @@ class MklDnnQuantizedMatMulOp : public MklDnnMatMulOpBase<Tweight, Toutput> {
371402
ComputeOutputRangeForInt32(context, &min_output_value, &max_output_value);
372403
float scale_int32 =
373404
std::max(std::abs(min_output_value), std::abs(max_output_value));
374-
const float min_freezed_output = context->input(7).flat<float>()(0);
375-
const float max_freezed_output = context->input(8).flat<float>()(0);
405+
const Tensor& min_freezed_tensor = context->input(7);
406+
const Tensor& max_freezed_tensor = context->input(8);
407+
// min-max values of freezed output range should be scalar.
408+
OP_REQUIRES(context,
409+
TensorShapeUtils::IsScalar(min_freezed_tensor.shape()),
410+
errors::InvalidArgument(
411+
"`min_freezed_output` must be rank 0 but is rank ",
412+
min_freezed_tensor.dims()));
413+
OP_REQUIRES(context,
414+
TensorShapeUtils::IsScalar(max_freezed_tensor.shape()),
415+
errors::InvalidArgument(
416+
"`max_freezed_output` must be rank 0 but is rank ",
417+
max_freezed_tensor.dims()));
418+
const float min_freezed_output = min_freezed_tensor.scalar<float>()();
419+
const float max_freezed_output = max_freezed_tensor.scalar<float>()();
376420
float scale_eightbit =
377421
std::max(std::abs(min_freezed_output), std::abs(max_freezed_output));
378422
float scale = 1.0;

tensorflow/core/kernels/mkl/mkl_qmatmul_op_test.cc

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
15-
#if defined(INTEL_MKL) && defined(ENABLE_MKL)
15+
#if defined(INTEL_MKL)
1616
#define EIGEN_USE_THREADS
1717

1818
#include <functional>
@@ -64,10 +64,10 @@ TEST_F(QuantizedMatMulTest, Small_withBias) {
6464
AddInputFromArray<qint8>(TensorShape({3, 4}),
6565
{7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18});
6666
AddInputFromArray<qint32>(TensorShape({4}), {1, 2, 3, 4});
67-
AddInputFromArray<float>(TensorShape({1}), {0});
68-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
69-
AddInputFromArray<float>(TensorShape({1}), {-127.0f});
70-
AddInputFromArray<float>(TensorShape({1}), {127.0f});
67+
AddInputFromArray<float>(TensorShape({}), {0});
68+
AddInputFromArray<float>(TensorShape({}), {255.0f});
69+
AddInputFromArray<float>(TensorShape({}), {-127.0f});
70+
AddInputFromArray<float>(TensorShape({}), {127.0f});
7171

7272
TF_ASSERT_OK(RunOpKernel());
7373
// Here are the results we expect, from hand calculations:
@@ -116,10 +116,10 @@ TEST_F(QuantizedMatMulTest, Small_withNegBias) {
116116
AddInputFromArray<qint8>(TensorShape({3, 4}),
117117
{7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18});
118118
AddInputFromArray<qint32>(TensorShape({4}), {100, -200, 300, -400});
119-
AddInputFromArray<float>(TensorShape({1}), {0});
120-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
121-
AddInputFromArray<float>(TensorShape({1}), {-127.0f});
122-
AddInputFromArray<float>(TensorShape({1}), {127.0f});
119+
AddInputFromArray<float>(TensorShape({}), {0});
120+
AddInputFromArray<float>(TensorShape({}), {255.0f});
121+
AddInputFromArray<float>(TensorShape({}), {-127.0f});
122+
AddInputFromArray<float>(TensorShape({}), {127.0f});
123123

124124
TF_ASSERT_OK(RunOpKernel());
125125
// Here are the results we expect, from hand calculations:
@@ -178,10 +178,10 @@ TEST_F(QuantizedMatMulTest, Small_WithNegInp) {
178178
AddInputFromArray<qint8>(TensorShape({3, 2}), {1, 4, 2, 5, 3, 6});
179179
// Bias
180180
AddInputFromArray<float>(TensorShape({2}), {10.0f, 20.0f});
181-
AddInputFromArray<float>(TensorShape({1}), {-12.0f});
182-
AddInputFromArray<float>(TensorShape({1}), {243.0f});
183-
AddInputFromArray<float>(TensorShape({1}), {-127.0f});
184-
AddInputFromArray<float>(TensorShape({1}), {127.0f});
181+
AddInputFromArray<float>(TensorShape({}), {-12.0f});
182+
AddInputFromArray<float>(TensorShape({}), {243.0f});
183+
AddInputFromArray<float>(TensorShape({}), {-127.0f});
184+
AddInputFromArray<float>(TensorShape({}), {127.0f});
185185

186186
TF_ASSERT_OK(RunOpKernel());
187187
// First calculate C = A * B,
@@ -240,12 +240,12 @@ TEST_F(QuantizedMatMulTest, Small_withBiasAndReq) {
240240
AddInputFromArray<qint8>(TensorShape({3, 4}),
241241
{7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18});
242242
AddInputFromArray<qint32>(TensorShape({4}), {10, -20, 30, -40});
243-
AddInputFromArray<float>(TensorShape({1}), {0});
244-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
245-
AddInputFromArray<float>(TensorShape({1}), {-127.0f});
246-
AddInputFromArray<float>(TensorShape({1}), {127.0f});
247-
AddInputFromArray<float>(TensorShape({1}), {0});
248-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
243+
AddInputFromArray<float>(TensorShape({}), {0});
244+
AddInputFromArray<float>(TensorShape({}), {255.0f});
245+
AddInputFromArray<float>(TensorShape({}), {-127.0f});
246+
AddInputFromArray<float>(TensorShape({}), {127.0f});
247+
AddInputFromArray<float>(TensorShape({}), {0});
248+
AddInputFromArray<float>(TensorShape({}), {255.0f});
249249

250250
TF_ASSERT_OK(RunOpKernel());
251251
// Here are the results we expect, from hand calculations:
@@ -308,12 +308,12 @@ TEST_F(QuantizedMatMulTest, Small_withBiasAndDeq) {
308308
AddInputFromArray<qint8>(TensorShape({3, 4}),
309309
{7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18});
310310
AddInputFromArray<qint32>(TensorShape({4}), {10, -20, 30, -40});
311-
AddInputFromArray<float>(TensorShape({1}), {0});
312-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
313-
AddInputFromArray<float>(TensorShape({1}), {-127.0f});
314-
AddInputFromArray<float>(TensorShape({1}), {127.0f});
315-
AddInputFromArray<float>(TensorShape({1}), {0});
316-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
311+
AddInputFromArray<float>(TensorShape({}), {0});
312+
AddInputFromArray<float>(TensorShape({}), {255.0f});
313+
AddInputFromArray<float>(TensorShape({}), {-127.0f});
314+
AddInputFromArray<float>(TensorShape({}), {127.0f});
315+
AddInputFromArray<float>(TensorShape({}), {0});
316+
AddInputFromArray<float>(TensorShape({}), {255.0f});
317317

318318
TF_ASSERT_OK(RunOpKernel());
319319
// Here are the results we expect, from hand calculations:
@@ -375,10 +375,10 @@ TEST_F(QuantizedMatMulTest, Small_withBiasAndRelu) {
375375
{7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18});
376376
AddInputFromArray<float>(TensorShape({4}),
377377
{100.0f, -200.0f, 300.0f, -400.0f});
378-
AddInputFromArray<float>(TensorShape({1}), {0});
379-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
380-
AddInputFromArray<float>(TensorShape({1}), {-127.0f});
381-
AddInputFromArray<float>(TensorShape({1}), {127.0f});
378+
AddInputFromArray<float>(TensorShape({}), {0});
379+
AddInputFromArray<float>(TensorShape({}), {255.0f});
380+
AddInputFromArray<float>(TensorShape({}), {-127.0f});
381+
AddInputFromArray<float>(TensorShape({}), {127.0f});
382382

383383
TF_ASSERT_OK(RunOpKernel());
384384
// Here are the results we expect, from hand calculations:
@@ -431,12 +431,12 @@ TEST_F(QuantizedMatMulTest, Small_withBiasAndReluAndReq) {
431431
AddInputFromArray<qint8>(TensorShape({3, 4}),
432432
{7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18});
433433
AddInputFromArray<qint32>(TensorShape({4}), {10, -20, 30, -40});
434-
AddInputFromArray<float>(TensorShape({1}), {0});
435-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
436-
AddInputFromArray<float>(TensorShape({1}), {-127.0f});
437-
AddInputFromArray<float>(TensorShape({1}), {127.0f});
438-
AddInputFromArray<float>(TensorShape({1}), {0});
439-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
434+
AddInputFromArray<float>(TensorShape({}), {0});
435+
AddInputFromArray<float>(TensorShape({}), {255.0f});
436+
AddInputFromArray<float>(TensorShape({}), {-127.0f});
437+
AddInputFromArray<float>(TensorShape({}), {127.0f});
438+
AddInputFromArray<float>(TensorShape({}), {0});
439+
AddInputFromArray<float>(TensorShape({}), {255.0f});
440440

441441
TF_ASSERT_OK(RunOpKernel());
442442
// Here are the results we expect, from hand calculations:
@@ -502,10 +502,10 @@ TEST_F(QuantizedMatMulTest, Small_withWeightCached) {
502502
AddInputFromArray<qint8>(TensorShape({3, 4}),
503503
{7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18});
504504
AddInputFromArray<qint32>(TensorShape({4}), {1, 2, 3, 4});
505-
AddInputFromArray<float>(TensorShape({1}), {0});
506-
AddInputFromArray<float>(TensorShape({1}), {255.0f});
507-
AddInputFromArray<float>(TensorShape({1}), {-127.0f});
508-
AddInputFromArray<float>(TensorShape({1}), {127.0f});
505+
AddInputFromArray<float>(TensorShape({}), {0});
506+
AddInputFromArray<float>(TensorShape({}), {255.0f});
507+
AddInputFromArray<float>(TensorShape({}), {-127.0f});
508+
AddInputFromArray<float>(TensorShape({}), {127.0f});
509509

510510
int64 start_time = Env::Default()->NowMicros();
511511
TF_ASSERT_OK(RunOpKernel());
@@ -543,4 +543,4 @@ TEST_F(QuantizedMatMulTest, Small_withWeightCached) {
543543

544544
} // namespace tensorflow
545545

546-
#endif // INTEL_MKL && ENABLE_MKL
546+
#endif // INTEL_MKL

0 commit comments

Comments
 (0)
0