예제 #1
0
 def __init__(self):
     super().__init__()
     self.view_ = P.Reshape()
     self.CumSum = P.CumSum()
     self.ReduceSum = P.ReduceSum(keep_dims=True)
     self.BatchMatMul_b = P.BatchMatMul(transpose_b=True)
     self.BatchMatMul_a = P.BatchMatMul(transpose_a=True)
     self.Mul = P.Mul()
예제 #2
0
    def __init__(self,
                 probs=None,
                 seed=None,
                 dtype=mstype.int32,
                 name="Categorical"):
        param = dict(locals())
        param['param_dict'] = {'probs': probs}
        valid_dtype = mstype.uint_type + mstype.int_type + mstype.float_type
        Validator.check_type_name("dtype", dtype, valid_dtype,
                                  type(self).__name__)
        super(Categorical, self).__init__(seed, dtype, name, param)

        self._probs = self._add_parameter(probs, 'probs')
        if self.probs is not None:
            check_rank(self.probs)
            check_prob(self.probs)
            check_sum_equal_one(probs)

            # update is_scalar_batch and broadcast_shape
            # drop one dimension
            if self.probs.shape[:-1] == ():
                self._is_scalar_batch = True
            self._broadcast_shape = self._broadcast_shape[:-1]

        self.argmax = P.ArgMaxWithValue(axis=-1)
        self.broadcast = broadcast_to
        self.cast = P.Cast()
        self.clip_by_value = C.clip_by_value
        self.concat = P.Concat(-1)
        self.cumsum = P.CumSum()
        self.dtypeop = P.DType()
        self.exp = exp_generic
        self.expand_dim = P.ExpandDims()
        self.fill = P.Fill()
        self.gather = P.GatherNd()
        self.greater = P.Greater()
        self.issubclass = P.IsSubClass()
        self.less = P.Less()
        self.log = log_generic
        self.log_softmax = P.LogSoftmax()
        self.logicor = P.LogicalOr()
        self.logicand = P.LogicalAnd()
        self.multinomial = P.Multinomial(seed=self.seed)
        self.reshape = P.Reshape()
        self.reduce_sum = P.ReduceSum(keep_dims=True)
        self.select = P.Select()
        self.shape = P.Shape()
        self.softmax = P.Softmax()
        self.squeeze = P.Squeeze()
        self.squeeze_first_axis = P.Squeeze(0)
        self.squeeze_last_axis = P.Squeeze(-1)
        self.square = P.Square()
        self.transpose = P.Transpose()
        self.is_nan = P.IsNan()

        self.index_type = mstype.int32
        self.nan = np.nan
예제 #3
0
 def construct(self):
     return (P.CumSum()(self.x0, self.axis0), P.CumSum()(self.x1,
                                                         self.axis1),
             P.CumSum()(self.x2, self.axis2), P.CumSum()(self.x3,
                                                         self.axis3),
             P.CumSum()(self.x4, self.axis4), P.CumSum()(self.x5,
                                                         self.axis5),
             P.CumSum()(self.x6, self.axis6))
    def __init__(self,
                 batch_size:int,
                 vocab_size:int,
                 k:int=0,
                 p:float=1.0,
                 temperature:float=1.0,
                 min_tokens_to_keep:int=1,
                 fp16:bool=False):
        super(TopKTopP_Filter, self).__init__()

        self.topK = P.TopK(sorted=True)
        self.batch_size = batch_size
        self.vocab_size = vocab_size
        self.min_tokens_to_keep = min_tokens_to_keep
        self.k = k
        self.p = p
        self.temp = temperature
        self.fp16 = fp16

        self.cumsum = P.CumSum()
        
        self.onehot = P.OneHot()
        self.cast = P.Cast()
        self.expand_dims = P.ExpandDims()
        self.device_target = get_context("device_target")
        self.mask = Tensor(
            np.zeros((batch_size, vocab_size)), dtype=mstype.float32)
        self.on_value = Tensor(1.0, mstype.float32)
        self.off_value = Tensor(0.0, mstype.float32)
        self.softmax = P.Softmax()
        self.safety_mask_left = np.zeros(
            (batch_size, min_tokens_to_keep), dtype=float)
        self.safety_mask_right = np.ones((batch_size, 
                                         vocab_size-min_tokens_to_keep), 
                                         dtype=float)
        self.safety_mask = Tensor(np.concatenate((self.safety_mask_left, 
                                                 self.safety_mask_right), axis=1), 
                                                 dtype=mstype.float32)
        self.NINF = float(-1e6)
        
        self.expand_dims = P.ExpandDims()
        assert self.temp > 0.0, 'temperature must be positive'
        assert self.k >= 0, 'the top_k number must be no negative.'
        if self.k > 0:
            assert self.min_tokens_to_keep <= self.k, 'K must be larger than or equal to min_token_to_keep for top p sampling'
