|
| 1 | +# Copyright 1999-2021 Alibaba Group Holding 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 | +import numpy as np |
| 16 | + |
| 17 | +from .....config import Config |
| 18 | +from .....core import ChunkGraph |
| 19 | +from .....tensor.random import TensorRand |
| 20 | +from .....tensor.arithmetic import TensorAdd |
| 21 | +from .....tensor.fetch import TensorFetch |
| 22 | +from .....resource import Resource |
| 23 | +from ...core import Task |
| 24 | +from ..analyzer import GraphAnalyzer |
| 25 | +from ..assigner import GraphAssigner |
| 26 | + |
| 27 | + |
| 28 | +def test_assigner_with_fetch_inputs(): |
| 29 | + band_num = 8 |
| 30 | + all_bands = [(f"address_{i}", "numa-0") for i in range(band_num)] |
| 31 | + inputs = [ |
| 32 | + TensorFetch(key=str(i), source_key=str(i), dtype=np.dtype(int)).new_chunk([]) |
| 33 | + for i in range(band_num) |
| 34 | + ] |
| 35 | + no_fetch_inputs = [TensorRand(i).new_chunk([]) for i in range(4)] |
| 36 | + results = [TensorAdd(lhs=inp, rhs=1).new_chunk([inp]) for inp in inputs] |
| 37 | + cur_assigns = dict( |
| 38 | + (fetch_chunk.op.key, band[0][0]) |
| 39 | + for fetch_chunk, band in zip(reversed(inputs), all_bands) |
| 40 | + ) |
| 41 | + |
| 42 | + chunk_graph = ChunkGraph() |
| 43 | + for fetch_chunk, add_chunk in zip(inputs, results): |
| 44 | + chunk_graph.add_node(fetch_chunk) |
| 45 | + chunk_graph.add_node(add_chunk) |
| 46 | + chunk_graph.add_edge(fetch_chunk, add_chunk) |
| 47 | + for inp in no_fetch_inputs: |
| 48 | + results.append(inp) |
| 49 | + chunk_graph.add_node(inp) |
| 50 | + chunk_graph.results = results |
| 51 | + |
| 52 | + band_resource = dict((band, Resource(num_cpus=1)) for band in all_bands) |
| 53 | + |
| 54 | + task = Task("mock_task", "mock_session") |
| 55 | + analyzer = GraphAnalyzer(chunk_graph, band_resource, task, Config()) |
| 56 | + subtask_graph = analyzer.gen_subtask_graph(cur_assigns) |
| 57 | + |
| 58 | + assigner = GraphAssigner( |
| 59 | + chunk_graph, list(GraphAnalyzer._iter_start_ops(chunk_graph)), band_resource |
| 60 | + ) |
| 61 | + assigns = assigner.assign(cur_assigns) |
| 62 | + key_to_assign = dict((c.key, band) for c, band in assigns.items()) |
| 63 | + for subtask in subtask_graph: |
| 64 | + input_chunks = list(subtask.chunk_graph.iter_indep()) |
| 65 | + if all(isinstance(inp.op, TensorFetch) for inp in input_chunks): |
| 66 | + # all inputs are fetch, expect band should be None |
| 67 | + assert subtask.expect_band is None |
| 68 | + else: |
| 69 | + # if subtask has truly initial chunks, expect band should be |
| 70 | + # same as assign results |
| 71 | + for inp in input_chunks: |
| 72 | + if not isinstance(inp.op, TensorFetch): |
| 73 | + assert subtask.expect_band == key_to_assign[inp.key] |
0 commit comments