tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

pooling-and-reduction-keep-dims.https.any.js (4143B)


      1 // META: title=validation tests for pooling and reduction operators keep dimensions
      2 // META: global=window,worker
      3 // META: variant=?cpu
      4 // META: variant=?gpu
      5 // META: variant=?npu
      6 // META: script=../resources/utils.js
      7 // META: script=../resources/utils_validation.js
      8 // META: timeout=long
      9 
     10 'use strict';
     11 
     12 // This is used to reproduce an issue(crbug.com/331841268) of averagePool2d in
     13 // ResNetV2 50 model.
     14 //      [input]
     15 //         |
     16 //  [globalAveragePool]
     17 //         |
     18 //     [conv2d]
     19 //         |
     20 //     [reshape]
     21 //         |
     22 //     [output]
     23 promise_test(async t => {
     24  const builder = new MLGraphBuilder(context);
     25 
     26  const avgPool2dInputShape = [1, 7, 7, 2048];
     27  const avgPool2dInput = builder.input(
     28      `avgPool2dInput`, {dataType: 'float32', shape: avgPool2dInputShape});
     29  const avgPool2dOutput =
     30      builder.averagePool2d(avgPool2dInput, {layout: 'nhwc'});
     31  const conv2dFilterShape = [1001, 1, 1, 2048];
     32  const conv2dFilter = builder.constant(
     33      {dataType: 'float32', shape: conv2dFilterShape},
     34      new Float32Array(sizeOfShape(conv2dFilterShape)).fill(1));
     35  const conv2dBias = builder.constant(
     36      {dataType: 'float32', shape: [1001]}, new Float32Array(1001).fill(0.01));
     37  const conv2dOutput = builder.conv2d(avgPool2dOutput, conv2dFilter, {
     38    inputLayout: 'nhwc',
     39    filterLayout: 'ohwi',
     40    padding: [0, 0, 0, 0],
     41    bias: conv2dBias
     42  });
     43  const newShape = [1, 1001];
     44  const reshapeOutput = builder.reshape(conv2dOutput, newShape);
     45  assert_equals(reshapeOutput.dataType, avgPool2dInput.dataType);
     46  assert_array_equals(reshapeOutput.shape, newShape);
     47 
     48  const [graph, inputTensor, outputTensor] = await Promise.all([
     49    builder.build({reshapeOutput}),
     50    context.createTensor(
     51        {dataType: 'float32', shape: avgPool2dInputShape, writable: true}),
     52    context.createTensor({dataType: 'float32', shape: newShape, readable: true})
     53  ]);
     54 
     55  context.writeTensor(
     56      inputTensor,
     57      new Float32Array(sizeOfShape(avgPool2dInputShape)).fill(0.1));
     58 
     59  context.dispatch(
     60      graph, {'avgPool2dInput': inputTensor}, {'reshapeOutput': outputTensor});
     61  await context.readTensor(outputTensor);
     62 }, 'Test global average pool operator\'s output shape for ResNetV2 50 model.');
     63 
     64 // This is used to reproduce an issue(crbug.com/331841268) of reduceMean in
     65 // ResNetV2 50 model.
     66 //      [input]
     67 //         |
     68 //    [reduceMean]
     69 //         |
     70 //     [conv2d]
     71 //         |
     72 //     [reshape]
     73 //         |
     74 //     [output]
     75 promise_test(async t => {
     76  const builder = new MLGraphBuilder(context);
     77 
     78  const reduceMeanInputShape = [1, 7, 7, 2048];
     79  const reduceMeanInput = builder.input(
     80      `reduceMeanInput`, {dataType: 'float32', shape: reduceMeanInputShape});
     81  const reduceMeanOutput =
     82      builder.reduceMean(reduceMeanInput, {axes: [1, 2], keepDimensions: true});
     83  const conv2dFilterShape = [1001, 1, 1, 2048];
     84  const conv2dFilter = builder.constant(
     85      {dataType: 'float32', shape: conv2dFilterShape},
     86      new Float32Array(sizeOfShape(conv2dFilterShape)).fill(1));
     87  const conv2dBias = builder.constant(
     88      {dataType: 'float32', shape: [1001]}, new Float32Array(1001).fill(0.01));
     89  const conv2dOutput = builder.conv2d(reduceMeanOutput, conv2dFilter, {
     90    inputLayout: 'nhwc',
     91    filterLayout: 'ohwi',
     92    padding: [0, 0, 0, 0],
     93    bias: conv2dBias
     94  });
     95  const newShape = [1, 1001];
     96  const reshapeOutput = builder.reshape(conv2dOutput, newShape);
     97  assert_equals(reshapeOutput.dataType, reduceMeanInput.dataType);
     98  assert_array_equals(reshapeOutput.shape, newShape);
     99 
    100  const [graph, inputTensor, outputTensor] = await Promise.all([
    101    builder.build({reshapeOutput}),
    102    context.createTensor(
    103        {dataType: 'float32', shape: reduceMeanInputShape, writable: true}),
    104    context.createTensor({dataType: 'float32', shape: newShape, readable: true})
    105  ]);
    106 
    107  context.writeTensor(
    108      inputTensor,
    109      new Float32Array(sizeOfShape(reduceMeanInputShape)).fill(0.1));
    110 
    111  context.dispatch(
    112      graph, {'reduceMeanInput': inputTensor}, {'reshapeOutput': outputTensor});
    113  await context.readTensor(outputTensor);
    114 }, 'Test reduceMean operator\'s output shape for ResNetV2 50 model.');