forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NPU] Support dataloader on npu place. (PaddlePaddle#31867)
- Loading branch information
Showing
7 changed files
with
313 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (c) 2021 PaddlePaddle 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. | ||
|
||
#ifdef PADDLE_WITH_ASCEND_CL | ||
#include "paddle/fluid/platform/npu_resource_pool.h" | ||
#include "paddle/fluid/platform/npu_info.h" | ||
|
||
namespace paddle { | ||
namespace platform { | ||
|
||
NpuStreamResourcePool::NpuStreamResourcePool() { | ||
int dev_cnt = platform::GetNPUDeviceCount(); | ||
pool_.reserve(dev_cnt); | ||
for (int dev_idx = 0; dev_idx < dev_cnt; ++dev_idx) { | ||
auto creator = [dev_idx] { | ||
platform::SetNPUDeviceId(dev_idx); | ||
aclrtStream stream; | ||
PADDLE_ENFORCE_NPU_SUCCESS(aclrtCreateStream(&stream)); | ||
return stream; | ||
}; | ||
|
||
auto deleter = [dev_idx](aclrtStream stream) { | ||
platform::SetNPUDeviceId(dev_idx); | ||
PADDLE_ENFORCE_NPU_SUCCESS(aclrtDestroyStream(stream)); | ||
}; | ||
|
||
pool_.emplace_back(ResourcePool<NpuStreamObject>::Create(creator, deleter)); | ||
} | ||
} | ||
|
||
NpuStreamResourcePool& NpuStreamResourcePool::Instance() { | ||
static NpuStreamResourcePool pool; | ||
return pool; | ||
} | ||
|
||
std::shared_ptr<NpuStreamObject> NpuStreamResourcePool::New(int dev_idx) { | ||
PADDLE_ENFORCE_GE( | ||
dev_idx, 0, | ||
platform::errors::InvalidArgument( | ||
"The dev_idx should be not less than 0, but got %d.", dev_idx)); | ||
PADDLE_ENFORCE_LT( | ||
dev_idx, pool_.size(), | ||
platform::errors::OutOfRange( | ||
"The dev_idx should be less than device count %d, but got %d.", | ||
pool_.size(), dev_idx)); | ||
return pool_[dev_idx]->New(); | ||
} | ||
|
||
NpuEventResourcePool::NpuEventResourcePool() { | ||
int dev_cnt = platform::GetNPUDeviceCount(); | ||
pool_.reserve(dev_cnt); | ||
for (int dev_idx = 0; dev_idx < dev_cnt; ++dev_idx) { | ||
auto creator = [dev_idx] { | ||
platform::SetNPUDeviceId(dev_idx); | ||
aclrtEvent event; | ||
PADDLE_ENFORCE_NPU_SUCCESS(aclrtCreateEvent(&event)); | ||
return event; | ||
}; | ||
|
||
auto deleter = [dev_idx](aclrtEvent event) { | ||
platform::SetNPUDeviceId(dev_idx); | ||
PADDLE_ENFORCE_NPU_SUCCESS(aclrtDestroyEvent(event)); | ||
}; | ||
|
||
pool_.emplace_back(ResourcePool<NpuEventObject>::Create(creator, deleter)); | ||
} | ||
} | ||
|
||
NpuEventResourcePool& NpuEventResourcePool::Instance() { | ||
static NpuEventResourcePool pool; | ||
return pool; | ||
} | ||
|
||
std::shared_ptr<NpuEventObject> NpuEventResourcePool::New(int dev_idx) { | ||
PADDLE_ENFORCE_GE( | ||
dev_idx, 0, | ||
platform::errors::InvalidArgument( | ||
"The dev_idx should be not less than 0, but got %d.", dev_idx)); | ||
PADDLE_ENFORCE_LT( | ||
dev_idx, pool_.size(), | ||
platform::errors::OutOfRange( | ||
"The dev_idx should be less than device count %d, but got %d.", | ||
pool_.size(), dev_idx)); | ||
return pool_[dev_idx]->New(); | ||
} | ||
|
||
} // namespace platform | ||
} // namespace paddle | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 2021 PaddlePaddle 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. | ||
|
||
#pragma once | ||
|
||
#ifdef PADDLE_WITH_ASCEND_CL | ||
#include <memory> | ||
#include <type_traits> | ||
#include <vector> | ||
|
||
#include "acl/acl.h" | ||
#include "paddle/fluid/platform/resource_pool.h" | ||
|
||
namespace paddle { | ||
namespace platform { | ||
|
||
using NpuStreamObject = std::remove_pointer<aclrtStream>::type; | ||
using NpuEventObject = std::remove_pointer<aclrtEvent>::type; | ||
|
||
class NpuStreamResourcePool { | ||
public: | ||
std::shared_ptr<NpuStreamObject> New(int dev_idx); | ||
|
||
static NpuStreamResourcePool &Instance(); | ||
|
||
private: | ||
NpuStreamResourcePool(); | ||
|
||
DISABLE_COPY_AND_ASSIGN(NpuStreamResourcePool); | ||
|
||
private: | ||
std::vector<std::shared_ptr<ResourcePool<NpuStreamObject>>> pool_; | ||
}; | ||
|
||
class NpuEventResourcePool { | ||
public: | ||
std::shared_ptr<NpuEventObject> New(int dev_idx); | ||
|
||
static NpuEventResourcePool &Instance(); | ||
|
||
private: | ||
NpuEventResourcePool(); | ||
|
||
DISABLE_COPY_AND_ASSIGN(NpuEventResourcePool); | ||
|
||
private: | ||
std::vector<std::shared_ptr<ResourcePool<NpuEventObject>>> pool_; | ||
}; | ||
|
||
} // namespace platform | ||
} // namespace paddle | ||
|
||
#endif |
46 changes: 46 additions & 0 deletions
46
python/paddle/fluid/tests/unittests/test_dataloader_npu.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright (c) 2021 PaddlePaddle 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. | ||
|
||
from __future__ import division | ||
|
||
import sys | ||
import unittest | ||
import numpy as np | ||
|
||
import paddle | ||
from ..unittests.test_multiprocess_dataloader_static import TestStaticDataLoader | ||
|
||
paddle.enable_static() | ||
|
||
|
||
class TestStaticDataLoader(TestStaticDataLoader): | ||
def test_main(self): | ||
results = [] | ||
places = [paddle.NPUPlace(0)] | ||
|
||
for num_workers in [0, 2]: | ||
print(self.__class__.__name__, places, num_workers) | ||
sys.stdout.flush() | ||
ret = self._run_main( | ||
num_workers=num_workers, places=places, use_pe=False) | ||
results.append(ret) | ||
|
||
diff = np.max( | ||
np.abs(results[0]['loss'] - results[1]['loss']) / | ||
np.abs(results[0]['loss'])) | ||
self.assertLess(diff, 1e-2) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters