<commit_before>local w2nn = require 'w2nn'
local reconstruct = require 'reconstruct'
local image = require 'image'
local iproc = require 'iproc'
local gm = require 'graphicsmagick'

alpha_util = {}
alpha_util.sum2d = nn.SpatialConvolutionMM(1, 1, 3, 3, 1, 1, 1, 1):cuda()
alpha_util.sum2d.weight:fill(1)
alpha_util.sum2d.bias:zero()

function alpha_util.make_border(rgb, alpha, offset)
   if not alpha then
      return rgb
   end
   
   local mask = alpha:clone()
   mask[torch.gt(mask, 0.0)] = 1
   mask[torch.eq(mask, 0.0)] = 0
   local mask_nega = (mask - 1):abs():byte()
   local eps = 1.0e-7

   rgb = rgb:clone()
   rgb[1][mask_nega] = 0
   rgb[2][mask_nega] = 0
   rgb[3][mask_nega] = 0

   for i = 1, offset do
      local mask_weight = alpha_util.sum2d:forward(mask:cuda()):float()
      local border = rgb:clone()
      for j = 1, 3 do
	 border[j]:copy(alpha_util.sum2d:forward(rgb[j]:reshape(1, rgb:size(2), rgb:size(3)):cuda()))
	 border[j]:cdiv((mask_weight + eps))
	 rgb[j][mask_nega] = border[j][mask_nega]
      end
      mask = mask_weight:clone()
      mask[torch.gt(mask_weight, 0.0)] = 1
      mask_nega = (mask - 1):abs():byte()
   end
   rgb[torch.gt(rgb, 1.0)] = 1.0
   rgb[torch.lt(rgb, 0.0)] = 0.0

   return rgb
end
function alpha_util.composite(rgb, alpha, model2x)
   if not alpha then
      return rgb
   end
   if not (alpha:size(2) == rgb:size(2) and  alpha:size(3) == rgb:size(3)) then
      if model2x then
	 alpha = reconstruct.scale(model2x, 2.0, alpha)
      else<commit_msg>Fix cuda tensor error in async environment<commit_after>local w2nn = require 'w2nn'
local reconstruct = require 'reconstruct'
local image = require 'image'
local iproc = require 'iproc'
local gm = require 'graphicsmagick'

alpha_util = {}

function alpha_util.make_border(rgb, alpha, offset)
   if not alpha then
      return rgb
   end
   local sum2d = nn.SpatialConvolutionMM(1, 1, 3, 3, 1, 1, 1, 1):cuda()
   sum2d.weight:fill(1)
   sum2d.bias:zero()

   local mask = alpha:clone()
   mask[torch.gt(mask, 0.0)] = 1
   mask[torch.eq(mask, 0.0)] = 0
   local mask_nega = (mask - 1):abs():byte()
   local eps = 1.0e-7

   rgb = rgb:clone()
   rgb[1][mask_nega] = 0
   rgb[2][mask_nega] = 0
   rgb[3][mask_nega] = 0

   for i = 1, offset do
      local mask_weight = sum2d:forward(mask:cuda()):float()
      local border = rgb:clone()
      for j = 1, 3 do
	 border[j]:copy(sum2d:forward(rgb[j]:reshape(1, rgb:size(2), rgb:size(3)):cuda()))
	 border[j]:cdiv((mask_weight + eps))
	 rgb[j][mask_nega] = border[j][mask_nega]
      end
      mask = mask_weight:clone()
      mask[torch.gt(mask_weight, 0.0)] = 1
      mask_nega = (mask - 1):abs():byte()
   end
   rgb[torch.gt(rgb, 1.0)] = 1.0
   rgb[torch.lt(rgb, 0.0)] = 0.0

   return rgb
end
function alpha_util.composite(rgb, alpha, model2x)
   if not alpha then
      return rgb
   end
   if not (alpha:size(2) == rgb:size(2) and  alpha:size(3) == rgb:size(3)) then
      if model2x then
	 alpha = reconstruct.scale(model2x, 2.0, alpha)
      else
{'fraction_non_alphanumeric': 0.15382059800664452, 'fraction_numerical': 0.03920265780730897, 'mean_word_length': 3.4084919472913615, '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': '6393035', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1177, 'n_words': 278}