X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=pysvrt.git;a=blobdiff_plain;f=svrtset.py;h=54c4dec2efd560810be6ea9e76a5b60c82b162c7;hp=cbc71a373237356007bca386de4890036ac902e5;hb=HEAD;hpb=b6cc25f4622917b0025d613d617b5ce9e23f07e9 diff --git a/svrtset.py b/svrtset.py index cbc71a3..54c4dec 100755 --- a/svrtset.py +++ b/svrtset.py @@ -29,6 +29,9 @@ from torch.autograd import Variable import svrt +# FIXME +import resource + ###################################################################### def generate_one_batch(s): @@ -41,17 +44,18 @@ def generate_one_batch(s): class VignetteSet: - def __init__(self, problem_number, nb_samples, batch_size, cuda = False): + def __init__(self, problem_number, nb_samples, batch_size, cuda = False, logger = None): if nb_samples%batch_size > 0: print('nb_samples must be a multiple of batch_size') raise self.cuda = cuda - self.batch_size = batch_size self.problem_number = problem_number - self.nb_batches = nb_samples // batch_size - self.nb_samples = self.nb_batches * self.batch_size + + self.batch_size = batch_size + self.nb_samples = nb_samples + self.nb_batches = self.nb_samples // self.batch_size seeds = torch.LongTensor(self.nb_batches).random_() mp_args = [] @@ -61,6 +65,7 @@ class VignetteSet: self.data = [] for b in range(0, self.nb_batches): self.data.append(generate_one_batch(mp_args[b])) + if logger is not None: logger(self.nb_batches * self.batch_size, b * self.batch_size) # Weird thing going on with the multi-processing, waiting for more info @@ -88,29 +93,51 @@ class VignetteSet: ###################################################################### class CompressedVignetteSet: - def __init__(self, problem_number, nb_samples, batch_size, cuda = False): + def __init__(self, problem_number, nb_samples, batch_size, cuda = False, logger = None): if nb_samples%batch_size > 0: print('nb_samples must be a multiple of batch_size') raise self.cuda = cuda - self.batch_size = batch_size self.problem_number = problem_number - self.nb_batches = nb_samples // batch_size - self.nb_samples = self.nb_batches * self.batch_size + + self.batch_size = batch_size + self.nb_samples = nb_samples + self.nb_batches = self.nb_samples // self.batch_size + self.targets = [] self.input_storages = [] acc = 0.0 acc_sq = 0.0 + usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss for b in range(0, self.nb_batches): target = torch.LongTensor(self.batch_size).bernoulli_(0.5) input = svrt.generate_vignettes(problem_number, target) - acc += input.float().sum() / input.numel() - acc_sq += input.float().pow(2).sum() / input.numel() + + # FIXME input_as_float should not be necessary but there + # are weird memory leaks going on, which do not seem to be + # my fault + if b == 0: + input_as_float = input.float() + else: + input_as_float.copy_(input) + acc += input_as_float.sum() / input.numel() + acc_sq += input_as_float.pow(2).sum() / input.numel() + self.targets.append(target) self.input_storages.append(svrt.compress(input.storage())) + if logger is not None: logger(self.nb_batches * self.batch_size, b * self.batch_size) + + # FIXME + if resource.getrusage(resource.RUSAGE_SELF).ru_maxrss > 16e6: + print('Memory leak?!') + raise + + mem = (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss - usage) * 1024 + print('Using {:.02f}Gb total {:.02f}b / samples' + .format(mem / (1024 * 1024 * 1024), mem / self.nb_samples)) self.mean = acc / self.nb_batches self.std = sqrt(acc_sq / self.nb_batches - self.mean * self.mean)