You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_mechanisms.py 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. different Privacy test.
  16. """
  17. import pytest
  18. from mindspore import context
  19. from mindspore import Tensor
  20. from mindspore.common import dtype as mstype
  21. from mindarmour.privacy.diff_privacy import NoiseAdaGaussianRandom
  22. from mindarmour.privacy.diff_privacy import AdaClippingWithGaussianRandom
  23. from mindarmour.privacy.diff_privacy import NoiseMechanismsFactory
  24. from mindarmour.privacy.diff_privacy import ClipMechanismsFactory
  25. @pytest.mark.level0
  26. @pytest.mark.platform_x86_ascend_training
  27. @pytest.mark.platform_arm_ascend_training
  28. @pytest.mark.env_onecard
  29. @pytest.mark.component_mindarmour
  30. def test_graph_factory():
  31. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  32. grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
  33. norm_bound = 1.0
  34. initial_noise_multiplier = 0.1
  35. alpha = 0.5
  36. decay_policy = 'Step'
  37. factory = NoiseMechanismsFactory()
  38. noise_mech = factory.create('Gaussian',
  39. norm_bound,
  40. initial_noise_multiplier)
  41. noise = noise_mech(grad)
  42. print('Gaussian noise: ', noise)
  43. ada_noise_mech = factory.create('AdaGaussian',
  44. norm_bound,
  45. initial_noise_multiplier,
  46. noise_decay_rate=alpha,
  47. decay_policy=decay_policy)
  48. ada_noise = ada_noise_mech(grad)
  49. print('ada noise: ', ada_noise)
  50. @pytest.mark.level0
  51. @pytest.mark.platform_x86_ascend_training
  52. @pytest.mark.platform_arm_ascend_training
  53. @pytest.mark.env_onecard
  54. @pytest.mark.component_mindarmour
  55. def test_pynative_factory():
  56. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  57. grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
  58. norm_bound = 1.0
  59. initial_noise_multiplier = 0.1
  60. alpha = 0.5
  61. decay_policy = 'Step'
  62. factory = NoiseMechanismsFactory()
  63. noise_mech = factory.create('Gaussian',
  64. norm_bound,
  65. initial_noise_multiplier)
  66. noise = noise_mech(grad)
  67. print('Gaussian noise: ', noise)
  68. ada_noise_mech = factory.create('AdaGaussian',
  69. norm_bound,
  70. initial_noise_multiplier,
  71. noise_decay_rate=alpha,
  72. decay_policy=decay_policy)
  73. ada_noise = ada_noise_mech(grad)
  74. print('ada noise: ', ada_noise)
  75. @pytest.mark.level0
  76. @pytest.mark.platform_x86_ascend_training
  77. @pytest.mark.platform_arm_ascend_training
  78. @pytest.mark.env_onecard
  79. @pytest.mark.component_mindarmour
  80. def test_pynative_gaussian():
  81. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  82. grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
  83. norm_bound = 1.0
  84. initial_noise_multiplier = 0.1
  85. alpha = 0.5
  86. decay_policy = 'Step'
  87. factory = NoiseMechanismsFactory()
  88. noise_mech = factory.create('Gaussian',
  89. norm_bound,
  90. initial_noise_multiplier)
  91. noise = noise_mech(grad)
  92. print('Gaussian noise: ', noise)
  93. ada_noise_mech = factory.create('AdaGaussian',
  94. norm_bound,
  95. initial_noise_multiplier,
  96. noise_decay_rate=alpha,
  97. decay_policy=decay_policy)
  98. ada_noise = ada_noise_mech(grad)
  99. print('ada noise: ', ada_noise)
  100. @pytest.mark.level0
  101. @pytest.mark.platform_x86_ascend_training
  102. @pytest.mark.platform_arm_ascend_training
  103. @pytest.mark.env_onecard
  104. @pytest.mark.component_mindarmour
  105. def test_graph_ada_gaussian():
  106. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  107. grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
  108. norm_bound = 1.0
  109. initial_noise_multiplier = 0.1
  110. noise_decay_rate = 0.5
  111. decay_policy = 'Step'
  112. ada_noise_mech = NoiseAdaGaussianRandom(norm_bound,
  113. initial_noise_multiplier,
  114. seed=0,
  115. noise_decay_rate=noise_decay_rate,
  116. decay_policy=decay_policy)
  117. res = ada_noise_mech(grad)
  118. print(res)
  119. @pytest.mark.level0
  120. @pytest.mark.platform_x86_ascend_training
  121. @pytest.mark.platform_arm_ascend_training
  122. @pytest.mark.env_onecard
  123. @pytest.mark.component_mindarmour
  124. def test_pynative_ada_gaussian():
  125. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  126. grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
  127. norm_bound = 1.0
  128. initial_noise_multiplier = 0.1
  129. noise_decay_rate = 0.5
  130. decay_policy = 'Step'
  131. ada_noise_mech = NoiseAdaGaussianRandom(norm_bound,
  132. initial_noise_multiplier,
  133. seed=0,
  134. noise_decay_rate=noise_decay_rate,
  135. decay_policy=decay_policy)
  136. res = ada_noise_mech(grad)
  137. print(res)
  138. @pytest.mark.level0
  139. @pytest.mark.platform_x86_ascend_training
  140. @pytest.mark.platform_arm_ascend_training
  141. @pytest.mark.env_onecard
  142. @pytest.mark.component_mindarmour
  143. def test_graph_exponential():
  144. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  145. grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
  146. norm_bound = 1.0
  147. initial_noise_multiplier = 0.1
  148. alpha = 0.5
  149. decay_policy = 'Exp'
  150. factory = NoiseMechanismsFactory()
  151. ada_noise = factory.create('AdaGaussian',
  152. norm_bound,
  153. initial_noise_multiplier,
  154. noise_decay_rate=alpha,
  155. decay_policy=decay_policy)
  156. ada_noise = ada_noise(grad)
  157. print('ada noise: ', ada_noise)
  158. @pytest.mark.level0
  159. @pytest.mark.platform_x86_ascend_training
  160. @pytest.mark.platform_arm_ascend_training
  161. @pytest.mark.env_onecard
  162. @pytest.mark.component_mindarmour
  163. def test_pynative_exponential():
  164. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  165. grad = Tensor([0.3, 0.2, 0.4], mstype.float32)
  166. norm_bound = 1.0
  167. initial_noise_multiplier = 0.1
  168. alpha = 0.5
  169. decay_policy = 'Exp'
  170. factory = NoiseMechanismsFactory()
  171. ada_noise = factory.create('AdaGaussian',
  172. norm_bound,
  173. initial_noise_multiplier,
  174. noise_decay_rate=alpha,
  175. decay_policy=decay_policy)
  176. ada_noise = ada_noise(grad)
  177. print('ada noise: ', ada_noise)
  178. @pytest.mark.level0
  179. @pytest.mark.platform_x86_ascend_training
  180. @pytest.mark.platform_arm_ascend_training
  181. @pytest.mark.env_onecard
  182. @pytest.mark.component_mindarmour
  183. def test_ada_clip_gaussian_random_pynative():
  184. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  185. decay_policy = 'Linear'
  186. beta = Tensor(0.5, mstype.float32)
  187. norm_bound = Tensor(1.0, mstype.float32)
  188. beta_stddev = 0.1
  189. learning_rate = 0.1
  190. target_unclipped_quantile = 0.3
  191. ada_clip = AdaClippingWithGaussianRandom(decay_policy=decay_policy,
  192. learning_rate=learning_rate,
  193. target_unclipped_quantile=target_unclipped_quantile,
  194. fraction_stddev=beta_stddev,
  195. seed=1)
  196. next_norm_bound = ada_clip(beta, norm_bound)
  197. print('Liner next norm clip:', next_norm_bound)
  198. decay_policy = 'Geometric'
  199. ada_clip = AdaClippingWithGaussianRandom(decay_policy=decay_policy,
  200. learning_rate=learning_rate,
  201. target_unclipped_quantile=target_unclipped_quantile,
  202. fraction_stddev=beta_stddev,
  203. seed=1)
  204. next_norm_bound = ada_clip(beta, norm_bound)
  205. print('Geometric next norm clip:', next_norm_bound)
  206. @pytest.mark.level0
  207. @pytest.mark.platform_x86_ascend_training
  208. @pytest.mark.platform_arm_ascend_training
  209. @pytest.mark.env_onecard
  210. @pytest.mark.component_mindarmour
  211. def test_ada_clip_gaussian_random_graph():
  212. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  213. decay_policy = 'Linear'
  214. beta = Tensor(0.5, mstype.float32)
  215. norm_bound = Tensor(1.0, mstype.float32)
  216. beta_stddev = 0.1
  217. learning_rate = 0.1
  218. target_unclipped_quantile = 0.3
  219. ada_clip = AdaClippingWithGaussianRandom(decay_policy=decay_policy,
  220. learning_rate=learning_rate,
  221. target_unclipped_quantile=target_unclipped_quantile,
  222. fraction_stddev=beta_stddev,
  223. seed=1)
  224. next_norm_bound = ada_clip(beta, norm_bound)
  225. print('Liner next norm clip:', next_norm_bound)
  226. decay_policy = 'Geometric'
  227. ada_clip = AdaClippingWithGaussianRandom(decay_policy=decay_policy,
  228. learning_rate=learning_rate,
  229. target_unclipped_quantile=target_unclipped_quantile,
  230. fraction_stddev=beta_stddev,
  231. seed=1)
  232. next_norm_bound = ada_clip(beta, norm_bound)
  233. print('Geometric next norm clip:', next_norm_bound)
  234. @pytest.mark.level0
  235. @pytest.mark.platform_x86_ascend_training
  236. @pytest.mark.platform_arm_ascend_training
  237. @pytest.mark.env_onecard
  238. @pytest.mark.component_mindarmour
  239. def test_pynative_clip_mech_factory():
  240. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  241. decay_policy = 'Linear'
  242. beta = Tensor(0.5, mstype.float32)
  243. norm_bound = Tensor(1.0, mstype.float32)
  244. beta_stddev = 0.1
  245. learning_rate = 0.1
  246. target_unclipped_quantile = 0.3
  247. factory = ClipMechanismsFactory()
  248. ada_clip = factory.create('Gaussian',
  249. decay_policy=decay_policy,
  250. learning_rate=learning_rate,
  251. target_unclipped_quantile=target_unclipped_quantile,
  252. fraction_stddev=beta_stddev)
  253. next_norm_bound = ada_clip(beta, norm_bound)
  254. print('next_norm_bound: ', next_norm_bound)
  255. @pytest.mark.level0
  256. @pytest.mark.platform_x86_ascend_training
  257. @pytest.mark.platform_arm_ascend_training
  258. @pytest.mark.env_onecard
  259. @pytest.mark.component_mindarmour
  260. def test_graph_clip_mech_factory():
  261. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  262. decay_policy = 'Linear'
  263. beta = Tensor(0.5, mstype.float32)
  264. norm_bound = Tensor(1.0, mstype.float32)
  265. beta_stddev = 0.1
  266. learning_rate = 0.1
  267. target_unclipped_quantile = 0.3
  268. factory = ClipMechanismsFactory()
  269. ada_clip = factory.create('Gaussian',
  270. decay_policy=decay_policy,
  271. learning_rate=learning_rate,
  272. target_unclipped_quantile=target_unclipped_quantile,
  273. fraction_stddev=beta_stddev)
  274. next_norm_bound = ada_clip(beta, norm_bound)
  275. print('next_norm_bound: ', next_norm_bound)

MindArmour关注AI的安全和隐私问题。致力于增强模型的安全可信、保护用户的数据隐私。主要包含3个模块:对抗样本鲁棒性模块、Fuzz Testing模块、隐私保护与评估模块。 对抗样本鲁棒性模块 对抗样本鲁棒性模块用于评估模型对于对抗样本的鲁棒性,并提供模型增强方法用于增强模型抗对抗样本攻击的能力,提升模型鲁棒性。对抗样本鲁棒性模块包含了4个子模块:对抗样本的生成、对抗样本的检测、模型防御、攻防评估。