예제 #5
0
 def __init__(self):
     super(NetCumSum, self).__init__()
     self.cumsum = P.CumSum()
     self.axis = 1
예제 #6
0
 ('CheckValid', {
     'block': P.CheckValid(),
     'desc_inputs': [[20000, 4], [3]],
     'desc_bprop': [[20000]],
     'skip': ['backward']}),
 ('NMSWithMask', {
     'block': P.NMSWithMask(0.5),
     'desc_inputs': [[128, 5]],
     'desc_bprop': [[128, 5], [128], [128]],
     'skip': ['backward']}),
 ('Abs', {
     'block': P.Abs(),
     'desc_inputs': [[4]],
     'desc_bprop': [[4]]}),
 ('CumSum', {
     'block': P.CumSum(),
     'desc_const': [0],
     'desc_inputs': [Tensor(np.array([[3, 4],[1, 6]]).astype(np.float16))],
     'desc_bprop': [Tensor(np.array([[3, 4],[4, 10]]).astype(np.float16))]}),
 ('ReduceSum_3', {
     'block': P.ReduceSum(),
     'desc_const': [0],
     'desc_inputs': [[3, 2]],
     'desc_bprop': [[2]]}),
 ('ReduceSum_4', {
     'block': P.ReduceSum(keep_dims=True),
     'desc_const': [0],
     'desc_inputs': [[3, 2]],
     'desc_bprop': [[1, 2]]}),
 ('ReduceSum_5', {
     'block': P.ReduceSum(keep_dims=True),
예제 #7
0
 def __init__(self, axis):
     super(CumSumNet, self).__init__()
     self.axis = axis
     self.op = P.CumSum()
    def __init__(self,
                 decoder:Model,
                 model_config=None,
                 generate_length:int=1,
                 tokenizer:Optional[GPT2Tokenizer]=None,
                 topk_num:int=0,
                 topp_prob:float=1.0,
                 temperature:float=1.0,
                 min_tokens_to_keep:int=1,
                 early_stop:bool=False,
                 demo_mode:bool=False,
                 return_ids:bool=False,
                 return_last_token_logits:bool=False,
                 append_eos:bool=False):

       
        assert model_config is not None, 'Config is a must for sampling.'
        
        self.model_config = model_config
        self.topk_num = topk_num
        self.topp_prob = topp_prob
        self.temperature = temperature
        self.min_tokens_to_keep = min_tokens_to_keep
        
        self.decoder = decoder
        self.tokenizer = tokenizer
        self.reshape = P.Reshape()
        self.cumsum = P.CumSum()
        self.onehot = P.OneHot()
        self.generate_length = generate_length
        self.seq_length = model_config.seq_length
        self.batch_size = model_config.batch_size
        self.vocab_size = model_config.vocab_size
        
        self.on_value = Tensor(1.0, mstype.float32)
        self.off_value = Tensor(0.0, mstype.float32)
        self.cast = P.Cast()
        self.concat = P.Concat()
        self.early_stop = early_stop
        self.demo_mode = demo_mode
        self.return_ids = return_ids
        self.return_last_token_logits = return_last_token_logits
        self.append_eos = append_eos
        self.device_target = get_context("device_target")

        #different choice of sample function for adjusting several device target types
        if self.device_target == "GPU":
            self.sample_function = P.Multinomial(seed=1)
        elif self.device_target == "Ascend":
            self.sample_function = P.RandomCategorical(mstype.int32)
        else:
            raise NotImplementedError("Device Target {} not supported.".format(self.device_target))

        self.filter_distribution = TopKTopP_Filter(self.batch_size,
                                                   self.vocab_size,
                                                   k=self.topk_num,
                                                   p=self.topp_prob,
                                                   temperature=self.temperature,
                                                   min_tokens_to_keep=self.min_tokens_to_keep)

        if self.tokenizer is not None:
            self.eos_id = self.tokenizer.eos_token_id
        else:
            self.eos_id = model_config.vocab_size-1

        if self.tokenizer is not None:
            self.eos_text = self.tokenizer.eos_token
        else:
            self.eos_text = "<|endoftext|>"

        if self.demo_mode is True:
            assert self.batch_size == 1, 'Demo mode requires batchsize euqals to 1, but get batch_size={}'.format(
                self.batch_size)
예제 #9
0
    def __init__(self, exclusive=False, reverse=False):
        super(CumSum, self).__init__()
        self.cumsum_op = P.CumSum(exclusive, reverse)

        self.x0 = Tensor(x0)
        self.axis0 = axis0
        self.x1 = Tensor(x0)
        self.axis1 = axis1
        self.x2 = Tensor(x0)
        self.axis2 = axis2
        self.x3 = Tensor(x0)
        self.axis3 = axis3
        self.x4 = Tensor(x0)
        self.axis4 = axis4
        self.x5 = Tensor(x0)
        self.axis5 = axis5
        self.x6 = Tensor(x0)
        self.axis6 = axis6

        self.x7 = Tensor(x1)
        self.axis7 = axis0
        self.x8 = Tensor(x1)
        self.axis8 = axis1
        self.x9 = Tensor(x1)
        self.axis9 = axis2
        self.x10 = Tensor(x1)
        self.axis10 = axis3
        self.x11 = Tensor(x1)
        self.axis11 = axis4
        self.x12 = Tensor(x1)
        self.axis12 = axis5
        self.x13 = Tensor(x1)
        self.axis13 = axis6

        self.x14 = Tensor(x2)
        self.axis14 = axis0
        self.x15 = Tensor(x2)
        self.axis15 = axis1
        self.x16 = Tensor(x2)
        self.axis16 = axis2
        self.x17 = Tensor(x2)
        self.axis17 = axis3
        self.x18 = Tensor(x2)
        self.axis18 = axis4
        self.x19 = Tensor(x2)
        self.axis19 = axis5
        self.x20 = Tensor(x2)
        self.axis20 = axis6

        self.x21 = Tensor(x3)
        self.axis21 = axis0
        self.x22 = Tensor(x3)
        self.axis22 = axis1
        self.x23 = Tensor(x3)
        self.axis23 = axis2
        self.x24 = Tensor(x3)
        self.axis24 = axis3
        self.x25 = Tensor(x3)
        self.axis25 = axis4
        self.x26 = Tensor(x3)
        self.axis26 = axis5
        self.x27 = Tensor(x3)
        self.axis27 = axis6

        self.x28 = Tensor(x4)
        self.axis28 = axis0
        self.x29 = Tensor(x4)
        self.axis29 = axis1
        self.x30 = Tensor(x4)
        self.axis30 = axis2
        self.x31 = Tensor(x4)
        self.axis31 = axis3
        self.x32 = Tensor(x4)
        self.axis32 = axis4
        self.x33 = Tensor(x4)
        self.axis33 = axis5
        self.x34 = Tensor(x4)
        self.axis34 = axis6

        self.x35 = Tensor(x5)
        self.axis35 = axis0
예제 #10
0
    def __init__(
        self,
        decoder,
        config=None,
        batch_size=None,
        tokenizer=None,
        generate_length=1,
        topk_num=0,
        topp_prob=1.0,
        temperature=1.0,
        min_tokens_to_keep=1,
        early_stop=False,
        demo_mode=False,
        return_ids=False,
        return_last_token_logits=False,
        append_eos=False,
    ):

        assert config is not None, 'Config is a must for sampling.'

        self.decoder = decoder
        self.config = config
        self.tokenizer = tokenizer
        self.generate_length = generate_length
        self.topk_num = topk_num
        self.topp_prob = topp_prob
        self.temperature = temperature
        self.min_tokens_to_keep = min_tokens_to_keep
        self.early_stop = early_stop
        self.demo_mode = demo_mode
        self.return_ids = return_ids
        self.return_last_token_logits = return_last_token_logits
        self.append_eos = append_eos

        self.seq_length = config.seq_length
        self.batch_size = config.batch_size if batch_size is None else batch_size
        self.vocab_size = config.vocab_size

        self.on_value = Tensor(1.0, mstype.float32)
        self.off_value = Tensor(0.0, mstype.float32)
        self.reshape = P.Reshape()
        self.cumsum = P.CumSum()
        self.onehot = P.OneHot()
        self.cast = P.Cast()
        self.concat = P.Concat()
        self.sample_function = P.RandomCategorical(mstype.int32)
        self.filter_distribution = TopKTopP_Filter(
            batch_size=self.batch_size,
            vocab_size=self.vocab_size,
            k=self.topk_num,
            p=self.topp_prob,
            temperature=self.temperature,
            min_tokens_to_keep=self.min_tokens_to_keep)

        if self.tokenizer is not None:
            self.eos_id = self.tokenizer.eos_token_id
        else:
            self.eos_id = config.vocab_size - 1

        if self.tokenizer is not None:
            self.eos_text = self.tokenizer.eos_token
        else:
            self.eos_text = "<|endoftext|>"

        if self.demo_mode is True:
            assert self.batch_size == 1, 'Demo mode requires batchsize euqals to 1, but get batch_size={}'.format(
                self.batch_size)