<commit_before>local BCECriterion, parent = torch.class('nn.BCECriterion', 'nn.Criterion')

function BCECriterion:__init()
   parent.__init(self)
   self.sizeAverage = true
end

function BCECriterion:updateOutput(input, target)
    -- log(input) * target + log(1 - input) * (1 - target)
    self.output = torch.log(input):cmul(target)
    
    self.output:add(torch.add(-input,1):log():cmul(torch.add(-target,1)))

    if self.sizeAverage then
    	self.output:div(target:size(1))
    end

    return self.output:sum()
end

function BCECriterion:updateGradInput(input, target)
    -- target / input - (1 - target) / (1 - input)
    self.gradInput = torch.cdiv(target,input)
    self.gradInput:add(-1,torch.cdiv(torch.add(-target,1),torch.add(-input,1)))

    if self.sizeAverage then
    	self.gradInput:div(target:size(1))
    end
    
    return self.gradInput
end<commit_msg>rewrote BCECriterion for CUDA compat<commit_after>local BCECriterion, parent = torch.class('nn.BCECriterion', 'nn.Criterion')

function BCECriterion:__init()
   parent.__init(self)
   self.sizeAverage = true
end

function BCECriterion:updateOutput(input, target)
    -- log(input) * target + log(1 - input) * (1 - target)

    self.term1 = self.term1 or input.new()
    self.term2 = self.term2 or input.new()
    self.term3 = self.term3 or input.new()

    self.term1:resizeAs(input)
    self.term2:resizeAs(input)
    self.term3:resizeAs(input)

    self.term1:fill(1):add(-1,target)
    self.term2:fill(1):add(-1,input):log():cmul(self.term1)
    
    self.term3:copy(input):log():cmul(target)
    self.term3:add(self.term2)

    if self.sizeAverage then
    	self.term3:div(target:size(1))
    end

    return self.term3:sum()
end

function BCECriterion:updateGradInput(input, target)
    -- target / input - (1 - target) / (1 - input)
    
    self.term1 = self.term1 or input.new()
    self.term2 = self.term2 or input.new()
    
    self.term1:resizeAs(input)
    self.term2:resizeAs(input)
    
    self.term1:fill(1):add(-1,target)
    self.term2:fill(1):add(-1,input)

    self.term1:cdiv(self.term2)

    self.gradInput:resizeAs(input)
    self.gradInput:copy(target):cdiv(input)

    self.gradInput:add(-1,self.term1)

    if self.sizeAverage then
    	self.gradInput:div(target:size(1))
    end
    
    return self.gradInput
end
{'fraction_non_alphanumeric': 0.14532125916343253, 'fraction_numerical': 0.02328589909443726, 'mean_word_length': 3.5224171539961016, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1626775', 'n_tokens_mistral': 847, 'n_tokens_neox': 834, 'n_words': 134}