test_mechanisms.py 11.7 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 23 24 25
from mindarmour.privacy.diff_privacy import NoiseAdaGaussianRandom
from mindarmour.privacy.diff_privacy import AdaClippingWithGaussianRandom
from mindarmour.privacy.diff_privacy import NoiseMechanismsFactory
from mindarmour.privacy.diff_privacy import ClipMechanismsFactory
26 27 28 29


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
30
@pytest.mark.platform_arm_ascend_training
31 32
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
33
def test_graph_factory():
34
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
Z
zhenghuanhuan 已提交
35
    grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
36 37
    norm_bound = 1.0
    initial_noise_multiplier = 0.1
38
    alpha = 0.5
39
    decay_policy = 'Step'
40 41 42 43 44 45 46 47 48 49
    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,
50
                                    decay_policy=decay_policy)
51 52
    ada_noise = ada_noise_mech(grad)
    print('ada noise: ', ada_noise)
53 54 55 56


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


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
84
@pytest.mark.platform_arm_ascend_training
85 86
@pytest.mark.env_onecard
@pytest.mark.component_mindarmour
87 88
def test_pynative_gaussian():
    context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
Z
zhenghuanhuan 已提交
89
    grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
90 91
    norm_bound = 1.0
    initial_noise_multiplier = 0.1
92
    alpha = 0.5
93
    decay_policy = 'Step'
94 95 96 97 98 99 100 101 102 103
    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,
104
                                    decay_policy=decay_policy)
105 106
    ada_noise = ada_noise_mech(grad)
    print('ada noise: ', ada_noise)
107 108


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


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


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


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
175
@pytest.mark.platform_arm_ascend_training
Z
zhenghuanhuan 已提交
176 177 178 179 180 181 182 183
@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
184
    decay_policy = 'Exp'
185 186 187 188 189
    factory = NoiseMechanismsFactory()
    ada_noise = factory.create('AdaGaussian',
                               norm_bound,
                               initial_noise_multiplier,
                               noise_decay_rate=alpha,
190
                               decay_policy=decay_policy)
191
    ada_noise = ada_noise(grad)
Z
zhenghuanhuan 已提交
192
    print('ada noise: ', ada_noise)
Z
ZhidanLiu 已提交
193 194 195 196


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
197
@pytest.mark.platform_arm_ascend_training
Z
ZhidanLiu 已提交
198 199 200 201 202 203
@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)
204
    norm_bound = Tensor(1.0, mstype.float32)
Z
ZhidanLiu 已提交
205 206 207 208 209 210 211 212
    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)
213 214
    next_norm_bound = ada_clip(beta, norm_bound)
    print('Liner next norm clip:', next_norm_bound)
Z
ZhidanLiu 已提交
215 216 217 218 219 220 221

    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)
222 223
    next_norm_bound = ada_clip(beta, norm_bound)
    print('Geometric next norm clip:', next_norm_bound)
Z
ZhidanLiu 已提交
224 225 226 227


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
228
@pytest.mark.platform_arm_ascend_training
Z
ZhidanLiu 已提交
229 230 231 232 233 234
@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)
235
    norm_bound = Tensor(1.0, mstype.float32)
Z
ZhidanLiu 已提交
236 237 238 239 240 241 242 243
    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)
244 245
    next_norm_bound = ada_clip(beta, norm_bound)
    print('Liner next norm clip:', next_norm_bound)
Z
ZhidanLiu 已提交
246 247 248 249 250 251 252

    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)
253 254
    next_norm_bound = ada_clip(beta, norm_bound)
    print('Geometric next norm clip:', next_norm_bound)
Z
ZhidanLiu 已提交
255 256 257 258


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
259
@pytest.mark.platform_arm_ascend_training
Z
ZhidanLiu 已提交
260 261 262 263 264 265
@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)
266
    norm_bound = Tensor(1.0, mstype.float32)
Z
ZhidanLiu 已提交
267 268 269
    beta_stddev = 0.1
    learning_rate = 0.1
    target_unclipped_quantile = 0.3
270 271 272 273 274 275 276 277
    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 已提交
278 279 280 281


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
282
@pytest.mark.platform_arm_ascend_training
Z
ZhidanLiu 已提交
283 284 285 286 287 288
@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)
289
    norm_bound = Tensor(1.0, mstype.float32)
Z
ZhidanLiu 已提交
290 291 292
    beta_stddev = 0.1
    learning_rate = 0.1
    target_unclipped_quantile = 0.3
293 294 295 296 297 298 299 300
    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)