oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

Reorder primitive for quantization not behaving as expected

Open diaena opened this issue 6 years ago • 8 comments

Hello,

I am trying to follow the int8 inference example but I've simplified so I can easily track the values and how they change. I've pasted the code below.

The problem I am having is with the reorder primitive which doesn't seem to change the values I am passing it. Here's the output I'm getting from the code below: image

Other peculiar behaviour is that random '0' that's printed out on the first line of the output. I've been able to narrow it down to the call that's creating the src_reorder_pd but I'm not why that is.

What am I missing? I didn't raise this as a bug because I wasn't sure if indeed I'm doing something wrong or not.

Thanks!


#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

#include "dnnl.hpp"
#include "example_utils.hpp"

using namespace dnnl;

using tag = memory::format_tag;
using dt = memory::data_type;

void fill_memory(float * tensor_base, const uint32_t length, float multiplier) {
    for(uint32_t i = 0; i < length; ++i) {
        tensor_base[i] = i * multiplier;
    }
}

void print_memory(dnnl::memory dnnl_memory, int length, const char* str) {
    std::cout << ">>>> " << str << std::endl;
    std::vector<float> handle_(length);
    read_from_dnnl_memory(handle_.data(), dnnl_memory);
    for (int i = 0; i < length; i++) std::cout << handle_[i] << " ";
    std::cout << std::endl;
}

int main(int argc, char **argv) {

    // Convolution parameters
    const int mb = 1; // batch (N)

    const int ih = 5; // input height
    const int iw = 5; // input width
    const int ic = 1; // input channels

    const int kh = 3; // filter kernel height
    const int kw = 3; // filter kernel width
    const int oc = 1; // output channels

    const int oh = 5; // output height
    const int ow = 5; // output width

    // Allocate memory for tensor bases
    auto src_base = (float *) std::calloc(iw*ih*ic*mb, sizeof(float));
    auto wei_base = (float *) std::calloc(kw*kh*ic*oc, sizeof(float));
    auto dst_base = (float *) std::calloc(ow*oh*oc*mb, sizeof(float));
    auto bia_base = (float *) std::calloc(oc, sizeof(float));

    // Fill in tensors with known data - for easy correctness testing
    fill_memory(src_base, iw*ih*ic*mb, 0.01f);
    fill_memory(wei_base, kw*kh*ic*oc, 0.02f);
    fill_memory(dst_base, ow*oh*oc*mb, 0.03f);
    fill_memory(bia_base, oc, 0.04f);

    // oneDNN engine and stream
    auto eng = engine(dnnl::engine::kind::cpu, 0);
    stream s(eng);

    /// Configure tensor shapes
    memory::dims conv_src_tz = {mb, ic, ih, iw};
    memory::dims conv_weights_tz = {oc, ic, kh, kw};
    memory::dims conv_bias_tz = {oc};
    memory::dims conv_dst_tz = {mb, oc, oh, ow};
    memory::dims conv_strides = {1, 1};
    memory::dims conv_padding = {1, 1};

    // These scales have been predetermined offline
    const float src_scale = 0.000941176;
    const float wei_scale = 0.000627451;
    const float bia_scale = 0;
    const float dst_scale = 0.002823530;

    // Set scaling factors for input, weight, output and bias quantization
    const std::vector<float> src_scales    = {src_scale};
    const std::vector<float> weight_scales = {wei_scale};
    const std::vector<float> bias_scales   = {bia_scale};
    const std::vector<float> dst_scales    = {dst_scale};
    // Choose channel-wise scaling factors for convolution
    const float conv_scale = (src_scale * wei_scale) / dst_scale;
    std::vector<float> conv_scales = {conv_scale};

    /// The *source, weights, bias* and *destination* datasets use the single-scale
    /// format with mask set to '0', while the *output* from the convolution
    /// (conv_scales) will use the array format where mask = 2 corresponding
    /// to the output channel dimension.
    const int src_mask = 0;
    const int weight_mask = 0;
    const int bias_mask = 0;
    const int dst_mask = 0;
    const int conv_mask = 0; // for simplicity

    /// Create the memory primitives for user data (source, weights, and bias).
    /// The user data will be in its original 32-bit floating point format.
    auto user_src_memory = memory({{conv_src_tz}, dt::f32, tag::nhwc}, eng);
    write_to_dnnl_memory(src_base, user_src_memory);
    auto user_weights_memory = memory({{conv_weights_tz}, dt::f32, tag::ohwi}, eng);
    write_to_dnnl_memory(wei_base, user_weights_memory);
    auto user_bias_memory = memory({{conv_bias_tz}, dt::f32, tag::x}, eng);
    write_to_dnnl_memory(bia_base, user_bias_memory);

    /// Create a memory descriptor for each convolution parameter.
    /// The convolution data uses 8-bit integer values, so the memory
    /// descriptors are configured as:
    /// * 8-bit unsigned (u8) for source and destination.
    /// * 8-bit signed (s8) for bias and weights.
    auto conv_src_md = memory::desc({conv_src_tz}, dt::s8, tag::any);
    auto conv_bias_md = memory::desc({conv_bias_tz}, dt::s32, tag::any);
    auto conv_weights_md = memory::desc({conv_weights_tz}, dt::s8, tag::any);
    auto conv_dst_md = memory::desc({conv_dst_tz}, dt::s8, tag::any);

    /// Create a convolution descriptor passing the int8 memory
    /// descriptors as parameters.
    auto conv_desc = convolution_forward::desc(prop_kind::forward,
            algorithm::convolution_auto, conv_src_md, conv_weights_md,
            conv_bias_md, conv_dst_md, conv_strides, conv_padding,
            conv_padding);

    /// Configuring int8-specific parameters in an int8 primitive is done
    /// via the Attributes Primitive.
    primitive_attr conv_attr;
    conv_attr.set_output_scales(conv_mask, conv_scales);

    // Check if int8 convolution is supported
    try {
        convolution_forward::primitive_desc(conv_desc, conv_attr, eng);
    } catch (error &e) {
        if (e.status == dnnl_unimplemented)
            throw example_allows_unimplemented {
                    "No int8 convolution implementation is available for this "
                    "platform.\n"
                    "Please refer to the developer guide for details."};

        // On any other error just re-throw
        throw;
    }

    /// Create a primitive descriptor using the convolution descriptor
    /// and passing along the int8 attributes in the constructor. The primitive
    /// descriptor for the convolution will contain the specific memory
    /// formats for the computation.
    auto conv_prim_desc
            = convolution_forward::primitive_desc(conv_desc, conv_attr, eng);

    /// * Quantize data and weights *
    /// Create a memory for each of the convolution's data input
    /// parameters (source, bias, weights, and destination). Using the convolution
    /// primitive descriptor as the creation parameter enables oneDNN
    /// to configure the memory formats for the convolution.
    ///
    /// Scaling parameters are passed to the reorder primitive via the attributes
    /// primitive.
    ///
    /// User memory must be transformed into convolution-friendly memory
    /// (for int8 and memory format). A reorder layer performs the data
    /// transformation from f32 (the original user data) into int8 format
    /// (the data used for the convolution). In addition, the reorder
    /// transforms the user data into the required memory format (as explained
    /// in the simple_net example).
    auto conv_src_memory = memory(conv_prim_desc.src_desc(), eng);
    primitive_attr src_attr;
    src_attr.set_output_scales(src_mask, src_scales);
    auto src_reorder_pd
            = reorder::primitive_desc(eng, user_src_memory.get_desc(), eng,
                    conv_src_memory.get_desc(), src_attr);
    auto src_reorder = reorder(src_reorder_pd);
    src_reorder.execute(s, user_src_memory, conv_src_memory);
    print_memory(user_src_memory, mb*ic*ih*iw, "user_src_memory");
    print_memory(conv_src_memory, mb*ic*ih*iw, "conv_src_memory (reordered)");

    auto conv_weights_memory = memory(conv_prim_desc.weights_desc(), eng);
    primitive_attr weight_attr;
    weight_attr.set_output_scales(weight_mask, weight_scales);
    auto weight_reorder_pd
            = reorder::primitive_desc(eng, user_weights_memory.get_desc(), eng,
                    conv_weights_memory.get_desc(), weight_attr);
    auto weight_reorder = reorder(weight_reorder_pd);
    weight_reorder.execute(s, user_weights_memory, conv_weights_memory);
    print_memory(user_weights_memory, oc*ic*kh*kw, "user_wei_memory");
    print_memory(conv_weights_memory, oc*ic*kh*kw, "conv_wei_memory (reordered)");

    auto conv_bias_memory = memory(conv_prim_desc.bias_desc(), eng);
    primitive_attr bias_attr;
    bias_attr.set_output_scales(bias_mask, bias_scales);
    auto bias_reorder_pd
            = reorder::primitive_desc(eng, user_bias_memory.get_desc(), eng,
                    conv_bias_memory.get_desc(), bias_attr);
    auto bias_reorder = reorder(bias_reorder_pd);
    bias_reorder.execute(s, user_bias_memory, conv_bias_memory);
    print_memory(user_bias_memory, oc, "user_bia_memory");
    print_memory(conv_bias_memory, oc, "conv_bia_memory (reordered)");

    // No reordering needed for dst because there is no user memory format yet
    auto conv_dst_memory = memory(conv_prim_desc.dst_desc(), eng);

    /// Create the convolution primitive. The int8 example
    /// computes the same Convolution +ReLU layers from AlexNet simple-net.cpp
    /// using the int8 and PostOps approach.
    auto conv = convolution_forward(conv_prim_desc);
    conv.execute(s,
            {{DNNL_ARG_SRC, conv_src_memory},
                    {DNNL_ARG_WEIGHTS, conv_weights_memory},
                    {DNNL_ARG_BIAS, conv_bias_memory},
                    {DNNL_ARG_DST, conv_dst_memory}});
    print_memory(conv_dst_memory, mb*oc*oh*ow, "conv_dst_memory (convolution output)");

    /// *Dequantize the result*
    /// Finally, *dst memory* may be dequantized from int8 into the original
    /// f32 format. Create a memory primitive for the user data in the original
    /// 32-bit floating point format and then apply a reorder to transform the
    /// computation output data.
    auto user_dst_memory = memory({{conv_dst_tz}, dt::f32, tag::nchw}, eng);
    write_to_dnnl_memory(dst_base, user_dst_memory);
    primitive_attr dst_attr;
    dst_attr.set_output_scales(dst_mask, dst_scales);
    auto dst_reorder_pd
            = reorder::primitive_desc(eng, conv_dst_memory.get_desc(), eng,
                    user_dst_memory.get_desc(), dst_attr);
    auto dst_reorder = reorder(dst_reorder_pd);
    dst_reorder.execute(s, conv_dst_memory, user_dst_memory);

    s.wait();

    print_memory(conv_dst_memory, mb*oc*oh*ow, "conv_dst_memory (de-quantized)");
    printf(">>>> DONE\n");
}

