mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-14 20:57:59 +00:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/30072 Fix the test failure with mode/opt-lto by disabling openmp in both static and dynamic histograms. We will just use single thread in histogram processing as it's the common use case. Test Plan: ``` buck run mode/opt caffe2/caffe2/fb/fbgemm/numerical_debugger/workflows:int8_static_quantization_exporter -- --model-dir /mnt/public/summerdeng/ads/ --model-name downsized_ins_97293388_0.predictor --run --iter 10 --dataset-path /mnt/public/summerdeng/ads/ctr_instagram_story_int8/dataset/train/dataset_115764229_10 --hive-path="hive://ad_delivery/ig_ad_prefiltered_training_data_orc_injected/ds=2019-09-09/pipeline=ctr_instagram_story_click_only_model_opt_out_df" --collect-histogram --activation-histogram-file=/mnt/public/summerdeng/ads/ctr_instagram_story_int8/activation_histograms/dummy_debug_OOM.txt ``` ``` buck test mode/opt-lto caffe2/caffe2/quantization/server:dynamic_histogram_test -- --run-disabled ``` Reviewed By: hx89 Differential Revision: D18554614 fbshipit-source-id: cfff51174154e753b7123b4ec502b88ffc508917
75 lines
2 KiB
C++
75 lines
2 KiB
C++
#include <algorithm>
|
|
#include <array>
|
|
#include <random>
|
|
#include <sstream>
|
|
|
|
#include <gtest/gtest.h>
|
|
#include "caffe2/core/logging.h"
|
|
|
|
#include "dynamic_histogram.h"
|
|
|
|
using namespace std;
|
|
using namespace dnnlowp;
|
|
|
|
TEST(DynamicHistogram, HistSimilar) {
|
|
default_random_engine generator;
|
|
normal_distribution<float> distribution;
|
|
|
|
constexpr int n = 65536;
|
|
array<float, n> data; // make_array<float>(n);
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
data[i] = distribution(generator);
|
|
}
|
|
|
|
// Construct static and dynamic histogram and compare.
|
|
float minimum = *min_element(data.begin(), data.end());
|
|
float maximum = *max_element(data.begin(), data.end());
|
|
|
|
int nbins = 64;
|
|
Histogram static_hist(nbins, minimum, maximum);
|
|
vector<unique_ptr<DynamicHistogram>> dynamic_hist(3);
|
|
for (auto i = 0; i < dynamic_hist.size(); ++i) {
|
|
dynamic_hist[i].reset(new DynamicHistogram(nbins));
|
|
}
|
|
|
|
static_hist.Add(data.data(), n);
|
|
dynamic_hist[0]->Add(data.data(), n);
|
|
for (int i = 0; i < n; ++i) {
|
|
dynamic_hist[1]->Add(data[i]);
|
|
}
|
|
for (int i = 0; i < 64; ++i) {
|
|
dynamic_hist[2]->Add(data.data() + n / 64 * i, n / 64);
|
|
}
|
|
|
|
stringstream ss;
|
|
for (int i = 0; i < nbins; ++i) {
|
|
ss << (*static_hist.GetHistogram())[i] << " ";
|
|
}
|
|
LOG(INFO) << "static: " << ss.str();
|
|
|
|
vector<float> errors(dynamic_hist.size());
|
|
for (auto i = 0; i < dynamic_hist.size(); ++i) {
|
|
ss.str("");
|
|
const Histogram* dynamic_hist_result = dynamic_hist[i]->Finalize();
|
|
for (auto j = 0; j < nbins; ++j) {
|
|
ss << (*dynamic_hist_result->GetHistogram())[j] << " ";
|
|
}
|
|
LOG(INFO) << "dynamic " << i << " : " << ss.str();
|
|
|
|
// Compute the normalized squared error between the two histograms
|
|
float error = 0.0;
|
|
for (int j = 0; j < nbins; ++j) {
|
|
float e = (float)(*static_hist.GetHistogram())[j] -
|
|
(*dynamic_hist_result->GetHistogram())[j];
|
|
error += e * e;
|
|
}
|
|
error /= n;
|
|
LOG(INFO) << "error: " << error << endl;
|
|
errors[i] = error;
|
|
}
|
|
|
|
for (auto i = 0; i < dynamic_hist.size(); ++i) {
|
|
EXPECT_TRUE(errors[i] < 0.3);
|
|
}
|
|
}
|