Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eager global min_max oberver #7725

Merged
merged 11 commits into from
Mar 30, 2022
Next Next commit
eager global min_max oberver
liufengwei0103 committed Mar 8, 2022
commit 8bef6d27514e8d11dfaf6e3106a9a22f11d56963
81 changes: 81 additions & 0 deletions python/oneflow/test/modules/test_consistent_min_max_observer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
Copyright 2020 The OneFlow Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import unittest
import numpy as np
import oneflow as flow
import oneflow.unittest
from collections import OrderedDict
from oneflow.test_utils.automated_test_util import *
from oneflow.nn.modules import min_max_observer
from oneflow.test_utils.test_util import GenArgList
from oneflow.test.modules.test_min_max_observer import _check_min_max_observer


def _run_test_min_max_observer(
test_case,
placement,
sbp,
device_type,
weight_shape,
quantization_bit,
quantization_scheme,
quantization_formula,
per_layer_quantization,
):
weight = random_tensor(
len(weight_shape), *weight_shape, low=-0.5, high=0.5
).to_global(placement, sbp)
of_weight = weight.oneflow
np_weight = of_weight.numpy()

min_max_observer = flow.nn.MinMaxObserver(
quantization_formula=quantization_formula,
quantization_bit=quantization_bit,
quantization_scheme=quantization_scheme,
per_layer_quantization=per_layer_quantization,
)
scale, zero_point = min_max_observer(of_weight)
_check_min_max_observer(
test_case,
np_weight,
scale.numpy(),
zero_point.numpy(),
quantization_bit,
quantization_scheme,
quantization_formula,
per_layer_quantization,
)


class TestMinMaxObserver(flow.unittest.TestCase):
@globaltest
def test_min_max_observer(test_case):
arg_dict = OrderedDict()
arg_dict["device_type"] = ["cpu", "cuda"]
arg_dict["weight_shape"] = [(9, 48, 24, 10)]
arg_dict["quantization_bit"] = [8, 2]
arg_dict["quantization_scheme"] = ["symmetric", "affine"]
arg_dict["quantization_formula"] = ["google"]
arg_dict["per_layer_quantization"] = [True, False]
for arg in GenArgList(arg_dict):
for placement in all_placement():
for sbp in all_sbp(placement, valid_split_axis=[1, 2]):
_run_test_min_max_observer(test_case, placement, sbp, *arg)


if __name__ == "__main__":
unittest.main()