File size: 11,637 Bytes
0fd26a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""
WebDataset distributed utility functions, pipeline helper functions and sampler classes.
"""

from torch.utils.data import IterableDataset
import torch
import math
import random
import os
import logging

import braceexpand


def log_and_continue(exn):
    """Call in an exception handler to ignore any exception, issue a warning, and continue."""
    if "No images in sample" in str(exn) or "Only one image in sample" in str(exn):
        return True
    
    if isinstance(exn, FileNotFoundError) or "FileNotFoundError" in str(type(exn)):
        if os.environ.get("RANK", "0") == "0":
            logging.warning(f"Handling webdataset FileNotFoundError: {exn}. Ignoring and continuing.")
        return True
    
    logging.warning(f"Handling webdataset error ({repr(exn)}). Ignoring.")
    return True


# Distributed environment detection and shard allocation

def pytorch_worker_info(group=None):
    """Return node and worker info for PyTorch and some distributed environments."""
    rank = 0
    world_size = 1
    worker = 0
    num_workers = 1
    if "RANK" in os.environ and "WORLD_SIZE" in os.environ:
        rank = int(os.environ["RANK"])
        world_size = int(os.environ["WORLD_SIZE"])
    else:
        try:
            import torch.distributed
            if torch.distributed.is_available() and torch.distributed.is_initialized():
                group = group or torch.distributed.group.WORLD
                rank = torch.distributed.get_rank(group=group)
                world_size = torch.distributed.get_world_size(group=group)
        except ModuleNotFoundError:
            pass
    if "WORKER" in os.environ and "NUM_WORKERS" in os.environ:
        worker = int(os.environ["WORKER"])
        num_workers = int(os.environ["NUM_WORKERS"])
    else:
        try:
            import torch.utils.data
            worker_info = torch.utils.data.get_worker_info()
            if worker_info is not None:
                worker = worker_info.id
                num_workers = worker_info.num_workers
        except ModuleNotFoundError:
            pass
    return rank, world_size, worker, num_workers


def is_multi_node_environment():
    """
    check if in a multi-process (world_size > 1) environment.
    """
    try:
        import torch.distributed as dist
        if dist.is_available() and dist.is_initialized():
            if dist.get_world_size() > 1:
                return True
    except Exception:
        pass

    world_size = int(os.environ.get("WORLD_SIZE", os.environ.get("SLURM_NTASKS", "1")))
    nnodes = int(os.environ.get("NNODES", os.environ.get("SLURM_NNODES", "1")))
    if nnodes > 1:
        return True
    return world_size > 1


def split_data_by_node(urls, strategy="interleaved"):
    """split shards between nodes, even if the data is stored locally, it is recommended to use it to avoid duplicate training."""
    print('*'*80)
    print("split_data_by_node ing..................")
    gpus_per_node = torch.cuda.device_count()
    rank, world_size, worker, num_workers = pytorch_worker_info()
    print("rank: {}, world_size: {}, worker: {}, num_workers: {}, gpus_per_node: {}".format(
        rank, world_size, worker, num_workers, gpus_per_node))
    
    node_rank = rank // gpus_per_node
    node_world_size = world_size // gpus_per_node

    if len(urls) < node_world_size:
        print(f"Warning: Only {len(urls)} shards but {node_world_size} nodes. "
              f"All nodes will use all shards to avoid empty assignment.")
        print(f"Node {node_rank} has {len(urls)} URLs of {len(urls)} total.")
        print('*'*80)
        return urls

    if strategy == "chunk":
        urls_per_node = math.ceil(len(urls) / node_world_size)
        start_idx = node_rank * urls_per_node
        end_idx = min(start_idx + urls_per_node, len(urls))
        node_urls = urls[start_idx:end_idx]
    elif strategy == "interleaved":
        node_urls = urls[node_rank::node_world_size]
    elif strategy == "shuffled_chunk":
        shuffled_urls = random.sample(urls, len(urls))
        urls_per_node = math.ceil(len(shuffled_urls) / node_world_size)
        start_idx = node_rank * urls_per_node
        end_idx = min(start_idx + urls_per_node, len(urls))
        node_urls = shuffled_urls[start_idx:end_idx]
    else:
        raise ValueError(f"Unknown strategy {strategy}")
    
    print(f"Node {node_rank} has {len(node_urls)} URLs of {len(urls)} total.")
    print('*'*80)
    return node_urls


def get_dataset_size(shards, estimated_sample_per_shard=1000):
    """estimate the dataset size, based on the number of shards."""
    if ',' in shards:
        shards_list = []
        for pattern in shards.split(','):
            pattern = pattern.strip()
            if not pattern:
                continue
            shards_list.extend(list(braceexpand.braceexpand(pattern)))
    else:
        shards_list = list(braceexpand.braceexpand(shards))
    num_shards = len(shards_list)
    
    total_size = num_shards * estimated_sample_per_shard
    print(f"Estimating dataset size: {total_size} samples ({num_shards} shards * {estimated_sample_per_shard} samples/shard)")
    return total_size, num_shards


# Pipeline helper functions (module level, supports pickle/spawn)

def nodesplitter_identity(urls):
    return urls


def handle_reconstruction_task(sample, handler=log_and_continue):
    in_key = None
    if "in.png" in sample:
        in_key = "in.png"
    elif "in.jpg" in sample:
        in_key = "in.jpg"
    
    out_key = None
    if "out.png" in sample:
        out_key = "out.png"
    elif "out.jpg" in sample:
        out_key = "out.jpg"
    
    if in_key and not out_key:
        if in_key == "in.png":
            sample["out.png"] = sample["in.png"]
        else:
            sample["out.jpg"] = sample["in.jpg"]
    
    return sample


def extract_fields_to_tuple(sample, handler=log_and_continue):
    in_img = sample.get("in.png") or sample.get("in.jpg")
    out_img = sample.get("out.png") or sample.get("out.jpg")
    if out_img is None and in_img is not None:
        out_img = in_img
    sample_type = sample.get("type", None)
    
    return (in_img, out_img, sample_type)


def identity_function(x, handler=log_and_continue):
    return x


def has_input_image(sample):
    return "in.png" in sample or "in.jpg" in sample


class WeightedRoundRobinSampler(IterableDataset):
    def __init__(self, pipelines, weights):
        super().__init__()
        if len(weights) != len(pipelines):
            raise ValueError(f"number of weights ({len(weights)}) must be equal to the number of pipelines ({len(pipelines)})")
        
        self.pipelines = pipelines
        self.weights = weights
        
        total_weight = sum(weights)
        normalized_weights = [w / total_weight for w in weights]
        
        max_decimal_places = max(len(str(w).split('.')[-1]) if '.' in str(w) else 0 for w in normalized_weights)
        scale_factor = 10 ** max_decimal_places
        int_weights = [int(w * scale_factor) for w in normalized_weights]
        
        def gcd(a, b):
            while b:
                a, b = b, a % b
            return a
        
        def gcd_list(nums):
            result = nums[0]
            for num in nums[1:]:
                result = gcd(result, num)
            return result
        
        common_divisor = gcd_list(int_weights)
        int_weights = [w // common_divisor for w in int_weights]
        
        self.sampling_sequence = []
        for i, weight in enumerate(int_weights):
            self.sampling_sequence.extend([i] * weight)
    
    def __iter__(self):
        import itertools
        
        iterators = [iter(p) for p in self.pipelines]
        sequence_iter = itertools.cycle(self.sampling_sequence)
        active = [True] * len(iterators)
        
        while True:
            if not any(active):
                break
            
            idx = next(sequence_iter)
            if active[idx]:
                try:
                    yield next(iterators[idx])
                except StopIteration:
                    active[idx] = False
                    if not any(active):
                        break
                    continue


class StrictProportionalBatchSampler(IterableDataset):
    """
    a strictly proportional batch sampler (适用于 resampled=True)
    ensure that the samples in each batch are strictly allocated according to the weight ratio
    """
    def __init__(self, pipelines, weights, batch_size):
        super().__init__()
        if len(weights) != len(pipelines):
            raise ValueError(f"number of weights ({len(weights)}) must be equal to the number of pipelines ({len(pipelines)})")

        self.pipelines = pipelines
        self.weights = weights
        self.batch_size = batch_size
        
        total_weight = sum(weights)
        normalized_weights = [w / total_weight for w in weights]
        
        self.samples_per_pipeline = []
        float_counts = [batch_size * w for w in normalized_weights]
        
        int_counts = [round(c) for c in float_counts]
        
        current_sum = sum(int_counts)
        diff = batch_size - current_sum
        
        if diff != 0:
            errors = [(float_counts[i] - int_counts[i], i) for i in range(len(int_counts))]
            errors.sort(reverse=(diff > 0))
            
            for _ in range(abs(diff)):
                _, idx = errors.pop(0)
                int_counts[idx] += 1 if diff > 0 else -1
        
        self.samples_per_pipeline = int_counts
        
        weight_strs = [f"{w*100:.1f}%" for w in normalized_weights]
        sample_strs = [f"{count}" for count in self.samples_per_pipeline]
        actual_ratios = [f"{count/batch_size*100:.1f}%" for count in self.samples_per_pipeline]
        print(f"Strict proportional batch sampling enabled:")
        print(f"  Target weights: {' : '.join(weight_strs)}")
        print(f"  Actual samples per batch: {' : '.join(sample_strs)} (total={batch_size})")
        print(f"  Actual ratios: {' : '.join(actual_ratios)}")
    
    def __iter__(self):
        import random as _random
        
        iterators = [iter(p) for p in self.pipelines]
        
        while True:
            batch_samples = []
            
            for idx, count in enumerate(self.samples_per_pipeline):
                for _ in range(count):
                    sample = next(iterators[idx])
                    batch_samples.append(sample)
            
            _random.shuffle(batch_samples)
            
            normalized_samples = []
            for sample in batch_samples:
                if len(sample) == 3:
                    normalized_samples.append((sample[0], sample[1], sample[2], None))
                elif len(sample) == 4:
                    normalized_samples.append(sample)
                else:
                    raise ValueError(f"Unexpected sample length: {len(sample)}")
            
            batch_transposed = list(zip(*normalized_samples))
            
            batch_results = []
            for idx, items in enumerate(batch_transposed):
                if idx < 3:
                    filtered_items = [item for item in items if item is not None]
                    if len(filtered_items) != len(items):
                        raise ValueError(f"Found None in tensor items at index {idx}")
                    batch_results.append(torch.stack(list(filtered_items)))
                else:
                    type_list = list(items)
                    batch_results.append(type_list)
            
            yield tuple(batch_results)