ops add deterministicAlgorithms config

dd test\test_api\test_torch_deterministicalgorithms.py

dd test\test_api\test_torch_deterministicalgorithms.py

dd unpdate deterministicalgorithms.py

dd unpdate deterministicalgorithms.py
This commit is contained in:
guanlongfeng
2023-04-27 09:50:03 +08:00
parent e2af8aae2f
commit 7ef8d4794c
3 changed files with 87 additions and 0 deletions
@@ -0,0 +1,74 @@
# Copyright (c) 2023, Huawei Technologies.All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# 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 multiprocessing
import torch
from torch_npu.testing.testcase import TestCase, run_tests
class TorchDeterministicApiTestCase(TestCase):
def npu_op_exec(self, data, target, reduction):
loss = torch.nn.NLLLoss2d(reduction=reduction)
output = loss(data, target)
output = output.to("cpu")
output = output.detach().numpy()
return output
def test_npu_set_deterministic(self):
torch.use_deterministic_algorithms(True)
res = torch.are_deterministic_algorithms_enabled()
self.assertEqual(res, True)
torch.use_deterministic_algorithms(False)
res = torch.are_deterministic_algorithms_enabled()
self.assertEqual(res, False)
def test_npu_set_deterministic_false(self):
torch.use_deterministic_algorithms(False)
tensora = torch.randn(64, 3, 64, 64, dtype=torch.float, device="npu:0")
tensorsum = torch.sum(tensora)
index = 0
for i in range(100):
res = torch.sum(tensora)
if (res == tensorsum):
index = index +1
self.assertNotEqual(index, 100)
def test_npu_set_deterministic_true():
torch.use_deterministic_algorithms(True)
tensora = torch.randn(64, 3, 64, 64, dtype=torch.float, device="npu:0")
tensorsum = torch.sum(tensora)
index = 0
for i in range(100):
res = torch.sum(tensora)
if (res == tensorsum):
index = index +1
if (index != 100):
raise AssertionError("failed to test_npu_set_deterministic_true!")
def test_deterministic_newprocessing():
Process_jobs = []
multiprocessing.set_start_method("spawn")
p = multiprocessing.Process(target=test_npu_set_deterministic_true)
Process_jobs.append(p)
p.start()
p.join()
if __name__ == "__main__":
test_deterministic_newprocessing()
option = {}
option["ACL_OP_COMPILER_CACHE_MODE"] = "disable"
torch.npu.set_option(option)
run_tests()
+12
View File
@@ -123,6 +123,14 @@ namespace at_npu
}
}
void SetDeterministic() {
auto deterministicalgorithmsstatus = at::globalContext().deterministicAlgorithms();
if(deterministicaclnn_oldstatus != deterministicalgorithmsstatus){
AclSetCompileopt(aclCompileOpt::ACL_OP_DETERMINISTIC, deterministicalgorithmsstatus ? "1" : "0");
deterministicaclnn_oldstatus = deterministicalgorithmsstatus;
}
}
void OpCommandImpl::Run(
bool sync,
c10::SmallVector<int64_t, N> &sync_index,
@@ -158,6 +166,8 @@ namespace at_npu
auto stream = c10_npu::getCurrentNPUStream();
auto inputSize = params.inBuffer.size();
auto outputSize = params.outBuffer.size();
// open the deterministicAlgorithms config
SetDeterministic();
bool reset_flag = false;
if (ForceJitCompileList::GetInstance().Inlist(name) && env::CheckJitDisable())
{
@@ -271,6 +281,8 @@ namespace at_npu
torch_npu::profiler::PutMarkStamp(std::string(cur_paras->opType));
}
#endif
// open the deterministicAlgorithms config
SetDeterministic();
bool reset_flag = false;
if (!cur_paras->isJitDisable)
{
+1
View File
@@ -413,6 +413,7 @@ namespace at_npu
static std::unordered_map<std::thread::id, OpCommandImpls> opcommand_impls_map;
static std::mutex map_mutex;
static bool deterministicaclnn_oldstatus = false;
} // namespace native
} // namespace at_npu