-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[AINode] Integrate device manager framework #16998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b84be4d
stash
CRZbulabula 71be1ba
seems finish
CRZbulabula c0124f2
append license
CRZbulabula 40e8456
spotless
CRZbulabula ab4ff9c
resolve suggestions
CRZbulabula 9e7132d
Apply suggestions from code review
CRZbulabula 14cce9e
Update pool_controller.py
CRZbulabula 1a0d249
fix pipeline bug
f05bdb6
bug fix & append it
CRZbulabula f50101e
Update inference_request_pool.py
CRZbulabula 86f1c76
less inference mem ratio
CRZbulabula 1be9516
append cpu concurrent forecast CI
CRZbulabula 9dc5627
Fix CI
CRZbulabula aa0c38c
delete useless set device
CRZbulabula File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
96 changes: 96 additions & 0 deletions
96
integration-test/src/test/java/org/apache/iotdb/ainode/it/AINodeDeviceManageIT.java
This file contains hidden or 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,96 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| package org.apache.iotdb.ainode.it; | ||
|
|
||
| import org.apache.iotdb.it.env.EnvFactory; | ||
| import org.apache.iotdb.it.framework.IoTDBTestRunner; | ||
| import org.apache.iotdb.itbase.category.AIClusterIT; | ||
| import org.apache.iotdb.itbase.env.BaseEnv; | ||
|
|
||
| import org.junit.AfterClass; | ||
| import org.junit.Assert; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.junit.experimental.categories.Category; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.ResultSet; | ||
| import java.sql.ResultSetMetaData; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.util.Arrays; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
|
|
||
| import static org.apache.iotdb.ainode.utils.AINodeTestUtils.checkHeader; | ||
| import static org.apache.iotdb.ainode.utils.AINodeTestUtils.prepareDataInTable; | ||
| import static org.apache.iotdb.ainode.utils.AINodeTestUtils.prepareDataInTree; | ||
|
|
||
| @RunWith(IoTDBTestRunner.class) | ||
| @Category({AIClusterIT.class}) | ||
| public class AINodeDeviceManageIT { | ||
|
|
||
| @BeforeClass | ||
| public static void setUp() throws Exception { | ||
| // Init 1C1D1A cluster environment | ||
| EnvFactory.getEnv().initClusterEnvironment(1, 1); | ||
| prepareDataInTree(); | ||
| prepareDataInTable(); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void tearDown() throws Exception { | ||
| EnvFactory.getEnv().cleanClusterEnvironment(); | ||
| } | ||
|
|
||
| @Test | ||
| public void showAIDeviceTestInTree() throws SQLException { | ||
| try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TREE_SQL_DIALECT); | ||
| Statement statement = connection.createStatement()) { | ||
| showAIDevicesTest(statement); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void showAIDeviceTestInTable() throws SQLException { | ||
| try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT); | ||
| Statement statement = connection.createStatement()) { | ||
| showAIDevicesTest(statement); | ||
| } | ||
| } | ||
|
|
||
| private void showAIDevicesTest(Statement statement) throws SQLException { | ||
| final String showSql = "SHOW AI_DEVICES"; | ||
| final List<String> expectedDeviceIdList = new LinkedList<>(Arrays.asList("0", "1", "cpu")); | ||
| final List<String> expectedDeviceTypeList = | ||
| new LinkedList<>(Arrays.asList("cuda", "cuda", "cpu")); | ||
| try (ResultSet resultSet = statement.executeQuery(showSql)) { | ||
| ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); | ||
| checkHeader(resultSetMetaData, "DeviceId,DeviceType"); | ||
| while (resultSet.next()) { | ||
| String deviceId = resultSet.getString(1); | ||
| String deviceType = resultSet.getString(2); | ||
| Assert.assertEquals(expectedDeviceIdList.remove(0), deviceId); | ||
| Assert.assertEquals(expectedDeviceTypeList.remove(0), deviceType); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,17 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # |
17 changes: 17 additions & 0 deletions
17
iotdb-core/ainode/iotdb/ainode/core/device/backend/__init__.py
This file contains hidden or 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,17 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # |
42 changes: 42 additions & 0 deletions
42
iotdb-core/ainode/iotdb/ainode/core/device/backend/base.py
This file contains hidden or 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,42 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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 enum import Enum | ||
| from typing import ContextManager, Optional, Protocol | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| class BackendType(Enum): | ||
| """ | ||
| Different types of supported computation backends. | ||
| AINode will automatically select the available backend according to the order defined here. | ||
| """ | ||
|
|
||
| CUDA = "cuda" | ||
| CPU = "cpu" | ||
|
|
||
|
|
||
| class BackendAdapter(Protocol): | ||
| type: BackendType | ||
|
|
||
| # device basics | ||
| def is_available(self) -> bool: ... | ||
| def device_count(self) -> int: ... | ||
| def make_device(self, index: Optional[int]) -> torch.device: ... | ||
| def set_device(self, index: int) -> None: ... |
37 changes: 37 additions & 0 deletions
37
iotdb-core/ainode/iotdb/ainode/core/device/backend/cpu_backend.py
This file contains hidden or 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,37 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # | ||
|
|
||
| import torch | ||
|
|
||
| from iotdb.ainode.core.device.backend.base import BackendAdapter, BackendType | ||
|
|
||
|
|
||
| class CPUBackend(BackendAdapter): | ||
| type = BackendType.CPU | ||
|
|
||
| def is_available(self) -> bool: | ||
| return True | ||
|
|
||
| def device_count(self) -> int: | ||
| return 1 | ||
|
|
||
| def make_device(self, index: int | None) -> torch.device: | ||
| return torch.device("cpu") | ||
|
|
||
| def set_device(self, index: int) -> None: | ||
| return None | ||
39 changes: 39 additions & 0 deletions
39
iotdb-core/ainode/iotdb/ainode/core/device/backend/cuda_backend.py
This file contains hidden or 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,39 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # | ||
|
|
||
| import torch | ||
|
|
||
| from iotdb.ainode.core.device.backend.base import BackendAdapter, BackendType | ||
|
|
||
|
|
||
| class CUDABackend(BackendAdapter): | ||
| type = BackendType.CUDA | ||
|
|
||
| def is_available(self) -> bool: | ||
| return torch.cuda.is_available() | ||
|
|
||
| def device_count(self) -> int: | ||
| return torch.cuda.device_count() | ||
|
|
||
| def make_device(self, index: int | None) -> torch.device: | ||
| if index is None: | ||
| raise ValueError("CUDA backend requires a valid device index") | ||
| return torch.device(f"cuda:{index}") | ||
|
|
||
| def set_device(self, index: int) -> None: | ||
| torch.cuda.set_device(index) | ||
CRZbulabula marked this conversation as resolved.
Show resolved
Hide resolved
|
||
49 changes: 49 additions & 0 deletions
49
iotdb-core/ainode/iotdb/ainode/core/device/device_utils.py
This file contains hidden or 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,49 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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 dataclasses import dataclass | ||
| from typing import Optional, Union | ||
|
|
||
| import torch | ||
|
|
||
| DeviceLike = Union[torch.device, str, int] | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DeviceSpec: | ||
| type: str | ||
| index: Optional[int] | ||
|
|
||
|
|
||
| def parse_device_like(x: DeviceLike) -> DeviceSpec: | ||
| if isinstance(x, int): | ||
| return DeviceSpec("index", x) | ||
|
|
||
| if isinstance(x, str): | ||
| try: | ||
| return DeviceSpec("index", int(x)) | ||
| except ValueError: | ||
| s = x.strip().lower() | ||
| if ":" in s: | ||
| t, idx = s.split(":", 1) | ||
| return DeviceSpec(t, int(idx)) | ||
| return DeviceSpec(s, None) | ||
|
|
||
| if isinstance(x, torch.device): | ||
| return DeviceSpec(x.type, x.index) | ||
|
|
||
| raise TypeError(f"Unsupported device: {x!r}") |
This file contains hidden or 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,39 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # | ||
|
|
||
| import os | ||
| from dataclasses import dataclass | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DistEnv: | ||
| rank: int | ||
| local_rank: int | ||
| world_size: int | ||
|
|
||
|
|
||
| def read_dist_env() -> DistEnv: | ||
| # torchrun: | ||
| rank = int(os.environ.get("RANK", "0")) | ||
| world_size = int(os.environ.get("WORLD_SIZE", "1")) | ||
|
|
||
| # torchrun provides LOCAL_RANK; slurm often provides SLURM_LOCALID | ||
| local_rank = os.environ.get("LOCAL_RANK", os.environ.get("SLURM_LOCALID", "0")) | ||
| local_rank = int(local_rank) | ||
|
|
||
| return DistEnv(rank=rank, local_rank=local_rank, world_size=world_size) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.