test_mechanisms.py 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright 2019 Huawei Technologies Co., Ltd
#
# 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.
"""
different Privacy test.
"""
import pytest

from mindspore import context
20 21
from mindspore import Tensor
from mindspore.common import dtype as mstype
22
from mindarmour.diff_privacy import NoiseAdaGaussianRandom
Z
ZhidanLiu 已提交
23 24 25
from mindarmour.diff_privacy import AdaClippingWithGaussianRandom
from mindarmour.diff_privacy import NoiseMechanismsFactory
from mindarmour.diff_privacy import ClipMechanismsFactory
26 27 28 29 30 31


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
32
def test_graph_factory():
33
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
Z
zhenghuanhuan 已提交
34
    grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
35 36
    norm_bound = 1.0
    initial_noise_multiplier = 0.1
37
    alpha = 0.5
38
    decay_policy = 'Step'
39 40 41 42 43 44 45 46 47 48
    factory = NoiseMechanismsFactory()
    noise_mech = factory.create('Gaussian',
                                norm_bound,
                                initial_noise_multiplier)
    noise = noise_mech(grad)
    print('Gaussian noise: ', noise)
    ada_noise_mech = factory.create('AdaGaussian',
                                    norm_bound,
                                    initial_noise_multiplier,
                                    noise_decay_rate=alpha,
49
                                    decay_policy=decay_policy)
50 51
    ada_noise = ada_noise_mech(grad)
    print('ada noise: ', ada_noise)
52 53 54 55 56 57


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
58
def test_pynative_factory():
59
    context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
Z
zhenghuanhuan 已提交
60
    grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
61 62
    norm_bound = 1.0
    initial_noise_multiplier = 0.1
63
    alpha = 0.5
64
    decay_policy = 'Step'
65 66 67 68 69 70 71 72 73 74
    factory = NoiseMechanismsFactory()
    noise_mech = factory.create('Gaussian',
                                norm_bound,
                                initial_noise_multiplier)
    noise = noise_mech(grad)
    print('Gaussian noise: ', noise)
    ada_noise_mech = factory.create('AdaGaussian',
                                    norm_bound,
                                    initial_noise_multiplier,
                                    noise_decay_rate=alpha,
75
                                    decay_policy=decay_policy)
76 77
    ada_noise = ada_noise_mech(grad)
    print('ada noise: ', ada_noise)
78 79 80 81 82 83


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
84 85
def test_pynative_gaussian():
    context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
Z
zhenghuanhuan 已提交
86
    grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
87 88
    norm_bound = 1.0
    initial_noise_multiplier = 0.1
89
    alpha = 0.5
90
    decay_policy = 'Step'
91 92 93 94 95 96 97 98 99 100
    factory = NoiseMechanismsFactory()
    noise_mech = factory.create('Gaussian',
                                norm_bound,
                                initial_noise_multiplier)
    noise = noise_mech(grad)
    print('Gaussian noise: ', noise)
    ada_noise_mech = factory.create('AdaGaussian',
                                    norm_bound,
                                    initial_noise_multiplier,
                                    noise_decay_rate=alpha,
101
                                    decay_policy=decay_policy)
102 103
    ada_noise = ada_noise_mech(grad)
    print('ada noise: ', ada_noise)
104 105


106 107 108 109
@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
110
def test_graph_ada_gaussian():
111
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
Z
zhenghuanhuan 已提交
112
    grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
113 114
    norm_bound = 1.0
    initial_noise_multiplier = 0.1
115
    noise_decay_rate = 0.5
116
    decay_policy = 'Step'
117 118 119 120
    ada_noise_mech = NoiseAdaGaussianRandom(norm_bound,
                                            initial_noise_multiplier,
                                            seed=0,
                                            noise_decay_rate=noise_decay_rate,
121
                                            decay_policy=decay_policy)
122 123
    res = ada_noise_mech(grad)
    print(res)
124 125


126 127 128 129 130
@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
def test_pynative_ada_gaussian():
131
    context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
Z
zhenghuanhuan 已提交
132
    grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
133 134
    norm_bound = 1.0
    initial_noise_multiplier = 0.1
135
    noise_decay_rate = 0.5
136
    decay_policy = 'Step'
137 138 139 140
    ada_noise_mech = NoiseAdaGaussianRandom(norm_bound,
                                            initial_noise_multiplier,
                                            seed=0,
                                            noise_decay_rate=noise_decay_rate,
141
                                            decay_policy=decay_policy)
142
    res = ada_noise_mech(grad)
143 144 145 146 147 148 149
    print(res)


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
150 151
def test_graph_exponential():
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
Z
zhenghuanhuan 已提交
152
    grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
153 154 155
    norm_bound = 1.0
    initial_noise_multiplier = 0.1
    alpha = 0.5
156
    decay_policy = 'Exp'
157 158 159 160 161
    factory = NoiseMechanismsFactory()
    ada_noise = factory.create('AdaGaussian',
                               norm_bound,
                               initial_noise_multiplier,
                               noise_decay_rate=alpha,
162
                               decay_policy=decay_policy)
163
    ada_noise = ada_noise(grad)
164
    print('ada noise: ', ada_noise)