diaena avatar Nov 04 '20 21:11 diaena

Hi @diaena , thanks for your report! The example itself looks fine, I need some time to reproduce it locally.

igorsafo avatar Nov 04 '20 22:11 igorsafo

Ok, there are a couple issues:

  • method print_memory() expects dnnl_memory to contain float values, which is not true for conv_*_memory, so it prints incorrect values.

  • method set_output_scales() sets a scale which will be used in the following way: out = scale * in. As I understand in the example the *_scale is a scale to dequantize from s8 to f32. So to quantize f32 into s8 scale should be equal to 1.0f / scale.

igorsafo avatar Nov 04 '20 22:11 igorsafo

Hi Igor, thanks a lot! I managed to print more reasonable values, but I'm still confused about that '0' print? Any ideas?

diaena avatar Nov 05 '20 11:11 diaena

Hi Diana,

Could you please explain more on '0' print?

Meanwhile here are more observations:

  • There is an issue -- example expects both user and primitive memory to have the same size, however memory descriptors created by a primitive can have different size comparing to user memory descriptors. For example, primitive can request memory with some dimensions padded by 0s for performance reasons. An appropriate way to print memory would be to query size from memory and use it in print_memory():
void print_memory(dnnl::memory dnnl_memory, const char* str) {
    std::cout << ">>>> " << str << std::endl;
    const int size = dnnl_memory.get_desc().get_size();
    ...

print_memory(user_weights_memory, "user_wei_memory");
print_memory(conv_weights_memory, "conv_wei_memory (reordered)");
  • When I tried to reproduce this example on Intel Broadwell CPU I got an error "unimplemented" for weights reorder. This is caused by a weights reorder for combination of user's tag ohwi and convolution tag OIhw2i8o4i, because there is no instantiation of simple_reorder for this combination, because no one used ohwi for s8s8 convolution before (?). A workaround is to change user tag for weights in the example or to add this tag to a simple reorder.

  • const float bia_scale = 0; doesn't make much sense to me, because we will multiple bias data on it and it will result to 0s in bias, I changed it to 1.

  • print_memory(conv_dst_memory, mb*oc*oh*ow, "conv_dst_memory (de-quantized)"); prints not de-quantzed memory.

After I fixed all these issues I got the following:

>>>> user_src_memory
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24
>>>> conv_src_memory (reordered)
0 11 21 32 43 53 64 74 85 96 106 117 127 127 127 127 127 127 127 127 127 127 127 127 127
>>>> user_wei_memory
0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16
>>>> conv_wei_memory (reordered)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 96 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -128 -32 -2 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>>> user_bia_memory
0
>>>> conv_bia_memory (reordered)
0
>>>> conv_dst_memory (convolution output)
6 10 12 15 10 15 22 24 26 16 21 29 29 30 18 23 30 31 31 18 10 13 13 13 7
>>>> conv_dst_memory (de-quantized)
2125 3541.67 4250 5312.5 3541.67 5312.5 7791.67 8500 9208.33 5666.67 7437.5 10270.8 10270.8 10625 6375 8145.83 10625 10979.2 10979.2 6375 3541.67 4604.17 4604.17 4604.17 2479.17
>>>> DONE

Zeros in weights memory is a padding which i mentioned before. This is implementation - specific so you could not see it on your machine.

Here is the final version of source code:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

#include "dnnl.hpp"
#include "example_utils.hpp"

using namespace dnnl;

using tag = memory::format_tag;
using dt = memory::data_type;

void fill_memory(float * tensor_base, const uint32_t length, float multiplier) {
    for(uint32_t i = 0; i < length; ++i) {
        tensor_base[i] = i * multiplier;
    }
}

void print_memory(dnnl::memory dnnl_memory, const char* str) {
    std::cout << ">>>> " << str << std::endl;
    const int size = dnnl_memory.get_desc().get_size();
    switch (dnnl_memory.get_desc().data_type()) {
        case dt::f32:
            {
                int length = size / sizeof(float);
                std::vector<float> handle_(length);
                read_from_dnnl_memory(handle_.data(), dnnl_memory);
                for (int i = 0; i < length; i++)
                    std::cout << handle_[i] << " ";
                break;
            }
        case dt::s8:
            {
                int length = size / sizeof(int8_t);
                std::vector<int8_t> s8_handle_(length);
                read_from_dnnl_memory(s8_handle_.data(), dnnl_memory);
                for (int i = 0; i < length; i++)
                    std::cout << static_cast<float>(s8_handle_[i]) << " ";
                break;
             }
        case dt::s32:
            {
                int length = size / sizeof(int32_t);
                std::vector<int32_t> s32_handle_(length);
                read_from_dnnl_memory(s32_handle_.data(), dnnl_memory);
                for (int i = 0; i < length; i++)
                    std::cout << static_cast<float>(s32_handle_[i]) << " ";
                break;
             }
    default: break;
    }
    std::cout << std::endl;
}

int main(int argc, char **argv) {

    // Convolution parameters
    const int mb = 1; // batch (N)

    const int ih = 5; // input height
    const int iw = 5; // input width
    const int ic = 1; // input channels

    const int kh = 3; // filter kernel height
    const int kw = 3; // filter kernel width
    const int oc = 1; // output channels

    const int oh = 5; // output height
    const int ow = 5; // output width

    // Allocate memory for tensor bases
    auto src_base = (float *) std::calloc(iw*ih*ic*mb, sizeof(float));
    auto wei_base = (float *) std::calloc(kw*kh*ic*oc, sizeof(float));
    auto dst_base = (float *) std::calloc(ow*oh*oc*mb, sizeof(float));
    auto bia_base = (float *) std::calloc(oc, sizeof(float));

    // Fill in tensors with known data - for easy correctness testing
    fill_memory(src_base, iw*ih*ic*mb, 0.01f);
    fill_memory(wei_base, kw*kh*ic*oc, 0.02f);
    fill_memory(dst_base, ow*oh*oc*mb, 0.03f);
    fill_memory(bia_base, oc, 0.04f);

    // oneDNN engine and stream
    auto eng = engine(dnnl::engine::kind::cpu, 0);
    stream s(eng);

    /// Configure tensor shapes
    memory::dims conv_src_tz = {mb, ic, ih, iw};
    memory::dims conv_weights_tz = {oc, ic, kh, kw};
    memory::dims conv_bias_tz = {oc};
    memory::dims conv_dst_tz = {mb, oc, oh, ow};
    memory::dims conv_strides = {1, 1};
    memory::dims conv_padding = {1, 1};

    // These scales have been predetermined offline
    const float src_scale = 0.000941176;
    const float wei_scale = 0.000627451;
    const float bia_scale = 1;
    const float dst_scale = 0.002823530;

    // Set scaling factors for input, weight, output and bias quantization
    const std::vector<float> src_scales    = {1.0f / src_scale};
    const std::vector<float> weight_scales = {1.0f / wei_scale};
    const std::vector<float> bias_scales   = {1.0f / bia_scale};
    const std::vector<float> dst_scales    = {1.0f / dst_scale};
    // Choose channel-wise scaling factors for convolution
    const float conv_scale = (src_scale * wei_scale) / dst_scale;
    std::vector<float> conv_scales = {conv_scale};

    /// The *source, weights, bias* and *destination* datasets use the single-scale
    /// format with mask set to '0', while the *output* from the convolution
    /// (conv_scales) will use the array format where mask = 2 corresponding
    /// to the output channel dimension.
    const int src_mask = 0;
    const int weight_mask = 0;
    const int bias_mask = 0;
    const int dst_mask = 0;
    const int conv_mask = 0; // for simplicity

    /// Create the memory primitives for user data (source, weights, and bias).
    /// The user data will be in its original 32-bit floating point format.
    auto user_src_memory = memory({{conv_src_tz}, dt::f32, tag::nhwc}, eng);
    write_to_dnnl_memory(src_base, user_src_memory);
    auto user_weights_memory = memory({{conv_weights_tz}, dt::f32, tag::ohwi}, eng);
    write_to_dnnl_memory(wei_base, user_weights_memory);
    auto user_bias_memory = memory({{conv_bias_tz}, dt::f32, tag::x}, eng);
    write_to_dnnl_memory(bia_base, user_bias_memory);

    /// Create a memory descriptor for each convolution parameter.
    /// The convolution data uses 8-bit integer values, so the memory
    /// descriptors are configured as:
    /// * 8-bit unsigned (u8) for source and destination.
    /// * 8-bit signed (s8) for bias and weights.
    auto conv_src_md = memory::desc({conv_src_tz}, dt::s8, tag::any);
    auto conv_bias_md = memory::desc({conv_bias_tz}, dt::s32, tag::any);
    auto conv_weights_md = memory::desc({conv_weights_tz}, dt::s8, tag::any);
    auto conv_dst_md = memory::desc({conv_dst_tz}, dt::s8, tag::any);

    /// Create a convolution descriptor passing the int8 memory
    /// descriptors as parameters.
    auto conv_desc = convolution_forward::desc(prop_kind::forward,
            algorithm::convolution_auto, conv_src_md, conv_weights_md,
            conv_bias_md, conv_dst_md, conv_strides, conv_padding,
            conv_padding);

    /// Configuring int8-specific parameters in an int8 primitive is done
    /// via the Attributes Primitive.
    primitive_attr conv_attr;
    conv_attr.set_output_scales(conv_mask, conv_scales);

    // Check if int8 convolution is supported
    try {
        convolution_forward::primitive_desc(conv_desc, conv_attr, eng);
    } catch (error &e) {
        if (e.status == dnnl_unimplemented)
            throw example_allows_unimplemented {
                    "No int8 convolution implementation is available for this "
                    "platform.\n"
                    "Please refer to the developer guide for details."};

        // On any other error just re-throw
        throw;
    }

    /// Create a primitive descriptor using the convolution descriptor
    /// and passing along the int8 attributes in the constructor. The primitive
    /// descriptor for the convolution will contain the specific memory
    /// formats for the computation.
    auto conv_prim_desc
            = convolution_forward::primitive_desc(conv_desc, conv_attr, eng);

    /// * Quantize data and weights *
    /// Create a memory for each of the convolution's data input
    /// parameters (source, bias, weights, and destination). Using the convolution
    /// primitive descriptor as the creation parameter enables oneDNN
    /// to configure the memory formats for the convolution.
    ///
    /// Scaling parameters are passed to the reorder primitive via the attributes
    /// primitive.
    ///
    /// User memory must be transformed into convolution-friendly memory
    /// (for int8 and memory format). A reorder layer performs the data
    /// transformation from f32 (the original user data) into int8 format
    /// (the data used for the convolution). In addition, the reorder
    /// transforms the user data into the required memory format (as explained
    /// in the simple_net example).
    auto conv_src_memory = memory(conv_prim_desc.src_desc(), eng);
    primitive_attr src_attr;
    src_attr.set_output_scales(src_mask, src_scales);
    auto src_reorder_pd
            = reorder::primitive_desc(eng, user_src_memory.get_desc(), eng,
                    conv_src_memory.get_desc(), src_attr);
    auto src_reorder = reorder(src_reorder_pd);
    src_reorder.execute(s, user_src_memory, conv_src_memory);
    print_memory(user_src_memory, "user_src_memory");
    print_memory(conv_src_memory, "conv_src_memory (reordered)");

    auto conv_weights_memory = memory(conv_prim_desc.weights_desc(), eng);
    primitive_attr weight_attr;
    weight_attr.set_output_scales(weight_mask, weight_scales);
    auto weight_reorder_pd
            = reorder::primitive_desc(eng, user_weights_memory.get_desc(), eng,
                    conv_weights_memory.get_desc(), weight_attr);
    auto weight_reorder = reorder(weight_reorder_pd);
    weight_reorder.execute(s, user_weights_memory, conv_weights_memory);
    print_memory(user_weights_memory, "user_wei_memory");
    print_memory(conv_weights_memory, "conv_wei_memory (reordered)");

    auto conv_bias_memory = memory(conv_prim_desc.bias_desc(), eng);
    primitive_attr bias_attr;
    bias_attr.set_output_scales(bias_mask, bias_scales);
    auto bias_reorder_pd
            = reorder::primitive_desc(eng, user_bias_memory.get_desc(), eng,
                    conv_bias_memory.get_desc(), bias_attr);
    auto bias_reorder = reorder(bias_reorder_pd);
    bias_reorder.execute(s, user_bias_memory, conv_bias_memory);
    print_memory(user_bias_memory, "user_bia_memory");
    print_memory(conv_bias_memory, "conv_bia_memory (reordered)");

    // No reordering needed for dst because there is no user memory format yet
    auto conv_dst_memory = memory(conv_prim_desc.dst_desc(), eng);

    /// Create the convolution primitive. The int8 example
    /// computes the same Convolution +ReLU layers from AlexNet simple-net.cpp
    /// using the int8 and PostOps approach.
    auto conv = convolution_forward(conv_prim_desc);
    conv.execute(s,
            {{DNNL_ARG_SRC, conv_src_memory},
                    {DNNL_ARG_WEIGHTS, conv_weights_memory},
                    {DNNL_ARG_BIAS, conv_bias_memory},
                    {DNNL_ARG_DST, conv_dst_memory}});
    print_memory(conv_dst_memory, "conv_dst_memory (convolution output)");

    /// *Dequantize the result*
    /// Finally, *dst memory* may be dequantized from int8 into the original
    /// f32 format. Create a memory primitive for the user data in the original
    /// 32-bit floating point format and then apply a reorder to transform the
    /// computation output data.
    auto user_dst_memory = memory({{conv_dst_tz}, dt::f32, tag::nchw}, eng);
    write_to_dnnl_memory(dst_base, user_dst_memory);
    primitive_attr dst_attr;
    dst_attr.set_output_scales(dst_mask, dst_scales);
    auto dst_reorder_pd
            = reorder::primitive_desc(eng, conv_dst_memory.get_desc(), eng,
                    user_dst_memory.get_desc(), dst_attr);
    auto dst_reorder = reorder(dst_reorder_pd);
    dst_reorder.execute(s, conv_dst_memory, user_dst_memory);

    s.wait();

    print_memory(user_dst_memory, "user_dst_memory (de-quantized)");
    printf(">>>> DONE\n");
}

igorsafo avatar Nov 05 '20 22:11 igorsafo

Hi Igor, thanks a lot for for the updated source code, it makes plenty of sense and I managed to make some progress with my investigation.

In regards to the '0'-print, it's peculiar that you don't seem to get it, it might only be happening on my arm platform then?

So to make it more clear, I patched the updated code you pasted with this:

diff --git a/examples/my_int8_conv.cpp b/examples/my_int8_conv.cpp
index d0f0c31e6..9c4a4cab0 100644
--- a/examples/my_int8_conv.cpp
+++ b/examples/my_int8_conv.cpp
@@ -189,10 +189,12 @@ int main(int argc, char **argv) {
     /// in the simple_net example).
     auto conv_src_memory = memory(conv_prim_desc.src_desc(), eng);
     primitive_attr src_attr;
+    std::cout << "(before src_reorder_pd desc)\n";
     src_attr.set_output_scales(src_mask, src_scales);
     auto src_reorder_pd
             = reorder::primitive_desc(eng, user_src_memory.get_desc(), eng,
                     conv_src_memory.get_desc(), src_attr);
+    std::cout << "(after src_reorder_pd desc)\n";
     auto src_reorder = reorder(src_reorder_pd);
     src_reorder.execute(s, user_src_memory, conv_src_memory);
     print_memory(user_src_memory, "user_src_memory");

... so I can be able to show you the random '0' print I am talking about in the output that I get from running the program:

(before src_reorder_pd desc)
0
(after src_reorder_pd desc)
>>>> user_src_memory
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24
>>>> conv_src_memory (reordered)
0 11 21 32 43 53 64 74 85 96 106 117 127 127 127 127 127 127 127 127 127 127 127 127 127
>>>> user_wei_memory
0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16
>>>> conv_wei_memory (reordered)
0 32 64 96 127 127 127 127 127 -128 98 -2 -1
>>>> user_bia_memory
0
>>>> conv_bia_memory (reordered)
0
>>>> conv_dst_memory (convolution output)
3 6 8 9 7 9 14 16 17 11 14 20 21 21 13 16 22 22 22 14 9 12 12 12 7
>>>> user_dst_memory (de-quantized)
1062.5 2125 2833.33 3187.5 2479.17 3187.5 4958.33 5666.67 6020.83 3895.83 4958.33 7083.33 7437.5 7437.5 4604.17 5666.67 7791.67 7791.67 7791.67 4958.33 3187.5 4250 4250 4250 2479.17
>>>> DONE

Any ideas as to where to look?

Thanks, Diana

diaena avatar Nov 09 '20 18:11 diaena

Hi again,

I have new information. I build oneDNN without ACL and ran a plain benchdnn command with int8 and then f32 and I'm still seeing the 0-print but only for the int8.

$ ./tests/benchdnn/benchdnn --conv --mode=perf --dir=FWD_I --cfg=s8s8s8 --attr-oscale=common:2.25 --batch=./tests/benchdnn/inputs/conv/shapes_resnet_50
0
Output template: perf,%engine%,%impl%,%name%,%prb%,%Gops%,%Gfreq%,%-time%,%-Gflops%,%0time%,%0Gflops%
perf,cpu,igemm_s8u8s32:ref,"resnet_50:conv1",--conv --dir=FWD_I --cfg=s8s8s8 --attr-oscale=common:2.25 g1mb50ic3ih224oc64oh112kh7sh2ph3n"resnet_50:conv1",11.6215,0,245.323,47.372,246.891,47.0712
<...> // removed for readability
perf,cpu,igemm_s8u8s32:ref,"resnet_50:res5b_branch2a*2",--conv --dir=FWD_I --cfg=s8s8s8 --attr-oscale=common:2.25 mb50ic2048ih7oc512oh7kh1ph0n"resnet_50:res5b_branch2a*2",5.13802,0,430.867,11.9249,441.131,11.6474
tests:20 passed:20 skipped:0 mistrusted:0 unimplemented:0 failed:0 listed:0
total perf: min(ms):5314.49 avg(ms):5417.09

and for non-int8, there is no 0-print:

$ ./tests/benchdnn/benchdnn --conv --mode=perf --dir=FWD_I --batch=./tests/benchdnn/inputs/conv/shapes_resnet_50
Output template: perf,%engine%,%impl%,%name%,%prb%,%Gops%,%Gfreq%,%-time%,%-Gflops%,%0time%,%0Gflops%
perf,cpu,gemm:ref,"resnet_50:conv1",--conv --dir=FWD_I g1mb50ic3ih224oc64oh112kh7sh2ph3n"resnet_50:conv1",11.6215,0,170.091,68.3248,174.73,66.5108
<...> // removed for readability
perf,cpu,gemm:ref,"resnet_50:res5b_branch2a*2",--conv --dir=FWD_I mb50ic2048ih7oc512oh7kh1ph0n"resnet_50:res5b_branch2a*2",5.13802,0,185.117,27.7555,233.287,22.0244
tests:20 passed:20 skipped:0 mistrusted:0 unimplemented:0 failed:0 listed:0
total perf: min(ms):3628.56 avg(ms):3804.47

This is rather odd, isn't it?

Diana

diaena avatar Nov 09 '20 21:11 diaena

Hi Diana, Turned out this is an aarch64 - specific issue, I was able to reproduce it under qemu. Please review #879

Regards, Igor Safonov

igorsafo avatar Nov 10 '20 10:11 igorsafo

Thanks Igor, I will close this issue now.

diaena avatar Nov 10 '20 10:11 diaena