Z
zhenghuanhuan 已提交
165 166 167 168 169 170 171 172 173 174 175 176


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
def test_pynative_exponential():
    context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
    grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
    norm_bound = 1.0
    initial_noise_multiplier = 0.1
    alpha = 0.5
177
    decay_policy = 'Exp'
178 179 180 181 182
    factory = NoiseMechanismsFactory()
    ada_noise = factory.create('AdaGaussian',
                               norm_bound,
                               initial_noise_multiplier,
                               noise_decay_rate=alpha,
183
                               decay_policy=decay_policy)
184
    ada_noise = ada_noise(grad)
Z
zhenghuanhuan 已提交
185
    print('ada noise: ', ada_noise)
Z
ZhidanLiu 已提交
186 187 188 189 190 191 192 193 194 195


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
def test_ada_clip_gaussian_random_pynative():
    context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
    decay_policy = 'Linear'
    beta = Tensor(0.5, mstype.float32)
196
    norm_bound = Tensor(1.0, mstype.float32)
Z
ZhidanLiu 已提交
197 198 199 200 201 202 203 204
    beta_stddev = 0.1
    learning_rate = 0.1
    target_unclipped_quantile = 0.3
    ada_clip = AdaClippingWithGaussianRandom(decay_policy=decay_policy,
                                             learning_rate=learning_rate,
                                             target_unclipped_quantile=target_unclipped_quantile,
                                             fraction_stddev=beta_stddev,
                                             seed=1)
205 206
    next_norm_bound = ada_clip(beta, norm_bound)
    print('Liner next norm clip:', next_norm_bound)
Z
ZhidanLiu 已提交
207 208 209 210 211 212 213

    decay_policy = 'Geometric'
    ada_clip = AdaClippingWithGaussianRandom(decay_policy=decay_policy,
                                             learning_rate=learning_rate,
                                             target_unclipped_quantile=target_unclipped_quantile,
                                             fraction_stddev=beta_stddev,
                                             seed=1)
214 215
    next_norm_bound = ada_clip(beta, norm_bound)
    print('Geometric next norm clip:', next_norm_bound)
Z
ZhidanLiu 已提交
216 217 218 219 220 221 222 223 224 225


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
def test_ada_clip_gaussian_random_graph():
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
    decay_policy = 'Linear'
    beta = Tensor(0.5, mstype.float32)
226
    norm_bound = Tensor(1.0, mstype.float32)
Z
ZhidanLiu 已提交
227 228 229 230 231 232 233 234
    beta_stddev = 0.1
    learning_rate = 0.1
    target_unclipped_quantile = 0.3
    ada_clip = AdaClippingWithGaussianRandom(decay_policy=decay_policy,
                                             learning_rate=learning_rate,
                                             target_unclipped_quantile=target_unclipped_quantile,
                                             fraction_stddev=beta_stddev,
                                             seed=1)
235 236
    next_norm_bound = ada_clip(beta, norm_bound)
    print('Liner next norm clip:', next_norm_bound)
Z
ZhidanLiu 已提交
237 238 239 240 241 242 243

    decay_policy = 'Geometric'
    ada_clip = AdaClippingWithGaussianRandom(decay_policy=decay_policy,
                                             learning_rate=learning_rate,
                                             target_unclipped_quantile=target_unclipped_quantile,
                                             fraction_stddev=beta_stddev,
                                             seed=1)
244 245
    next_norm_bound = ada_clip(beta, norm_bound)
    print('Geometric next norm clip:', next_norm_bound)
Z
ZhidanLiu 已提交
246 247 248 249 250 251 252 253 254 255


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
def test_pynative_clip_mech_factory():
    context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
    decay_policy = 'Linear'
    beta = Tensor(0.5, mstype.float32)
256
    norm_bound = Tensor(1.0, mstype.float32)
Z
ZhidanLiu 已提交
257 258 259
    beta_stddev = 0.1
    learning_rate = 0.1
    target_unclipped_quantile = 0.3
260 261 262 263 264 265 266 267
    factory = ClipMechanismsFactory()
    ada_clip = factory.create('Gaussian',
                              decay_policy=decay_policy,
                              learning_rate=learning_rate,
                              target_unclipped_quantile=target_unclipped_quantile,
                              fraction_stddev=beta_stddev)
    next_norm_bound = ada_clip(beta, norm_bound)
    print('next_norm_bound: ', next_norm_bound)
Z
ZhidanLiu 已提交
268 269 270 271 272 273 274 275 276 277


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
def test_graph_clip_mech_factory():
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
    decay_policy = 'Linear'
    beta = Tensor(0.5, mstype.float32)
278
    norm_bound = Tensor(1.0, mstype.float32)
Z
ZhidanLiu 已提交
279 280 281
    beta_stddev = 0.1
    learning_rate = 0.1
    target_unclipped_quantile = 0.3
282 283 284 285 286 287 288 289
    factory = ClipMechanismsFactory()
    ada_clip = factory.create('Gaussian',
                              decay_policy=decay_policy,
                              learning_rate=learning_rate,
                              target_unclipped_quantile=target_unclipped_quantile,
                              fraction_stddev=beta_stddev)
    next_norm_bound = ada_clip(beta, norm_bound)
    print('next_norm_bound: ', next_norm_bound)